ggplot2:移动(重新定位)图例以免阻挡图形框
ggplot2: move (reposition) the legend so as not to block the graph box
如何将图例向左和底部移动几个点(图形框线的宽度)?
任务:我想将图例的背景设为半透明,但不与图形框重叠。
(红色边框 - 为了更好地可视化问题)
使用代码:
image + theme(
panel.background = element_rect(fill = "white", color = NA),
panel.border = element_rect(fill = NA, color = "black", size = 2),
panel.grid.major = element_line(color = "#00000040", linetype = 3),
axis.text = element_text(size = 10),
axis.title = element_text(size = 12),
axis.title.x = element_text(margin = margin(t = 10)),
axis.title.y = element_text(margin = margin(r = 10)),
legend.key = element_rect(fill = NA, color = NA),
legend.background = element_rect(fill = "#ffffff80", color = "red", size = 1),
legend.justification = c(1, 1),
legend.position = c(1, 1),
legend.title = element_text(size = 10, color = "black"),
plot.title = element_text(hjust = 0.5),
)
找到了!
legend.box.margin = margin(t = 2, r = 2),
如果你想让图例框与绘图边框的外边缘对齐,你需要调整legend.box.margin
,使上边缘的值与[中定义的线的宽度相同=12=].
没有示例数据,所以我创建了一些:
library(ggplot2)
x <- seq(0, 2 * pi, length.out = 100)
df <- data.frame(x = c(x, x), y = c(-cos(x), sin(x)),
group = rep(c("data1", "data2"), each = 100))
image <- ggplot(df, aes(x, y)) +
geom_line(aes(colour = group)) +
scale_colour_manual(values = c("red", "black"))
image + theme(
panel.background = element_rect(fill = "white", color = NA),
panel.border = element_rect(fill = NA, color = "black", size = 2),
panel.grid.major = element_line(color = "#00000040", linetype = 3),
axis.text = element_text(size = 10),
axis.title = element_text(size = 12),
axis.title.x = element_text(margin = margin(t = 10)),
axis.title.y = element_text(margin = margin(r = 10)),
legend.key = element_rect(fill = NA, color = NA),
legend.background = element_rect(fill = "#ffffff80", color = "red", size = 1),
legend.justification = c(1, 1),
legend.position = c(1, 1),
legend.title = element_text(size = 10, color = "black"),
legend.box.margin = margin(1, 0, 0, 0),
plot.title = element_text(hjust = 0.5)
)
由 reprex package (v0.3.0)
于 2020-05-25 创建
如何将图例向左和底部移动几个点(图形框线的宽度)?
任务:我想将图例的背景设为半透明,但不与图形框重叠。
(红色边框 - 为了更好地可视化问题)
使用代码:
image + theme(
panel.background = element_rect(fill = "white", color = NA),
panel.border = element_rect(fill = NA, color = "black", size = 2),
panel.grid.major = element_line(color = "#00000040", linetype = 3),
axis.text = element_text(size = 10),
axis.title = element_text(size = 12),
axis.title.x = element_text(margin = margin(t = 10)),
axis.title.y = element_text(margin = margin(r = 10)),
legend.key = element_rect(fill = NA, color = NA),
legend.background = element_rect(fill = "#ffffff80", color = "red", size = 1),
legend.justification = c(1, 1),
legend.position = c(1, 1),
legend.title = element_text(size = 10, color = "black"),
plot.title = element_text(hjust = 0.5),
)
找到了!
legend.box.margin = margin(t = 2, r = 2),
如果你想让图例框与绘图边框的外边缘对齐,你需要调整legend.box.margin
,使上边缘的值与[中定义的线的宽度相同=12=].
没有示例数据,所以我创建了一些:
library(ggplot2)
x <- seq(0, 2 * pi, length.out = 100)
df <- data.frame(x = c(x, x), y = c(-cos(x), sin(x)),
group = rep(c("data1", "data2"), each = 100))
image <- ggplot(df, aes(x, y)) +
geom_line(aes(colour = group)) +
scale_colour_manual(values = c("red", "black"))
image + theme(
panel.background = element_rect(fill = "white", color = NA),
panel.border = element_rect(fill = NA, color = "black", size = 2),
panel.grid.major = element_line(color = "#00000040", linetype = 3),
axis.text = element_text(size = 10),
axis.title = element_text(size = 12),
axis.title.x = element_text(margin = margin(t = 10)),
axis.title.y = element_text(margin = margin(r = 10)),
legend.key = element_rect(fill = NA, color = NA),
legend.background = element_rect(fill = "#ffffff80", color = "red", size = 1),
legend.justification = c(1, 1),
legend.position = c(1, 1),
legend.title = element_text(size = 10, color = "black"),
legend.box.margin = margin(1, 0, 0, 0),
plot.title = element_text(hjust = 0.5)
)
由 reprex package (v0.3.0)
于 2020-05-25 创建