ggplot 结合来自不同 data.frames 的两个图 [英] ggplot combining two plots from different data.frames

查看:23
本文介绍了ggplot 结合来自不同 data.frames 的两个图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将来自两个不同 data.frames 的两个 ggplots 合并到一个图中.您将在下面找到代码.我想合并情节 1&2 或情节 3&4.

I want to combine two ggplots, from two different data.frames, into one plot. Below you will find the code. I want to combine plot 1&2 or plots 3&4.

df1 <- data.frame(p=c(10,8,7,3,2,6,7,8),
             v=c(100,300,150,400,450,250,150,400))
df2 <- data.frame(p=c(10,8,6,4), v=c(150,250,350,400))

plot1 <- qplot(df1$v, df1$p)
plot2 <- qplot(df2$v, df2$p, geom="step")

plot3 <- ggplot(df1, aes(v, p)) + geom_point()
plot4 <- ggplot(df2, aes(v, p)) + geom_step()

这一定很容易做到,但不知何故我无法让它工作.感谢您抽出宝贵时间.

This must be very easy to do, but somehow I can't get it to work. Thanks for your time.

推荐答案

正如 Baptiste 所说,您需要在 geom 级别指定 data 参数.要么

As Baptiste said, you need to specify the data argument at the geom level. Either

#df1 is the default dataset for all geoms
(plot1 <- ggplot(df1, aes(v, p)) + 
    geom_point() +
    geom_step(data = df2)
)

#No default; data explicitly specified for each geom
(plot2 <- ggplot(NULL, aes(v, p)) + 
      geom_point(data = df1) +
      geom_step(data = df2)
)

这篇关于ggplot 结合来自不同 data.frames 的两个图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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