简体   繁体   English

带有facet_grid的ggplot2柱形图

[英]ggplot2 column chart with facet_grid

I'm trying to make a R ggplot2 chart with columns grouping one variable, and a facet_grid to break down by another. 我正在尝试制作一个R ggplot2图表,其中的列将一个变量分组,而facet_grid其分解为另一个变量。 Both ways I've tried have failed: 我尝试过的两种方法都失败了:

1) Setting x as the facet_grid variable breaks down by that variable twice: 1)将x设置为facet_grid变量会两次被该变量分解:

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

在此处输入图片说明

2) Dropping x produces an error: 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

Set x to the same variable as the color arg: x设置为与arg color相同的变量:

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM