2017-06-13 94 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