考虑以下数据
df<-data.frame( c(63.5, 64, 64.5, 65, 65.5, 66, 66.5), c(0.8,0.8,0.5,0.5,0.5,0,0))
colnames(df)<-c("age", "labor_force_participation")
df$pensionbreak<-cut(df$age,
breaks = c(-Inf, 64.4,65.5,Inf),
labels = c("prior pension", "transition area", "after pension"))
p <- ggplot(df, aes(age, labor_force_participation, colour=pensionbreak))
p + geom_point() +
geom_smooth(method = "lm", se = TRUE) +
xlab("age") +
ylab("fraction of males working") +
labs(color = "Retirement") +
theme_bw()上面的数据和图表显示了在领取养老金福利之前和之后(大约65岁)的劳动力参与率。正如你所看到的,图中有三条线,即1)之前的养老金,2)过渡区,以及3)养老金之后。过渡区在那里,因为并不是每个人都能在65岁时获得确切的养老金福利(一些人早一点领取,另一些人晚一点领取)。
现在我想保留之前的养老金和养老金之后的线,但我只想保留过渡区的数据点。换句话说,我想保留红线(之前的养老金)和蓝线(养老金之后),我只想1)保留过渡区域中的点,但2)在那里没有线。谁能给我解释一下我该如何修改我的代码才能得到这个。我很感谢你的帮助。
发布于 2020-03-23 23:26:46
您可以为每个层提供不同的数据子集:
p +
geom_point(data = subset(df, pensionbreak == "transition area")) +
geom_smooth(
data = subset(df, pensionbreak != "transition area"),
method = "lm", se = TRUE
) +
xlab("age") +
ylab("fraction of males working") +
labs(color = "Retirement") +
theme_bw()

如果要指定哪种颜色是哪种颜色,请使用scale_color_manual。
https://stackoverflow.com/questions/60816391
复制相似问题