【问题标题】:R ggplot2: Change the spacing between the legend and the panelR ggplot2:更改图例和面板之间的间距
【发布时间】:2016-12-28 21:21:28
【问题描述】:

如何更改ggplot2 2.2.0中的图例区域和面板之间的间距?

library(ggplot2)
library(dplyr)
library(tidyr)

dfr <- data.frame(x=factor(1:20),y1=runif(n=20)) %>%
        mutate(y2=1-y1) %>%
        gather(variable,value,-x)


ggplot(dfr,aes(x=x,y=value,fill=variable))+
  geom_bar(stat="identity")+
  theme(legend.position="top",
        legend.justification="right")

更改 legend.marginlegend.box.margin 似乎没有任何作用。

ggplot(dfr,aes(x=x,y=value,fill=variable))+
  geom_bar(stat="identity")+
  theme(legend.position="top",
        legend.justification="right",
        legend.margin=margin(0,0,0,0),
        legend.box.margin=margin(0,0,0,0))

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    实际上,我认为您提到的选项会起作用。它们似乎对我有用;也许您没有输入适当的值。

    看看这两个,看看我在说什么:

    ggplot(dfr,aes(x=x,y=value,fill=variable))+
      geom_bar(stat="identity")+
      theme(legend.position="top",
            legend.justification="right",
            legend.margin=margin(0,0,0,0),
            legend.box.margin=margin(-10,-10,-10,-10))
    

    ggplot(dfr,aes(x=x,y=value,fill=variable))+
      geom_bar(stat="identity")+
      theme(legend.position="top",
            legend.justification="right",
            legend.margin=margin(0,0,0,0),
            legend.box.margin=margin(10,10,10,10))
    

    【讨论】:

    • 啊,是的!它确实有效。谢谢。价值观发生了很大变化。
    • 这不是很奇怪吗? 0 的边距应该已经使它非常接近图表?这是否意味着我们通过设置负边距来重叠另一个边距元素?
    猜你喜欢
    • 2019-11-17
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多