R 控件ggplot2图例外观不影响绘图
我使用ggplot2绘制直线,如下所示:R 控件ggplot2图例外观不影响绘图,r,plot,ggplot2,legend,R,Plot,Ggplot2,Legend,我使用ggplot2绘制直线,如下所示: ggplot(iris, aes(Petal.Width,Petal.Length,color=Species)) + geom_line() + theme_bw() 我发现传奇标记很小,所以我希望它们更大。如果更改大小,绘图上的线条也会更改: ggplot(iris, aes(Petal.Width,Petal.Length,color=Species)) + geom_line(size=4) + theme_bw() 但我只想在图例中看到
ggplot(iris, aes(Petal.Width,Petal.Length,color=Species)) + geom_line() + theme_bw()
我发现传奇标记很小,所以我希望它们更大。如果更改大小,绘图上的线条也会更改:
ggplot(iris, aes(Petal.Width,Petal.Length,color=Species)) + geom_line(size=4) + theme_bw()
但我只想在图例中看到粗线条,我希望情节上的线条变细。我尝试使用legend.key.size
但它改变了标记的平方,而不是线条的宽度:
library(grid) # for unit
ggplot(iris,aes(Petal.Width,Petal.Length,color=Species))+geom_line()+theme_bw() + theme(legend.key.size=unit(1,"cm"))
我还尝试使用以下要点:
ggplot(iris,aes(Petal.Width,Petal.Length,color=Species)) + geom_line() + geom_point(size=4) + theme_bw()
当然,它仍然会影响情节和传说:
我想用线来画,用点来画图例
所以我想问两件事:
要仅更改图例中的线宽,应使用函数
guides()
,然后对于color=
使用guides\u legend()
和override.aes=
并设置size=
。这将覆盖绘图中使用的大小,并仅为图例使用新的大小值
ggplot(iris,aes(Petal.Width,Petal.Length,color=Species))+geom_line()+theme_bw()+
guides(colour = guide_legend(override.aes = list(size=3)))
要在图例中获取点和绘图中的线,解决方法是添加geom_点(大小=0)
以确保点不可见,然后在guides()中设置linetype=0
以删除线和size=3
以获取更大的点
ggplot(iris,aes(Petal.Width,Petal.Length,color=Species))+geom_line()+theme_bw()+
geom_point(size=0)+
guides(colour = guide_legend(override.aes = list(size=3,linetype=0)))
太棒了,谢谢!这就是我需要的!有机会回答我的第二个问题吗?这可以设定一个主题吗?