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

时间:2018-12-20 15:35:02

标签: r ggplot2 plot

我想同时包含数据帧q的点和q的平滑函数,以及df1的平滑函数。该图仅返回q的点。有没有办法做到这一点?谢谢。

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"))

1 个答案:

答案 0 :(得分:1)

您的颜色映射没有意义。您已将颜色映射到“ GP”和“已观察”。但是色标中的值与这些值不匹配。您可以使用

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"))

enter image description here