R ggplot:“一个;“尺寸稳健”;在右下角放置水平图例的方法

R ggplot:“一个;“尺寸稳健”;在右下角放置水平图例的方法,r,ggplot2,legend,R,Ggplot2,Legend,我试图在绘图区域外的右下角放置一个水平图例 我意识到这之前已经讨论过了。然而,在经历了一个漫长而令人沮丧的上午之后,我无法找到一个“尺寸稳健”的解决方案 以下是我找到的3种解决方案: 当我将图例位置设置为底部时:图例位于 底部的中心,但我无法用 图例.对齐 当我将图例位置设置为c(1,0)时:图例位于 右下角。但是,图例放置在绘图中 当我添加plot.margin并将legend.position进一步向下推时: 图例位于绘图的右下角和外部。但是,当我更改打印区域的大小时,图例的位置不再正确

我试图在绘图区域外的右下角放置一个水平图例

我意识到这之前已经讨论过了。然而,在经历了一个漫长而令人沮丧的上午之后,我无法找到一个“尺寸稳健”的解决方案

以下是我找到的3种解决方案:

  • 当我将
    图例位置设置为
    底部时:图例位于
    底部的中心,但我无法用
    
    图例.对齐
  • 当我将
    图例位置设置为
    c(1,0)
    时:图例位于 右下角。但是,图例放置在绘图中
  • 当我添加
    plot.margin
    并将
    legend.position
    进一步向下推时: 图例位于绘图的右下角和外部。但是,当我更改打印区域的大小时,图例的位置不再正确

第三种解决方案的定位不正确:


可复制代码:

# Generate random data------

sample.n = 50
sample.data = data.frame(x = runif(sample.n,0,1), y = runif(sample.n,0,1), value = runif(sample.n,0,10))

# Plot ------

library(ggplot2)

# Margins are fine. And If I change the size of the plotting area, the legend will always be bottom- centered
# Problem: Can't push it to the right side  
ggplot(sample.data, aes(x = x, y = y, color = value)) + geom_point() + 
  theme(legend.direction = "horizontal", legend.position = "bottom") 

# Pushed to the bottom-right Successfully 
# Problem: Legend inside the plot
ggplot(sample.data, aes(x = x, y = y, color = value)) + geom_point() + 
  theme(legend.direction = "horizontal",legend.position = c(1,0)) # Problem: inside plot 

# Placed at the bottom-right corner outside the plot region 
# Problem: If I change the size of the plotting region, the legend will not be cornred
ggplot(sample.data, aes(x = x, y = y, color = value)) + geom_point() + 
  theme(legend.direction = "horizontal", legend.position = c(0.9,-0.2),  
        plot.margin = unit(c(1, 1, 5, 1), "lines"))  

Theme Legend的文档:

图例。位置图例的位置(“无”、“左”、“右”, “底部”、“顶部”或两元素数字向量)

图例。对齐将图例定位在绘图内的定位点 (“中心”或两元素数字向量)


编辑使用2.2.0,可以使用主题中的
图例.对齐
图例.边距
将图例推到右侧

# Generate random data------

sample.n = 50
sample.data = data.frame(x = runif(sample.n,0,1), y = runif(sample.n,0,1), value = runif(sample.n,0,10))

# Plot ------

library(ggplot2)
library(gtable)
library(grid)

p = ggplot(sample.data, aes(x = x, y = y, color = value)) + geom_point() + 
  theme(legend.direction = "horizontal", legend.position = "bottom",
          legend.box.background = element_rect(colour = "black"))

p + theme(legend.justification = "right",
          legend.margin = margin(t = 2, r = 0, b = 2, l = 2, unit = "mm"))


不管它值多少钱,我保留了原来的答案,但是更新到了ggplot版本2.2.0

我在这里所做的是从绘图中提取图例,将图例包装在视口中,使视口位于右侧,将图例放回绘图中,然后删除原始图例

# Generate random data------

sample.n = 50
sample.data = data.frame(x = runif(sample.n,0,1), y = runif(sample.n,0,1), value = runif(sample.n,0,10))

# Plot ------

library(ggplot2)
library(gtable)
library(grid)

p = ggplot(sample.data, aes(x = x, y = y, color = value)) + geom_point() + 
  theme(legend.direction = "horizontal", legend.position = "bottom")

# Get the ggplot grob
g = ggplotGrob(p)

# Get the legend
index = which(g$layout$name == "guide-box")
leg = g$grobs[[index]]

# Wrap the legend in a viewport
leg$vp = viewport(x = unit(1, "npc"), width = sum(leg$widths), just = "right")

### Put the legend back into the plot
     # First, get the current location of the legend
pos = g$layout[index, ]
g = gtable_add_grob(g, leg, t = pos$t, l = pos$l)

# Remove the original legend
g$grobs <- g$grobs[-index]
g$layout <- g$layout[-index, ]

# Draw the chart
grid.newpage()
grid.draw(g)
#生成随机数据------
样本n=50
sample.data=data.frame(x=runif(sample.n,0,1),y=runif(sample.n,0,1),value=runif(sample.n,0,10))
#密谋------
图书馆(GG2)
图书馆(gtable)
图书馆(网格)
p=ggplot(sample.data,aes(x=x,y=y,color=value))+geom_point()
主题(legend.direction=“水平”,legend.position=“底部”)
#把它弄得乱七八糟
g=ggb(p)
#获得传奇
索引=哪个(g$layout$name==“引导框”)
支腿=g$grobs[[指数]]
#在视口中包裹图例
leg$vp=视口(x=单位(1,“npc”),宽度=总和(leg$widths),just=“right”)
###将图例放回情节中
#首先,获取图例的当前位置
pos=g$布局[索引,]
g=表格加总(g,支腿,t=位置$t,l=位置$l)
#删除原始图例

g$grobs我决定我想要的数字有多大,然后调整传奇人物的位置等等@RichardTelford我曾经这样做过,它确实奏效了。然而,我试图为多个数据集绘制相同的图,每个数据集的大小不同。所以这个解决方案现在不适用。非常感谢桑迪!感谢您的帮助:-)
# Get the ggplot grob
g = ggplotGrob(p)

# Get the legend
index = which(g$layout$name == "guide-box")
leg = g$grobs[[index]]

# Remove the legend right margin and an internal margin
leg$widths[4] = unit(0, "mm")
leg$grobs[[1]]$widths[5] = unit(0, "mm")

# Wrap the legend in a viewport
leg$vp = viewport(x = unit(1, "npc"), width = sum(leg$widths), just = "right")

# Wrap the internal part of the legend in a right justified viewport
leg$grobs[[1]]$vp = viewport(x = unit(1, "npc"), width = sum(leg$grobs[[1]]$widths), just = "right")

### Put the legend back into the plot
     # First, get the current location of the legend
pos = g$layout[index, ]
g = gtable_add_grob(g, leg, t = pos$t, l = pos$l)

# Remove the original legend
g$grobs <- g$grobs[-index]
g$layout <- g$layout[-index, ]

# Draw the chart
grid.newpage()
grid.draw(g)