为多个数据子集制作散点图 [英] Make scatter plots for multiple subsets of data

查看:56
本文介绍了为多个数据子集制作散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Let me introduce my data-set and my preliminary result first for better understanding my question. my dataset looks like:

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

我想为每个Place的每个Species制作Size相对于Conc.的散点图.我所做的是使用 ggplot2 制作如下图:

I want to make scatterplot of Size against Conc. for each Species at each Place. What I have done uses ggplot2 to make a graph as below:

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

虽然此图按物种组以不同颜色绘制,但它汇总了数据集中的所有数据,无法针对不同位置绘制.

Though this graph plots by the species group in different color, it summarizes all data in the dataset and fails to plot for different places.

我认为下面的代码

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

仅用于绘制 A 位置,我可以对不同的地方一一执行此操作.但是,在我的真实数据集中,place 变量有很多不同的地方,我无法手动将它们全部输入.因此,我的问题实际上是如何让 R 一次自动为不同的地方绘制这些图?

works for plotting just place A and I can do this for different places one by one. However, in my real dataset, the place variable has tons of different places, and I can't type them all out one by one manually. Thus my question actually is how to let R make those plots for different places automatically at one time?

推荐答案

尝试:

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

如果位置太多:

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

或者,在一张图中:

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

这篇关于为多个数据子集制作散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆