gpt4 book ai didi

r - ggplot 中图例键之间的间距

转载 作者:行者123 更新时间:2023-12-01 22:17:54 27 4
gpt4 key购买 nike

图表顶部有一个图例。我希望图例左对齐,并且能够设置美学符号(彩色方 block )和文本之间的间距(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)))

enter image description here

最佳答案

adjust 添加边距element_text

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)),
legend.text = element_text(margin = margin(r = 10, unit = "pt")))

enter image description here

关于r - ggplot 中图例键之间的间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51780836/

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