2015-10-07 78 views
0

我正在尝试制作R ggplot2图表,其中列将一个变量分组,一个facet_grid以另一个变为分解。我已经试过这两种方法都失败了:ggplot2柱状图与facet_grid

1)设置x作为facet_grid变量发生故障时通过可变两次:

ggplot(diamonds, aes(x=cut, y=price, fill=color)) + 
    geom_bar(position="dodge", stat="identity") + 
    facet_grid(cut ~ ., scales="free") 

enter image description here

2)删除x产生一个错误:

ggplot(diamonds, aes(y=price, fill=color)) + 
    geom_bar(position="dodge", stat="identity") + 
    facet_grid(cut ~ ., scales="free") 
# Error in exists(name, envir = env, mode = mode): 
# argument "env" is missing, with no default 

回答

4

x设置为与相同的变量0 ARG:

ggplot(diamonds, aes(x=color, y=price, fill=color)) + 
    geom_bar(position="dodge", stat="identity") + 
    facet_grid(cut ~ ., scales="free") 

enter image description here

+0

这些价格值似乎是不准确的。例如,Fair和D型钻石的平均价格为4291美元,而该图表显示的价值为3x。 diamond>% filter(cut ==“Fair”,color ==“D”)%>% summary(price = mean(price,na.rm = TRUE)) – Joe