创建由 R 中的两个分组变量分组的数据集的多面板图

Creating a multi-panel plot of a data set grouped by two grouping variables in R

我正在尝试解决以下练习:

根据参数'diam'和'na'用“faceting”绘制变量'K1'和'K2'之间关系的散点图(细分canvas 通过这两个变量)。最后,根据环的 'thickness' 为点分配不同的颜色(不要忘记之前将其分解)。该图应该类似于这个(“grosor”代表“thickness”):

现在,我最后尝试使用的代码是下面的 (数据集称为“qerat”)ggplot(qerat, aes(K1,K2, fill=factor(grosor))) + geom_point() + facet_wrap(vars(diam,na))

¿谁能帮我指出错误在哪里? ¡非常感谢!

也许您正在寻找 facet_grid() 方法。这里的代码使用与您类似的数据:

library(ggplot2)
#Data
data("diamonds")
#Plot
ggplot(diamonds,aes(x=carat,y=price,color=factor(cut)))+
  geom_point()+
  facet_grid(color~clarity)

输出:

对于您的代码,由于没有数据,我建议进行下一步更改:

#Code
ggplot(qerat, aes(K1,K2, color=factor(grosor)))+
  geom_point() +
  facet_grid(diam~na)