如何在ggplot中将图例文本居中

问题描述 投票:0回答:1

我有一个如下所示的情节: enter image description here

我使用这些数据和代码来生成它:

structure(list(zone = c("Zone 1", "Zone 2", "Zone 3", "Zone 4", 
"Zone 5", "Total", "Zone 1", "Zone 2", "Zone 3", "Zone 4", "Zone 5", 
"Total"), effort = c(3226, 4705, 2443, 131, 276, 10781, 1757, 
7293, 1631, 34.4, 442, 11157), creelyear = c("2013-2014", "2013-2014", 
"2013-2014", "2013-2014", "2013-2014", "2013-2014", "2022-2023", 
"2022-2023", "2022-2023", "2022-2023", "2022-2023", "2022-2023"
)), class = "data.frame", row.names = c(NA, -12L))
df %>% 
  ggplot(aes(x= zone, y= effort, fill=creelyear)) +
  geom_bar(position="dodge",  stat="identity", color = "black")  +                                                      #Select data for plotting
  scale_y_continuous(labels = comma, name = "Pressure (h/km)",
                     limits = c(0,12000), expand = c(0, 0))  +       #Change y axis name and expand so bars touch y axis
  scale_fill_manual(name = "", labels = c("2013-2014", "2022-2023"), values = c("#000000","#d11141"))+
  scale_x_discrete(limit = c("Zone 1", "Zone 2", "Zone 3", "Zone 4", "Zone 5", "Total")) +
  xlab("Zone") +                                                                  #Change name of x axis
  theme(legend.position = 'top',                                                    #Select legend position
        legend.spacing.x = unit(1.0, 'cm'),                                         #Select legend spacing value and units
        legend.background = element_blank(),
        legend.box.background = element_rect(colour = "black"),
        legend.text = element_text(margin = margin(r = 6.2, unit = "cm"), hjust = 2),
        panel.background = element_rect(fill='transparent'),                        #Set panel background to transparent for presentations
        plot.background = element_rect(fill='transparent', color = "transparent"),  #Set plot background to transparent for presentations
        panel.grid.major = element_blank(),                                         #Remove major gridlines
        panel.grid.minor = element_blank(),
        panel.border = element_rect(colour = "black", fill=NA, size=1))

如何使文本在图例中居中,并在颜色和文本之间留出更多空间,使其看起来更美观?

r ggplot2 plot legend
1个回答
0
投票

以下是一些可能对您有用的选项。

legend.key.spacing.x
是我用来尝试通过顶部跨越图例实现您想要的效果的方法。请记住,您正在指定间距的精确测量值,这将取决于您的总图像尺寸和绘图大小等。首先是一个简单的垫子,无需太多扩展盒子。其次是您正在寻找的内容。第三是如果我是你我个人会做什么。

library(ggplot2)
df <- structure(list(zone = c("Zone 1", "Zone 2", "Zone 3", "Zone 4", 
    "Zone 5", "Total", "Zone 1", "Zone 2", "Zone 3", "Zone 4", "Zone 5", 
    "Total"), effort = c(3226, 4705, 2443, 131, 276, 10781, 1757, 
        7293, 1631, 34.4, 442, 11157), creelyear = c("2013-2014", "2013-2014", 
            "2013-2014", "2013-2014", "2013-2014", "2013-2014", "2022-2023", 
            "2022-2023", "2022-2023", "2022-2023", "2022-2023", "2022-2023"
        )), class = "data.frame", row.names = c(NA, -12L))

base <- df |> 
    ggplot(aes(x= zone, y= effort, fill=creelyear)) +
    geom_bar(position="dodge",  stat="identity", color = "black") +
    scale_y_continuous(labels = scales::comma, name = "Pressure (h/km)",
        limits = c(0,12000), expand = c(0, 0)) +
    scale_fill_manual(labels = c("2013-2014", "2022-2023"), values = c("#000000","#d11141"))+ # No need to specify name = "" if you set legend title to element_blank()
    scale_x_discrete(limit = c("Zone 1", "Zone 2", "Zone 3", "Zone 4", "Zone 5", "Total")) +
    xlab("Zone") +
    theme(panel.background = element_rect(fill='transparent'),
        plot.background = element_rect(fill='transparent', color = "transparent"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_rect(colour = "black", fill=NA, linewidth=1))
base +
    theme(legend.position = 'top',
        legend.key.spacing.x = unit(1.5, 'cm'),
        legend.background = element_rect(colour = "black"),
        legend.title = element_blank())

base +
    theme(legend.position = 'top',
        legend.key.spacing.x = unit(10, 'cm'),
        legend.background = element_rect(colour = "black"),
        legend.title = element_blank())

base +
    theme(legend.position = 'inside',
        legend.byrow = TRUE,
        legend.justification.inside = c(0.025, 0.975),
        legend.background = element_rect(colour = "black"),
        legend.title = element_blank())

创建于 2024-05-06,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.