2017-12-01 7 views
0

geom_col()と同様に、稜線の代わりに棒の集合を描画するのにggridgesパッケージを使用できますか?棒グラフにR ggridges(R-joyplot)を使用する

dt = tibble(
    hr = c(1,2,3,4,1,2,3,4), 
    fr = c(.1,.5,.9,.1,.4,.9,.9,.4), 
    gr = c('Mon','Mon','Mon','Mon','Sun','Sun','Sun','Sun') 
) 

下のプロットは私を与える:

は、私は、次のようなデータを持っている

ggplot(dt, aes(x=hr, y=gr, height=fr)) + 
    geom_ridgeline() + ylab(NULL) 

enter image description here

あなたはそれが値を結ぶ線を描画します見ることができるように。私が代わりを探していますと、このプロットのように、個々の列です。ここで

ggplot(dt, aes(x=hr, y=fr)) + 
    geom_col() + ylab(NULL) + 
    facet_wrap(~gr) 

enter image description here

+0

_「私はそれがx値との間に一定の高さを表していることが必要です。」_私はそれが何を意味するのか理解していません。おそらくあなたが実際に望むものを描くことができます。 'geom_ridgeline'のスムージングバージョンをお探しですか? – Axeman

+0

半分焼いた質問に申し訳ありません。私は質問を更新した – kbk78

+0

[MCVE。](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) –

答えて

1

は、個々のバーをトレースするソリューションです。

library(tidyverse) 
library(ggridges) 

dt = tibble(
    hr = c(1,2,3,4,1,2,3,4), 
    fr = c(.1,.5,.9,.1,.4,.9,.9,.4), 
    gr = c('Mon','Mon','Mon','Mon','Sun','Sun','Sun','Sun') 
) 

# function that turns an x, y pair into the shape of a bar of given width 
make_bar <- function(x, y, width = 0.9) { 
    xoff <- width/2 
    data.frame(x = c(x-xoff*(1+2e-8), x-xoff*(1+1e-8), x-xoff, x+xoff, x+xoff*(1+1e-8), x+xoff*(1+2e-8)), 
      height = c(NA, 0, y, y, 0, NA)) 
} 

# convert data table using make_bar function 
dt %>% 
    mutate(bars = map2(hr, fr, ~make_bar(.x, .y))) %>% 
    unnest() -> dt_bars 

ggplot(dt_bars, aes(x=x, y=gr, height=height)) + 
    geom_ridgeline() + ylab(NULL) 

enter image description here

関連する問題