2017-06-13 69 views
-1

我需要使用下面的數據組和變量的箱線圖:Barplot與小組和分組

df<-as.data.frame(cbind(c(1,2,3), 
         c(0.4,-0.11,-0.07), 
         c(0.31,0.07,0), 
         c(0.45,-0.23,0.02))) 
names(df)<-c('cat','var1','var2','var3') 

我需要與cat1在橫座標barplot每個變量的測量縱座標。

例如,關於cat=1,我需要在橫座標中表示cat1的數量,其中3個barlot表示(var1,.. var3)的值。

+0

那你試試?什麼地方出了錯?您可以在ggplot [here](http://ggplot2.tidyverse.org/reference/geom_bar.html)中瞭解更多關於酒吧情節的內容。 – GGamba

回答

0
library(tidyverse) 
df <- df %>% 
    gather(var, val, -cat) 

ggplot(df, aes(cat, val, fill=var)) + 
    geom_col(position="dodge") 

enter image description here