R ggplot2:更改图例和面板之间的间距

R ggplot2:更改图例和面板之间的间距

问题描述:

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

enter image description here

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)) 

其实,我认为你提到的选项是可行的。他们似乎为我工作;也许你没有输入适当的值。

看一看这两个,看看我说的是:

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)) 

enter image description here

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)) 

enter image description here

+1

啊,是的!它确实有效。谢谢。值已经改变了很多。 – rmf