我该如何减少两个导轨之间的差距在一个情节。 在下面的例子中,两个导游是从颜色和大小的规模,我想改变两个这样的差距,标题“尺寸”是对的下方的图例,点1。设计的角度来看,它可能没有感觉在这个例子中,但我的实际应用中它的作用。
df=data.frame(x=rnorm(100),y=rnorm(100),color=factor(rbinom(100,1,0.5)),size=runif(100))
ggplot(df,aes(x=x,y=y,color=color,size=size)) + geom_point()
编辑:这是阴谋。 我想提出的绿线和箭头突出小的差距。
我试图发挥定制legend
或guide
参数,但我不能找到一个解决方案。 我希望给使用GGPLOT2设置的解决方案。
在此基础上,2个解决方案gtable
和grid
包。
为gtable
方案中,所述代码是从这个激发了问题 。
library(gtable)
# Data transformation
data <- ggplot_build(p)
gtable <- ggplot_gtable(data)
# Determining index of legends table
lbox <- which(sapply(gtable$grobs, paste) == "gtable[guide-box]")
# changing the space between the 2 legends: here -0.5 lines
guide <- gtable$grobs[[lbox]]
gtable$grobs[[lbox]]$heights <- unit.c(guide$heights[1:2],
unit(-.5,'lines'), ## you can the GAP here
guide$heights[4:5])
# Plotting
grid.draw(gtable)
使用类似grid
包(我们在传说中的视口重绘)
pp <- grid.get('guide',grep=T)
depth <- downViewport(pp$wrapvp$name)
guide <- grid.get('guide',grep=T)
grid.rect(gp=gpar(fill='white'))
guide$heights <- unit.c(guide$heights[1:2],unit(-0.2,'lines'),guide$heights[4],unit(0.1,'lines'))
grid.draw(guide)
upViewport(depth)
现在看来使用的主题参数成为可能:
ggplot(df,aes(x=x,y=y,color=color,size=size)) + geom_point() +
theme(legend.spacing.y = unit(-0.5, "cm"))
您也可以尝试降低传说边距:
legend.margin = margin(-0.5,0,0,0, unit="cm")
以上
legend.margin=unit(0, "cm")
文章来源: Smaller gap between two legends in one plot (e.g. color and size scale)