[英]plotting 2 datasets with unequal lengths using ggplot2
I have 2 datasets with unequal lengths for plotting using ggplots2:我有 2 个长度不等的数据集用于使用 ggplots2 进行绘图:
Data A;
column x column y
0.23 1.54
0.44 1.46
0.69 1.37
0.70 1.21
0.75 1.01
0.88 0.91
Data B:
column x column y
0.13 1.24
0.34 1.16
0.49 1.07
0.54 0.99
0.69 1.01
I'm sure of how to write a code in ggplot2 for plotting these two data sets together.我确定如何在 ggplot2 中编写代码以将这两个数据集绘制在一起。 In both cases, plots shown as x axis = column x and y axis= column y.
在这两种情况下,图显示为 x 轴 = x 列和 y 轴 = y 列。 Can someone help me please?
有人能帮助我吗?
James詹姆士
Suppose you have datasets A and B as a data.frame:假设您有数据集 A 和 B 作为 data.frame:
A <- data.frame(x=1:5, y=11:15)
B <- data.frame(x=1:10, y=20:11)
You have to join them together:您必须将它们连接在一起:
df <- rbind(A, B) # Join A and B together.
df
x y
1 1 11
2 2 12
3 3 13
4 4 14
5 5 15
6 1 20
7 2 19
8 3 18
9 4 17
10 5 16
11 6 15
12 7 14
13 8 13
14 9 12
15 10 11
Then you can plot it:然后你可以绘制它:
ggplot(data=df, aes(x=x, y=y)) + geom_point()
If you want to distinguish points from dataset A and B by color:如果要通过颜色区分数据集 A 和 B 中的点:
df$dataset <- c(rep("A", nrow(A)), rep("B", nrow(B)))
df
x y dataset
1 1 11 A
2 2 12 A
3 3 13 A
4 4 14 A
5 5 15 A
6 1 20 B
7 2 19 B
8 3 18 B
9 4 17 B
10 5 16 B
11 6 15 B
12 7 14 B
13 8 13 B
14 9 12 B
15 10 11 B
ggplot(data=df, aes(x=x, y=y, col=dataset)) + geom_point()
If you want to distinguish points from dataset A and B by color and size and change axis labels:如果要通过颜色和大小区分数据集 A 和 B 中的点并更改轴标签:
ggplot(data=df, aes(x=x, y=y, col=dataset, size=dataset)) + geom_point() +
scale_color_manual(name="Dataset", labels = c("Data A","Data B"), values=c("red", "blue")) +
scale_size_manual(name="Dataset", labels = c("Data A","Data B"), values=c(10, 5)) +
xlab("xxxx") + ylab("yyyy")
I know this comes up all the time when looking to plot data points (which are sparse) and aa line from a theoretical curve (which has lots of data points)我知道在从理论曲线(有很多数据点)绘制数据点(稀疏)和一条线时,总是会出现这种情况
In this case you can give the different aesthetic mappings to each piece of ggplot's geometries, individually.在这种情况下,您可以分别为 ggplot 的每个几何图形提供不同的美学映射。
Eg [EDITED HERE TO MAKE THE BEST EXAMPLE BE FIRST]例如[在此处编辑以制作最佳示例为先]
ggplot() +
geom_point(data = df_A, aes(x, y)) +
geom_line(data = df_B, aes(x, y), color = "red") +
theme_minimal()
or要么
ggplot() +
with(df_A, geom_point(aes(x, y))) +
with(df_B, geom_line(aes(x, y)), color = "red") +
theme_minimal()
One option is to get the data into one data.frame.一种选择是将数据放入一个 data.frame 中。 Here's an example using
ldply()
from plyr()
, assuming your data.frames are named d1
and d2
:这是使用
ldply()
的plyr()
的示例,假设您的 data.frames 被命名为d1
和d2
:
library(plyr)
> d3 <- ldply(list(d1 = d2, d2 = d2))
> rbind(head(d3,2), tail(d3,2))
.id column.x column.y
1 d1 0.13 1.24
2 d1 0.34 1.16
9 d2 0.54 0.99
10 d2 0.69 1.01
Or simply pass the different datasets to different geoms when plotting.或者在绘图时简单地将不同的数据集传递给不同的几何体。 Something like this:
像这样的东西:
ggplot() +
geom_point(data = d1, aes(column.x, column.y)) +
geom_point(data = d2, aes(column.x, column.y), colour = "red")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.