0

我已经从我的数据的一列中按组创建了一个 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))

我的输出: 在此处输入图像描述 理想情况下,如果有必要,我希望能够移动图例,但右侧的图例会很好。我还想知道是否可以创建一个单独的图例并将其保存到一个对象中。

4

1 回答 1

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

或者你想要的任何位置

可以通过以下说明添加外部图例:如何将外部图例添加到 ggpairs()?

于 2019-06-15T16:24:13.407 回答