简体   繁体   English

ggplot2 将图例移动到绘图区域,键周围没有边框

[英]ggplot2 moving legend in to plot area without borders around key

Hi im trying to add my legend area to my plot area without the white borders around my key.嗨,我正在尝试将我的图例区域添加到我的绘图区域,而我的密钥周围没有白色边框。 So far i have this:到目前为止,我有这个:

Legend in plot area but white border present:绘图区域中的图例但存在白色边框:

传奇1

Legend style i want:我想要的传奇风格:

传奇2

ggplot(data=Pig, aes(x=breed, y= p1_plus_p3_fat_depth_mm, color=sex))+
geom_boxplot()+
theme(legend.title=element_blank()) +
theme(panel.grid.major = element_blank()) +
theme(panel.grid.minor = element_blank())+
xlab("Pig Breed") +
ylab("The P1 and P3 Fat Depth in mm")+
scale_colour_discrete(name  ="Sex",breaks=c("B", "S"),labels=c("Boar", "Sow"))+
theme(legend.justification = c(1, 1), legend.position = c(1, 1))+
theme(legend.background = element_rect(fill="transparent"))

You will need to use legend.key = element_blank() to drop the background and borders around the boxplot icons.您将需要使用legend.key = element_blank()删除箱线图图标周围的背景和边框。

ggplot(data=iris[c("Species", "Sepal.Length")], aes(x=Species, y= Sepal.Length, color=Species))+
  geom_boxplot()+
  theme(legend.title=element_blank()) +
  theme(panel.grid.major = element_blank()) +
  theme(panel.grid.minor = element_blank())+
  theme(legend.justification = c(1, 1), legend.position = c(1, 1))+
  theme(legend.background = element_blank(),
        legend.key = element_blank())

在此处输入图片说明

I prefer using element_blank() which will remove a theme element as opposed to setting it to transparent and leaving it there.我更喜欢使用element_blank()删除主题元素,而不是将其设置为透明并将其留在那里。 I've found depending on the graphics device the transparent can be interpreted differently.我发现根据图形设备的不同,透明可以有不同的解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM