r facet_wrap 未与 geom_point 正确分组

djd*_*ick 2 r ggplot2 facet-wrap

我正在与 R 中的facet_wrap 作斗争。它应该很简单,但是facet 变量没有被拾取?这是我正在运行的:

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
Run Code Online (Sandbox Code Playgroud)

结果图

我对分面变量进行了着色,以试图帮助说明正在发生的事情。该图的每个方面应该只有一种颜色,而不是您在此处看到的颜色。有谁知道发生了什么事吗?

Did*_*rts 5

此错误是由于您正在使用$数据框名称来引用aes(). 使用时,ggplot()您应该只使用变量名称,aes()因为数据框已在 中命名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
Run Code Online (Sandbox Code Playgroud)

这是使用钻石数据集的示例。

diamonds2<-diamonds[sample(nrow(diamonds),1000),]

ggplot(diamonds2,aes(diamonds2$carat,diamonds2$price,color=diamonds2$color))+geom_point()+
          facet_wrap(~color)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

ggplot(diamonds2,aes(carat,price,color=color))+geom_point()+
  facet_wrap(~color)    
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述