旋转箱线图图例(R,ggplot2)

标签 r ggplot2 legend

我正在使用以下代码。

 library(ggplot2)
 mtcars$carb <- as.factor(mtcars$carb)
 mtcars$am <- as.factor(mtcars$am)
 mtcars <- mtcars[!(mtcars$carb==6 | mtcars$carb==8),]
 ggplot(mtcars) + 
    geom_boxplot(aes(x = carb, y = mpg, fill = am), 
                 position = position_dodge(0.9)) +
 guides(fill = guide_legend(direction = "horizontal"))

这会导致:

enter image description here

我想旋转图例以获得所需的结果:

enter image description here

有人可以帮忙吗?谢谢。

最佳答案

好吧,我根据@LFischer 的建议想出了办法。可能有一种更简单的方法,但经过一番尝试和错误后,这对我来说是这样:

 library(ggplot2)
 mtcars$carb <- as.factor(mtcars$carb)
 mtcars$am <- as.factor(mtcars$am)
 mtcars <- mtcars[!(mtcars$carb==6 | mtcars$carb==8),]
 ggplot(mtcars) + 
    geom_boxplot(aes(x = carb, y = mpg, fill = am), 
                 position = position_dodge(0.9)) +
 guides(fill = guide_legend(reverse = TRUE, direction = "vertical", label.position = "top", label.theme = element_text(angle = 90, vjust = 0.5), title.position = "bottom", title.theme = element_text(angle = 90)))

关于旋转箱线图图例(R,ggplot2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56867739/

相关文章:

r - 在 r 中的 ggplot2 中具有 3 个标准偏差的线图

python-2.7 - Matplotlib:在轴外绘制两个图例使其被图形框截断

r - 如何在R中获取函数调用的行号?

r - 有什么方法可以将调色板添加到尺寸美学中吗?

r - 如何计算序列中的运行次数?

r - 将列添加到 R 列表中的多个数据框

r - 如何在 R 中对日期时间进行子集化并旋转测量列

r - 图例 ggplot 图不显示均值、误差线和彩色矩形

python - 将图例添加到散点图

matlab - 使用 saveas 不保存图例中的修改