为多个数据子集制作散点图

时间:2014-10-28 03:44:06

标签: r plot

让我先介绍一下我的数据集和初步结果,以便更好地理解我的问题。我的数据集看起来像:

Place      Species      Size      Conc.
 A           BT          24        0.2
 A           ST          76        1.4
 ...
 B           BT          45        1.2
 B           ST          21        0.7
 ...

我希望在每个Size为每个Conc.制作Species点对Place的散点图。我所做的工作使用ggplot2制作如下图表:

scatterplot <- ggplot(mydata, aes(x = Size, y = Conc, color = Species)) + 
  geom_point(shape = 1) 

虽然该图以不同颜色的物种群绘制,但它总结了数据集中的所有数据,无法绘制不同的地方。

我认为下面的代码

scatterplot <- ggplot(mydata[mydata$place == "A"], aes(x = Size, y = Conc, color = Species)) + geom_point(shape = 1) 

用于绘制地点A,我可以逐个为不同的地方做这个。但是,在我的真实数据集中,place变量有很多不同的地方,我无法手动逐个输入。因此,我的问题实际上是如何让R一次自动为不同的地方制作这些情节?

1 个答案:

答案 0 :(得分:1)

尝试:

 ggplot(ddf)+geom_point(aes(Size, Conc.))+facet_grid(Place~Species)

enter image description here

如果地方太多:

ggplot(ddf)+geom_point(aes(Size, Conc., color=Place))+facet_grid(.~Species)

enter image description here

或者,在一张图中:

ggplot(ddf)+geom_point(aes(Size, Conc., color=Place,shape=Species), size=5)

enter image description here

相关问题