R 在ggplot2中叠加散射图
我使用ggplot2在R中创建散点图,以可视化随时间变化的人口。我的数据集如下所示:R 在ggplot2中叠加散射图,r,plot,ggplot2,scatter-plot,R,Plot,Ggplot2,Scatter Plot,我使用ggplot2在R中创建散点图,以可视化随时间变化的人口。我的数据集如下所示: sampling_period cage total_1 total_2 total_3 4 y 34 95 12 4 n 89 12 13 5 n
sampling_period cage total_1 total_2 total_3
4 y 34 95 12
4 n 89 12 13
5 n 23 10 2
我已经用以下代码为变量total_1、total_2和total_3绘制了单独的散点图:
qplot(data=BLPcaged, x=sampling_period, y=total_1, color=cage_or_control)
qplot(data=BLPcaged, x=sampling_period, y=total_2, color=cage_or_control)
qplot(data=BLPcaged, x=sampling_period, y=total_3, color=cage_or_control)
我想创建一个散点图,其中包含随时间变化的所有三个种群的信息。我希望最终产品由三个散点图组成,一个在另一个上面,轴的比例相同。通过这种方式,我可以在一个可视化中比较所有三个群体
我知道我可以使用facet为因子的级别绘制不同的图,但它也可以用于为不同的变量创建不同的图吗?您可以使用melt()
将total
作为可以刻面的因子来重塑数据:
BLPcaged = data.frame(sampling_period=c(4,4,5),
cage=c('y','n','n'),
total_1=c(34,89,23),
total_2=c(95,12,10),
total_3=c(12,13,2))
library(reshape2)
BLPcaged.melted = melt(BLPcaged,
id.vars=c('sampling_period','cage'),
variable.name='total')
所以现在BLPcaged.melled
看起来像这样:
sampling_period cage total value
1 4 y total_1 34
2 4 n total_1 89
3 5 n total_1 23
4 4 y total_2 95
5 4 n total_2 12
6 5 n total_2 10
7 4 y total_3 12
8 4 n total_3 13
9 5 n total_3 2
然后,您可以通过total
来区分这一点:
ggplot(BLPcaged.melted, aes(sampling_period, value, color=cage)) +
geom_point() +
facet_grid(total~.)