r facet_wrap不能用geom_point正确分组 [英] r facet_wrap not grouping properly with geom_point
问题描述
我在R的facet_wrap中挣扎。它应该很简单,但是facet变量没有被拾取?这是我正在运行:
I'm struggling with facet_wrap in R. It should be simple however the facet variable is not being picked up? Here is what I'm running:
plot = ggplot(data = item.household.descr.count, mapping = aes(x=item.household.descr.count$freq, y = item.household.descr.count$descr, color = item.household.descr.count$age.cat)) + geom_point()
plot = plot + facet_wrap(~ age.cat, ncol = 2)
plot
我给faceting变量着色以试图说明发生了什么。情节应该在每个方面只有一种颜色,而不是你在这里看到的。有人知道发生了什么吗?
I colored the faceting variable to try to help illustrate what is going on. The plot should have only one color in each facet instead of what you see here. Does anyone know what is going on?
推荐答案
这个错误是由于您使用 $
和数据框名称来引用 aes()
中的变量。使用 ggplot()
,您应该只使用 aes()
中的变量名称,因为数据框已在 data =
。
This error is caused by fact that you are using $
and data frame name to refer to your variables inside the aes()
. Using ggplot()
you should only use variables names in aes()
as data frame is named already in data=
.
plot = ggplot(data = item.household.descr.count,
mapping = aes(x=freq, y = descr, color = age.cat)) + geom_point()
plot = plot + facet_wrap(~ age.cat, ncol = 2)
plot
这是一个使用钻石数据集的例子。
Here is an example using diamonds dataset.
diamonds2<-diamonds[sample(nrow(diamonds),1000),]
ggplot(diamonds2,aes(diamonds2$carat,diamonds2$price,color=diamonds2$color))+geom_point()+
facet_wrap(~color)
ggplot(diamonds2,aes(carat,price,color=color))+geom_point()+
facet_wrap(~color)
这篇关于r facet_wrap不能用geom_point正确分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!