简体   繁体   English

如何在ggplot2中的同一图上绘制线和点?

[英]How to draw lines and points on same plot in ggplot2?

I would like to include both the points from the data frame q and the smoothing function of q, as well as the smoothing function from df1. 我想同时包含数据帧q中的点和q的平滑函数,以及df1的平滑函数。 The plot only returns the points of q. 该图仅返回q的点。 Is there a way to do this? 有没有办法做到这一点? Thanks. 谢谢。

df1 <- data.frame(Rate=rnorm(10, 500, 100), Damage=rnorm(10, 50, 15))
q <- data.frame(R=rnorm(20, 550, 50), V=rnorm(20, 35, 10))

ggplot(df1,aes(x=Rate,y=Damage)) +
#geom_polygon(data=ci,aes(x=x,y=y),fill='gray80') +
geom_point(data=q,aes(x=R,y=V)) +
geom_smooth(aes(col = "GPs"), method="auto",se=FALSE) +
geom_smooth(data=q, mapping=aes(x=R, y=V, col="observed"), 
          method="auto",se=FALSE) +
coord_cartesian(xlim=c(0,1000), ylim=c(0, 100)) +
theme_bw() +
scale_y_continuous(breaks=seq(0, 100, 20),) +
labs(subtitle="PPS post-emergence", 
   x = "Rate (mg/Ha)",
   y = NULL) +
scale_color_manual("My legend", values=c("Predicted (GPs)" = "steelblue", 
                                       "Observed (average)" = "firebrick"))

Your color mapping does not make sense. 您的颜色映射没有意义。 You have color mapped to 'GPs' and 'observed'. 您已将颜色映射到“ GP”和“已观察”。 But the values in your color scale do not match these. 但是色标中的值与这些值不匹配。 You can make it work using 您可以使用

ggplot(df1,aes(x=Rate,y=Damage)) +
  geom_point(data=q,aes(x=R,y=V)) +
  geom_smooth(aes(col = "GPs"), method="auto",se=FALSE) +
  geom_smooth(data=q, mapping=aes(x=R, y=V, col="observed"), 
              method="auto",se=FALSE) +
  coord_cartesian(xlim=c(0,1000), ylim=c(0, 100)) +
  theme_bw() +
  scale_y_continuous(breaks=seq(0, 100, 20),) +
  labs(subtitle="PPS post-emergence",
       x = "Rate (mg/Ha)",
       y = NULL) +
  scale_color_manual("My legend", values=c("GPs" = "steelblue",
                                           "observed" = "firebrick"))

在此处输入图片说明

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

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