我从数据的一列中按组创建了一个GGally图,但是没有图例来标识组名。如何在图例中添加图例?
我的代码:
ggpairs(data[ ,c(2,5)], mapping = ggplot2::aes(color = group),
upper = list(continuous = wrap("cor", size = 3)),
lower = list(combo = wrap(ggally_facethist, bins = 10)),
diag = list(continuous = wrap("densityDiag"),
mapping = ggplot2::aes(fill = group, alpha = 0.1)),
title="Data by Group") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(text = element_text(size = 10),
axis.text.x = element_text(angle = 90, hjust = 1))
我的输出: 理想情况下,如果需要,我希望能够移动图例,但是右侧的图例就可以了。我也想知道是否可以创建一个单独的图例并将其保存到对象中。
答案 0 :(得分:0)
尝试一下:
gg <- ggpairs(data[ ,c(2,5)], mapping = ggplot2::aes(color = group),
upper = list(continuous = wrap("cor", size = 3)),
lower = list(combo = wrap(ggally_facethist, bins = 10)),
diag = list(continuous = wrap("densityDiag"),
mapping = ggplot2::aes(fill = group, alpha = 0.1)),
title="Data by Group") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(text = element_text(size = 10),
axis.text.x = element_text(angle = 90, hjust = 1))
然后添加
gg + theme(legend.position = "bottom")
或您想要的任何职位
可以通过以下说明添加外部图例:How to add an external legend to ggpairs()?