繁体   English   中英

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

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

嗨,我正在尝试将我的图例区域添加到我的绘图区域,而我的密钥周围没有白色边框。 到目前为止,我有这个:

绘图区域中的图例但存在白色边框:

传奇1

我想要的传奇风格:

传奇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"))

您将需要使用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())

在此处输入图片说明

我更喜欢使用element_blank()删除主题元素,而不是将其设置为透明并将其留在那里。 我发现根据图形设备的不同,透明可以有不同的解释。

暂无
暂无

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

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