ggplot结合来自不同data.frames的两张图 [英] ggplot combining two plots from different data.frames
本文介绍了ggplot结合来自不同data.frames的两张图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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()
这一定很容易做到,但不知何故我无法得到它上班。感谢您的时间。正如Baptiste所说,您需要在geom级别指定数据参数。
解决方案 无论是
#df1是所有几何体的默认数据集
(plot1 < - ggplot(df1,aes(v ,p))+
geom_point()+
geom_step(data = df2)
)
或
#无默认值;为每个geom
明确指定的数据(plot2 <-ggplot(NULL,aes(v,p))+
geom_point(data = df1)+
geom_step(data = df2)
)
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.
解决方案
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)
)
or
#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屋!
查看全文