R:在ggplot boxplot中裁剪图例会留下两个单独的图例

R:在ggplot boxplot中裁剪图例会留下两个单独的图例,r,ggplot2,legend,boxplot,R,Ggplot2,Legend,Boxplot,我使用ggplot2软件包创建了一个刻面盒形图。R代码如下: version.labs <- c(`1`="Version 1.0", `2`="Version 2.0", `3`="Version 3.0", `4`="Version 4.0", `5`="Version 5.0") ggplot(df, aes(x=factor(Subsystem), y=Risk.value, fill=factor(Version)) ) + geom_jitter(position=pos

我使用ggplot2软件包创建了一个刻面盒形图。R代码如下:

version.labs <- c(`1`="Version 1.0", `2`="Version 2.0", `3`="Version 3.0", `4`="Version 4.0", `5`="Version 5.0")
ggplot(df, aes(x=factor(Subsystem), y=Risk.value, fill=factor(Version)) ) + 
  geom_jitter(position=position_jitter(width=0.3, height=0.2), aes(colour=factor(Version)), alpha=0.9) +
  geom_boxplot(alpha = 0.5, show.legend = FALSE) + facet_grid(.~Version, labeller = as_labeller(version.labs)) +
  theme(strip.text.x = element_text(size=9, color="black", face="bold"))
ggplot(df, aes(x=factor(Subsystem), y=Risk.value) ) + 
  geom_jitter(position=position_jitter(width=0.3, height=0.2), aes(colour=factor(Version)), alpha=0.9) +
  geom_boxplot(alpha = 0.5, show.legend = FALSE, aes(fill=factor(Version))) + facet_grid(.~Version, labeller = as_labeller(version.labs)) +
  theme(strip.text.x = element_text(size=9, color="black", face="bold")) +
  labs(x="Risk distribution per software version and subsystem type", y="Normalized risk value") + 
  guides(color=guide_legend("Version"))
这将生成一个类似于此的箱线图


可以看出有两个传说。所以,接近但没有雪茄。对于如何解决此问题的任何提示,我们将不胜感激。

您正在使用
fill
参数对图例进行分组和生成。可以使用
scale\u color\u manual
代替
scale\u fill\u manual
覆盖现有图例我解决了问题。就是因为我把我的aestetic fill函数
aes()
放在了general
ggplot()
中。这应该放在
geom\u boxplot()
中,否则常规
ggplot()
以及
geom\u boxplot()
都会添加图例。因此,我修复了这个问题,然后使用
guides()
更新
geom\u boxplot()
图例中的标题。完整代码如下所示:

version.labs <- c(`1`="Version 1.0", `2`="Version 2.0", `3`="Version 3.0", `4`="Version 4.0", `5`="Version 5.0")
ggplot(df, aes(x=factor(Subsystem), y=Risk.value, fill=factor(Version)) ) + 
  geom_jitter(position=position_jitter(width=0.3, height=0.2), aes(colour=factor(Version)), alpha=0.9) +
  geom_boxplot(alpha = 0.5, show.legend = FALSE) + facet_grid(.~Version, labeller = as_labeller(version.labs)) +
  theme(strip.text.x = element_text(size=9, color="black", face="bold"))
ggplot(df, aes(x=factor(Subsystem), y=Risk.value) ) + 
  geom_jitter(position=position_jitter(width=0.3, height=0.2), aes(colour=factor(Version)), alpha=0.9) +
  geom_boxplot(alpha = 0.5, show.legend = FALSE, aes(fill=factor(Version))) + facet_grid(.~Version, labeller = as_labeller(version.labs)) +
  theme(strip.text.x = element_text(size=9, color="black", face="bold")) +
  labs(x="Risk distribution per software version and subsystem type", y="Normalized risk value") + 
  guides(color=guide_legend("Version"))
最后的情节是这样的,我很满意


您的示例不可复制。但是我想你需要一本
缩放颜色手册
缩放填充手册
。谢谢你的评论。我也试过了,但没有解决问题。然而,我用另一个解决方案解决了这个问题,正如在另一个答案中所解释的。谢谢你的建议。我尝试使用以下命令:
scale\u fill\u manual(name=“Version”,labels=c(“v1.0”、“v2.0”、“v3.0”、“v4.0”、“v5.0”),values=c(“灰色”、“蓝色”、“绿色”、“红色”、“黑色”)
,但结果与第二个方框图几乎相同,即两个图例:-/