简体   繁体   English

R - 将 ggplot2 与两个不同大小的数据集一起使用

[英]R - Using ggplot2 with two data sets of different sizes

I want to do a scatter plot for two datasets with different sizes.我想为两个不同大小的数据集做一个散点图。

Imagine I have two data.frames: df1, df2.想象一下,我有两个 data.frames:df1、df2。 The row size of df1 is 100, and of df2 is 50. Is there a way to do a scatter plot with ggplot2? df1 的行大小是 100,df2 的行大小是 50。有没有办法用 ggplot2 做散点图? I've searched but couldn't find anything.我已经搜索过但找不到任何东西。 The online tutorials always assume that the datasets are of the same size, with equal value for the x-axis.在线教程总是假设数据集大小相同,x 轴的值相同。 Also, I want to plot the two datasets in the same graph, not side-by-side.另外,我想在同一个图中绘制两个数据集,而不是并排绘制。

Here's some exemplifying data:以下是一些示例数据:

df1<-data.frame(X1=1:10,Y11=11:20,Y12=21:30,Y13=31:40)

df2<-data.frame(X2=1.5:10.5,Y21=1.5:10.5)

Let's imagine X1 is a column with values measuring distance in km.假设 X1 是一列,其值以公里为单位测量距离。 Y11 is the vector with values for fuel consumption for Car1, Y12 is the vector with values for fuel consumption for Car2, and so on. Y11 是带有 Car1 油耗值的向量,Y12 是带有 Car2 油耗值的向量,依此类推。 Now X2 is still vector with values measuring distance in km, different from X1, but in the same range.现在 X2 仍然是向量,其值以公里为单位测量距离,与 X1 不同,但在相同的范围内。 Y21 is the fuel consumption for a modified Car1. Y21 是改装 Car1 的油耗。 I want to put them in the same scatter plot, with x-axis being distance(km) and y-axis being fuel consumption我想把它们放在同一个散点图中,x轴是距离(公里),y轴是油耗

This would be much easier to answer if you gave an example data set.如果您提供示例数据集,这将更容易回答。 but here is what you can do (make sure each data.frame has the same column names):但这是您可以做的(确保每个 data.frame 具有相同的列名):

df1 <- data.frame(x = 1:50, y = 1:50)
df2 <- data.frame(x = 100:1, y = 1:100)

df1$cat <- "df1"
df2$cat <- "df2"

df <- rbind(df1, df2)

library(ggplot2)
ggplot(df, aes(x, y, color = cat))+
  geom_point()

and that gives you this:这给了你这个: 在此处输入图片说明

If you want to plot all the data together, then It's best to reshape your data.如果您想将所有数据绘制在一起,那么最好重塑您的数据。 Here's an example using other tidyverse functions这是使用其他 tidyverse 函数的示例

library(tidyr)
library(dplyr)

dd <- bind_rows(
  df1 %>% gather(car, mpg, -X1) %>% rename(X=X1),
  df2 %>% gather(car, mpg, -X2) %>% rename(X=X2)
)

ggplot(dd, aes(X, mpg, color=car)) + geom_point()

It's an old question, but recently I have solved a similar problem using a quicker approach than the ones here这是一个老问题,但最近我使用比这里更快的方法解决了类似的问题

ggplot with 2 y axes on each side and different scales 每边有 2 个 y 轴和不同比例的 ggplot

Maybe you can first scale down one of the datasets, and then try out the "dual y-axes" function in ggplot2, namely,也许您可以先缩小其中一个数据集,然后在 ggplot2 中尝试“双 y 轴”功能,即,

p <– ggplot2(dataframe, ...)+...
p + scale_y_continuous(name, ..., sec.axis = sec_axis(...))

where sec.axis means "the second axis" Please refer to https://www.r-graph-gallery.com/line-chart-dual-Y-axis-ggplot2.html for details.其中sec.axis表示“第二轴”请参阅https://www.r-graph-gallery.com/line-chart-dual-Y-axis-ggplot2.html了解详情。 ?sec_axis in R also helps. R 中的?sec_axis也有帮助。

Thanks to a comment by MrFrick, I think I got it.感谢 MrFrick 的评论,我想我明白了。

ggplot(data=df1) + geom_point(aes(x=X1,y=Y11, 
                          color="Car1"))+
  geom_point(data=df2,aes(x=X2,y=Y21),color="ModCar2"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM