使用ggplot2绘制两个变量 - 相同的x轴 [英] Plotting two variables using ggplot2 - same x axis

查看:426
本文介绍了使用ggplot2绘制两个变量 - 相同的x轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个具有相同x轴的图 - 两个x的范围均为0-5。
我想将它们都结合到一个图中,但我没有找到前面的例子。
这里是我得到的:

  c < -  ggplot(survey,aes(often_post,often_privacy))+ stat_smooth(method =loess)
c < - ggplot(aes(frequent_read,often_privacy))+ stat_smooth(method =loess)

我怎样才能将它们结合起来?
y轴是通常隐私,在每个图中x轴是经常发布或频繁阅读。
我认为我可以轻松地将它们组合起来(不知何故),因为它们的范围都是0-5。



非常感谢!

解决方案

您可以使用 + 来合并同一个 ggplot 对象上的其他图。例如,绘制两对列的点和平滑线:

  ggplot(survey,aes(often_post,frequently_privacy)) + 
geom_point()+
geom_smooth()+
geom_point(aes(frequent_read,often_privacy))+
geom_smooth(aes(frequent_read,often_privacy))


I have two graphs with the same x axis - the range of x is 0-5 in both of them. I would like to combine both of them to one graph and I didn't find a previous example. Here is what I got:

c <- ggplot(survey, aes(often_post,often_privacy)) + stat_smooth(method="loess")
c <- ggplot(survey, aes(frequent_read,often_privacy)) + stat_smooth(method="loess")

How can I combine them? The y axis is "often privacy" and in each graph the x axis is "often post" or "frequent read". I thought I can combine them easily (somehow) because the range is 0-5 in both of them.

Many thanks!

解决方案

You can use + to combine other plots on the same ggplot object. For example, to plot points and smoothed lines for both pairs of columns:

ggplot(survey, aes(often_post,often_privacy)) + 
geom_point() +
geom_smooth() + 
geom_point(aes(frequent_read,often_privacy)) + 
geom_smooth(aes(frequent_read,often_privacy))

这篇关于使用ggplot2绘制两个变量 - 相同的x轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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