多个图例理由

Multiple legend justification

我试图在 ggplot 中证明多个图例的合理性,但没有任何真正的成功。在绘图区域(灰色区域)之外显示图例时,对齐是正确的。但是,当在绘图区域内显示图例时,图例居中(我想让它们左对齐)。我试过 this thread 但还是不能正常使用。

我的例子:

library(ggplot2)

ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), size = qsec)) +
  geom_point(aes(colour = factor(cyl), size = qsec)) +
  theme(legend.justification = c(1,0),
        legend.position = c(1,0),
        legend.margin = unit(0,"lines"),
        legend.box = "vertical",
        legend.key.size = unit(1,"lines"),
        legend.text.align = 0,
        legend.title.align = 0)

我们需要将 legend.box.just = "left" 添加到您现有的 theme()

ggplot(mtcars, aes(wt, mpg)) +
  geom_point(aes(colour = factor(cyl), size = qsec)) +
  geom_point(aes(colour = factor(cyl), size = qsec)) +
  theme(legend.box.just = "left",
        legend.justification = c(1,0),
        legend.position = c(1,0),
        legend.margin = unit(0,"lines"),
        legend.box = "vertical",
        legend.key.size = unit(1,"lines"),
        legend.text.align = 0,
        legend.title.align = 0)

你可以试试这个:

library(ggplot2)
data("mtcars")
g <- ggplot(mtcars, aes(wt, mpg))
g <- g + geom_point(aes(colour = factor(cyl), size = qsec))
g <- g + geom_point(aes(colour = factor(cyl), size = qsec))
g <- g + theme(legend.justification=c(0,0), legend.position=c(0,0))

对于其他职位,您可以从该文档中尝试http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/

也许你会在上面找到更好的解释link。