有没有办法在ggplot2中更改图例项之间的间距?我现在有
legend.position ="top"
Run Code Online (Sandbox Code Playgroud)
它会自动生成水平图例.然而,物品的间距非常接近,我想知道如何将它们分开更远.
在ggplot2中,我不知道如何增加图例项之间的差距.我读过这些职位的一些类似的问题1,2,但它并没有对我的情况下工作.下面是我的代码,它生成附图.我想增加图例项之间的差距,如附图所示.任何帮助或提示将非常感激.谢谢.
我的代码:
## data:
df <- data.frame(supp=rep(c(" link ratio 1:1 ", " link ratio 1:2 ", " link ratio 1:3 ",
" link ratio 1:4 ", " link ratio 1:5 ", " link ratio 1:6 "),
each=7, ## number of bargroups
ordered = TRUE), ## nrows
test_X=rep(c("1.0", "1.2", "1.4", "1.6", "1.8", "2.0", "2.2"), 6), ## ncols
test_Y=c(
8, 9, 16, 18, 23, 28, 27,
14, 15, 27, 30, 38, 47, 47,
8, 8, 11, 15, 21, …
Run Code Online (Sandbox Code Playgroud) 这是对/sf/ask/2259257941/的跟进
问题是调整图例元素以增加图例键之间的空间,而无需同时扩展图例键本身.解决方案可能是调整正确的图例主题选项.
期望的结果:图例关键文本标签之间的垂直空间更大,但不延伸图例关键线.
d <- data.frame(x = mtcars$mpg, y = 0.10)
vlines <- rbind(aggregate(d[1], d[2], mean),
aggregate(d[1], d[2], median))
vlines$stat <- rep(c("mean", "median"), each = nrow(vlines)/2)
library("ggplot2")
ggplot(data = d, aes(x = x, y = ..density..)) +
geom_histogram(fill = "lightblue", color = "black") +
geom_vline(data = vlines, mapping = aes(xintercept = x, colour = stat),
show.legend = TRUE) +
theme(legend.direction = "vertical",
legend.position = "right",
# legend.key = element_rect(size = 2),
legend.key.size = unit(3, "cm"),
# legend.key.width = …
Run Code Online (Sandbox Code Playgroud) 我在图的顶部有一个图例。我希望图例保持左对齐,并能够设置美学符号(彩色正方形)和文本之间的间距(1),以及文本和下一个美学符号之间的间距(2)。
library(tidyverse)
mtcars %>%
mutate(transmission = ifelse(am, "manual", "automatic")) %>%
ggplot() +
aes(x = transmission, fill = transmission) +
geom_bar() +
labs(fill = NULL) +
theme(
#legend.spacing.x = unit(.5, "char"), # adds spacing to the left too
legend.position = "top",
legend.justification = c(0,0),
legend.title=element_blank(),
legend.margin=margin(c(5,5,5,0)))
Run Code Online (Sandbox Code Playgroud)