gpt4 book ai didi

R ggplot2 : Change the spacing between the legend and the panel

转载 作者:行者123 更新时间:2023-12-03 22:29:40 25 4
gpt4 key购买 nike

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

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

关于R ggplot2 : Change the spacing between the legend and the panel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41362895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com