控制ggplot2图例而不影响绘图 [英] Control ggplot2 legend look without affecting the plot
问题描述
ggplot(虹膜,aes(Petal.Width,Petal.Length ,color = Species))+ geom_line()+ theme_bw()
。
我发现传说记号很小,所以我希望它们更大。如果我改变尺寸,情节上的线条也会改变:
ggplot(虹膜,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))
我也尝试过使用分数:
$ p $ g $ p $ ggplot(虹膜,aes(Petal.Width,Petal.Length,color = Species))+ geom_line()+ geom_point(size = 4) + theme_bw()
当然,它仍然影响剧情和图例:
I我想问两件事情:
$ b
b
- 如何在不更改图表的情况下更改图例中的线宽?
- 如何在绘图,但在图例中绘制点/点/方块?
只有在图例中,您应该使用函数 guides()
,然后为 color =
使用 guide_legend ()
与 override.aes =
并设置 size =
。这将覆盖plot中使用的大小,并将仅为图例使用新的大小值。
ggplot(虹膜,aes(Petal.Width ,Petal.Length,color = Species))+ geom_line()+ theme_bw()+
guides(color = guide_legend(override.aes = list(size = 3)))
为了得到图例中的点和线条,应该添加 geom_point(size = 0)
来确保点是不可见的,然后在 guides()
设置 linetype = 0
来移除行和 size = 3
以获得更大的分数。
ggplot(虹膜,aes(Petal.Width,Petal.Length,color = Species))+ geom_line()+ theme_bw( )+
geom_point(size = 0)+
guides(color = guide_legend(override.aes = list(size = 3,linetype = 0)))
p>
I'm plotting lines with ggplot2 like this:
ggplot(iris, aes(Petal.Width,Petal.Length,color=Species)) + geom_line() + theme_bw()
.
I find legend marks to be small so I want them to be bigger. If I change the size, lines on the plot change too:
ggplot(iris, aes(Petal.Width,Petal.Length,color=Species)) + geom_line(size=4) + theme_bw()
.
But I only want to see thick lines in the legend, I want lines on the plot to be thin. I tried to use legend.key.size
but it changes the square of the mark, not the width of the line:
library(grid) # for unit
ggplot(iris,aes(Petal.Width,Petal.Length,color=Species))+geom_line()+theme_bw() + theme(legend.key.size=unit(1,"cm"))
I also tried to use points:
ggplot(iris,aes(Petal.Width,Petal.Length,color=Species)) + geom_line() + geom_point(size=4) + theme_bw()
But of course it still affects both plot and legend:
I wanted to use lines for the plot and dots/points for the legend.
So I'm asking about two things:
- How to change width of line in the legend without changing the plot?
- How to draw lines in the plot, but draw points/dots/squares in the legend?
To change line width only in the legend you should use function guides()
and then for colour=
use guide_legend()
with override.aes=
and set size=
. This will override size used in plot and will use new size value just for legend.
ggplot(iris,aes(Petal.Width,Petal.Length,color=Species))+geom_line()+theme_bw()+
guides(colour = guide_legend(override.aes = list(size=3)))
To get points in legend and lines in plot workaround would be add geom_point(size=0)
to ensure that points are invisible and then in guides()
set linetype=0
to remove lines and size=3
to get larger points.
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)))
这篇关于控制ggplot2图例而不影响绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!