我有以下玩具示例:
library(ggplot2)
g <- ggplot(mpg, aes(class))
mpg$drv <- sample(as.character((-4:4)), size = length(mpg$drv), replace = T)
g + geom_bar(aes(fill = drv), position = "fill") + theme(legend.position = "bottom") + guides(colour = guide_legend(nrow = 1))
对我来说
生成下图。
尽管我可能会尝试,但我无法将底部的图例放在一行中。我环顾四周,guide_legend
似乎没有任何效果。有没有更好的方法来做到这一点,或者有没有另一种方法来强制图例排成一行?
发布于 2018-01-05 09:46:12
您在guides
中指定了colour
而不是fill
。试着这样做:
library(ggplot2)
mpg$drv <- sample(as.character((-4:4)), size = length(mpg$drv), replace = T)
g <- ggplot(mpg, aes(class))
g + geom_bar(aes(fill = drv), position = "fill") + theme(legend.position = "bottom") + guides(fill = guide_legend(nrow = 1))
https://stackoverflow.com/questions/48106036
复制相似问题