r-在ggplot2中,是否有一种相对简单的方法可以为数据中的不同组使用不同的地理信息



我有一组包含多个组的数据。我想把它们画在同一张图上,但比如说,一组用一条平滑的线,另一组用数据点。或者两者都有平滑的线条,但只有其中一个有数据点。一个例子:

library(reshape)
library(ggplot2)
set.seed(123)
x <- 1:1000
y <- 5 + rnorm(1000)
z <- 5 + 0.005*x + rnorm(1000)
df <- as.data.frame(cbind(x,y,z))
df <- melt(df,id=c("x"))
ggplot(df,aes(x=x,y=value,color=variable)) +
geom_point() + #here I want only the y variable graphed
geom_smooth() #here I want only the z variable graphed

它们都是相对于x变量绘制的,并且在相同的尺度上。有没有相对简单的方法来实现这一点?

使用每个绘图类型上的过滤数据设置数据参数

library(ggplot2)
library(reshape)
set.seed(123)
x <- 1:1000
y <- 5 + rnorm(1000)
z <- 5 + 0.005*x + rnorm(1000)
df <- as.data.frame(cbind(x,y,z))
df <- reshape::melt(df,id=c("x"))
df
ggplot(df,aes(x=x,y=value,color=variable)) +
geom_point(data=df[df$variable=="y",]) + #here I want only the y variable graphed
geom_smooth(data=df[df$variable=="z",]) #here I want only the z variable graphed

最新更新