我有两个具有相同 x 轴的图 - 在它们中 x 的范围都是 0-5。
我想将它们组合成一个图表,但我没有找到以前的例子。
这是我得到的:
c <- ggplot(survey, aes(often_post,often_privacy)) + stat_smooth(method="loess")
c <- ggplot(survey, aes(frequent_read,often_privacy)) + stat_smooth(method="loess")
我怎样才能把它们结合起来?
y 轴是“经常隐私”,在每个图中,x 轴是“经常发布”或“经常阅读”。
我想我可以很容易地(以某种方式)组合它们,因为它们的范围都是 0-5。
非常感谢!
最佳答案
Ben 解决方案的示例代码。
#Sample data
survey <- data.frame(
often_post = runif(10, 0, 5),
frequent_read = 5 * rbeta(10, 1, 1),
often_privacy = sample(10, replace = TRUE)
)
#Reshape the data frame
survey2 <- melt(survey, measure.vars = c("often_post", "frequent_read"))
#Plot using colour as an aesthetic to distinguish lines
(p <- ggplot(survey2, aes(value, often_privacy, colour = variable)) +
geom_point() +
geom_smooth()
)
关于r - 使用 ggplot2 绘制两个变量 - 相同的 x 轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11242420/