r - ggplot 垂直对齐图例

标签 r ggplot2

我已经看了一段时间了。我的图例用完了图表区域。有没有办法垂直证明 ggplot2 中的图例?

library(ggplot2)
df <- data.frame(x = 1:30, y = 1:30, color = letters[1:30])
ggplot(df, aes(x, y)) +
    geom_point(aes(colour = color)) +
    guides(col = guide_legend(nrow = 30))

最佳答案

玩弄legend.direction (“垂直”/“水平”)应该工作:

library(ggplot2)
df <- data.frame(x = 1:30, y = 1:30, color = letters[1:30])
ggplot(df, aes(x, y)) +
    geom_point(aes(colour = color)) +
    theme(legend.direction='horizontal')

您可能还想将它与 legend.box 结合使用(“水平垂直”)。

如果要控制图例的绝对位置,请添加:
theme(legend.direction = "vertical",
legend.box = "horizontal",
legend.position = c(0.025,0.975),
legend.justification = c(0, 1))

例如,将图例放置在图形的左上角内。

关于r - ggplot 垂直对齐图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12204179/

相关文章:

r - 有没有办法在 Shiny 的情况下调用关闭服务器进程的函数?

r - 将 Tukey 的重要性字母添加到箱线图中

r - 在 ggplot2 中对转换后的数据标记重新缩放的值

r - 如何向 ggridges 中的脊线图添加垂直颜色渐变?

r - 从 R 中的列表导出单独的文本文件

regex - 替换 R 中第一次出现的 ":",但不替换第二次出现的

r - 在带有分类变量的 r 输出中使用 lm() 的多元线性回归不完整?

r - 将功能区添加到 R 中的线图

r - 在 R 中,绘制连续/序列事件

在 R 中重现 "Computer composition with lines"