如何使geom_abline show_guide线条类型正确显示在图例中 [英] How to make geom_abline show_guide line types show up in legend correctly
问题描述
set.seed(586)
data <-data.frame(x = sort(runif(20)),y = sort((rnorm(1)* 1 :数据,aes(x = x,y = y:20 * .234)),grp =因子(样本(c(0,1),20,replace = T)))
ggplot ,shape = grp))+
geom_point()+
theme_classic()+
scale_shape_manual(PT Status,
values = c(1,3),
break = c(0,1),
labels = c(No,Yes))+
scale_x_continuous(My x)+
scale_y_continuous(My y )+
geom_abline(截距= .12,斜率= .98,线型= 1,show_guide = TRUE)+
geom_abline(截距= .05,斜率=( - 。3 + .98),线型= 3,show_guide = TRUE)+
theme(legend.position =bottom)
线条看起来很正确,我喜欢这些线条如何融入到图例中。但显然在我的代码中,我没有指定哪些数据转到哪个abline。有没有一种方法可以简单地在 geom_abline
代码中指明这一点,或者以其他方式对其进行重新编码,以使第一个实体为实体,第二个实体为虚线传说中的术语?
使用我创建的@Jorans建议
smDf <-data.frame(截距= c(.12,.05),斜率= c(.98,( - 3 + .98)),线型=因子(c(1,3)))
然后新的代码是:
ggplot(data,aes(x = x,y = y,shape = grp))+
geom_point()+
theme_classic()+
scale_shape_manual状态,
values = c(1,3),
breaks = c(0,1),
labels = c(No,Yes))+
scale_x_continuous(My x)+
scale_y_continuous(My y)+
geom_abline(aes(截距=截距,斜率=斜率,线型=线型),data = smDf,show_guide = TRUE)
这给了我:
那现在我该如何整合这两个?
想想,但你基本上需要给予相同基本结构的ggplot手动缩放比例以供合并:
ggplot()+
geom_point(data = data,aes(x = x,y = y,shape = grp))+
geom_abline(aes(intercept = intercept,slope = slope,linetype = linetype),data = smDf ,show_guide = TRUE)+
theme_classic()+
scale_shape_manual(name =PT Status,
values = c(1,3),
breaks = c(0, 1),
labels = c(No,Yes))+
scale_linetype_manual(name =PT Status,
values = c(1,3),
标签= c(否,是))+
scale_x_continuous(我的x)+
scale_y_co ntinuous(我的y)
set.seed(586)
data<-data.frame(x=sort(runif(20)),y=sort((rnorm(1)*1:20*.234)),grp=factor(sample(c(0,1),20,replace = T)))
ggplot(data, aes(x=x, y=y, shape=grp)) +
geom_point() +
theme_classic() +
scale_shape_manual("PT Status",
values=c(1,3),
breaks=c(0,1),
labels=c("No","Yes"))+
scale_x_continuous("My x") +
scale_y_continuous("My y")+
geom_abline(intercept=.12, slope=.98,linetype=1,show_guide = TRUE)+
geom_abline(intercept=.05, slope=(-.3+.98),linetype=3,show_guide = TRUE)+
theme(legend.position="bottom")
Lines look right, and I like how the lines are integrated into the legend. But obviously in my code I'm not specifying what data go to which abline. Is there a way to simply indicate this in the geom_abline
code, or some other way to recode it to make it so that the first one is solid and the second is dashed, in terms of the legend?
Using @Jorans suggestion I created
smDf<-data.frame(intercept=c(.12,.05),slope=c(.98, (-.3+.98)),linetype=factor(c(1,3)))
Then the new code is:
ggplot(data, aes(x=x, y=y, shape=grp)) +
geom_point() +
theme_classic() +
scale_shape_manual("PT Status",
values=c(1,3),
breaks=c(0,1),
labels=c("No","Yes"))+
scale_x_continuous("My x") +
scale_y_continuous("My y")+
geom_abline(aes(intercept=intercept, slope=slope, linetype=linetype), data=smDf, show_guide = TRUE)
Which gives me:
So now how do I integrate these two?
This could be better documented, I think, but you basically need to give ggplot manual scales of the same basic structure for them to be merged:
ggplot() +
geom_point(data = data,aes(x=x, y=y, shape=grp)) +
geom_abline(aes(intercept=intercept, slope=slope, linetype=linetype), data=smDf,show_guide = TRUE) +
theme_classic() +
scale_shape_manual(name = "PT Status",
values=c(1,3),
breaks=c(0,1),
labels=c("No","Yes"))+
scale_linetype_manual(name = "PT Status",
values=c(1,3),
labels=c("No","Yes"))+
scale_x_continuous("My x") +
scale_y_continuous("My y")
这篇关于如何使geom_abline show_guide线条类型正确显示在图例中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!