我正在使用以下代码。
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"))
这会导致:
我想旋转图例以获得所需的结果:
有人可以帮忙吗?谢谢。
最佳答案
好吧,我根据@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/