r - 在 R 中的 ggplot 条形图中包裹文本并增加 y 轴上的因素之间的空间?

标签 r ggplot2 bar-chart

我有一个数字,其中 y 轴上的因素是长类别名称。如果我把它们包起来,它们就会跑在一起,除非我把字体做得很小,即使这样,名字也有点太接近了。我想通过增加 y 轴因子之间的空间来给这些类别名称一些空间。但是,我一直无法找到一种方法来做到这一点。条形大小很好,我只是希望 y 轴上的条形和刻度线距离更远,以获得更好的文本间距。

没有换行并且字体 10 的文本太多,图表太少: nowrap_font10.png

以 15 个字符宽度和字体 10 重叠文本换行: wrap15_font10.png

以 15 个字符宽度和 5 号字体换行会导致小文本的间距仍然不够好: wrap15_font5.png

代码:

library(tidyverse)
    
df <- tribble(
~sjr_categories,                                               ~labels,               ~subgr_count,
"Sociology and Political Science",                             "control",             22,
"Sociology and Political Science",                             "nest",                2,
"Sociology and Political Science",                             "rank",                22,
"Sociology and Political Science",                             "mixed types",         15,
"Sociology and Political Science",                             "unclear definitions", 52,
"Political Science and International Relations",               "control",             22,
"Political Science and International Relations",               "nest",                2,
"Political Science and International Relations",               "rank",                8,
"Political Science and International Relations",               "mixed types",         2,
"Political Science and International Relations",               "unclear definitions", 34,
"Arts and Humanities (miscellaneous)",                         "control",             3,
"Arts and Humanities (miscellaneous)",                         "rank",                15,
"Arts and Humanities (miscellaneous)",                         "mixed types",         9,
"Arts and Humanities (miscellaneous)",                         "unclear definitions", 38,
"Social Sciences (miscellaneous)",                             "control",             4,
"Social Sciences (miscellaneous)",                             "nest",                5,
"Social Sciences (miscellaneous)",                             "rank",                9,
"Social Sciences (miscellaneous)",                             "mixed types",         10,
"Social Sciences (miscellaneous)",                             "unclear definitions", 34,
"Economics and Econometrics",                                  "control",             13,
"Economics and Econometrics",                                  "rank",                2,
"Economics and Econometrics",                                  "mixed types",         12,
"Economics and Econometrics",                                  "unclear definitions", 27,
"Geography, Planning and Development",                         "control",             9,
"Geography, Planning and Development",                         "rank",                2,
"Geography, Planning and Development",                         "mixed types",         2,
"Geography, Planning and Development",                         "unclear definitions", 18,
"Business and International Management",                       "control",             3,
"Business and International Management",                       "nest",                2,
"Business and International Management",                       "rank",                2,
"Business and International Management",                       "mixed types",         8,
"Business and International Management",                       "unclear definitions", 15,
"Computer Science Applications",                               "control",             4,
"Computer Science Applications",                               "mixed types",         5,
"Computer Science Applications",                               "other types",         3,
"Computer Science Applications",                               "unclear definitions", 18,
"Medicine (miscellaneous)",                                    "nest",                5,
"Medicine (miscellaneous)",                                    "rank",                8,
"Medicine (miscellaneous)",                                    "mixed types",         4,
"Medicine (miscellaneous)",                                    "unclear definitions", 12,
"Biochemistry, Genetics and Molecular Biology (miscellaneous)","nest",                5,
"Biochemistry, Genetics and Molecular Biology (miscellaneous)","rank",                11,
"Biochemistry, Genetics and Molecular Biology (miscellaneous)","mixed types",         2,
"Biochemistry, Genetics and Molecular Biology (miscellaneous)","unclear definitions", 8,
"Business, Management and Accounting (miscellaneous)",         "control",             7,
"Business, Management and Accounting (miscellaneous)",         "mixed types",         5,
"Business, Management and Accounting (miscellaneous)",         "unclear definitions", 11,
"Management of Technology and Innovation",                     "nest",                2,
"Management of Technology and Innovation",                     "unclear definitions", 19,
"Organizational Behavior and Human Resource Management",       "control",             8,
"Organizational Behavior and Human Resource Management",       "rank",                8,
"Organizational Behavior and Human Resource Management",       "unclear definitions", 5
)

df %>%
  ggplot(aes(x=subgr_count, y=fct_reorder(sjr_categories, subgr_count),
             fill = labels
             )) +
  geom_col() +
  theme_bw() +
  scale_fill_viridis(discrete = TRUE) +
  scale_y_discrete(labels = function(x) str_wrap(x, width = 15)) +
  theme(axis.text = element_text(size = 5)) + 
  labs(x = "count", y = NULL) 

ggsave(filename="wrap15_font5.png")

最佳答案

有关增加答案第二部分中条形间距的详细信息。

我认为这实际上是一个根据您自己的喜好进行的练习,选择什么看起来最好:选项包括:

  1. 将图例移至底部,为 y 轴文本提供更多宽度。您可以通过减小键的大小来缩小图例,从而为轴标签提供更多的垂直空间。
  2. 减小条形宽度,以便轴文本之间有更多空白。
  3. 最终图的大小,我假设它需要在 A4 纵向页面上呈现,并留有边距和图形标题。这可能是可能性的定义标准。
  4. 您在问题中明确指出的标准和权衡。

我个人会选择大幅减小条形宽度:遵循 Tufte 的高数据墨水比率原则:重要的是每个堆叠条形的相对长度而不是厚度。我已将条形宽度从默认的 0.9 减少到 0.8。

library(tibble)
library(ggplot2)
library(viridis)
library(forcats)
library(stringr)


df |> 
  ggplot(aes(x=subgr_count, y=fct_reorder(sjr_categories, subgr_count), 
             fill = labels)) +
  geom_col(width = 0.8) +
  scale_fill_viridis(discrete = TRUE) +
  scale_y_discrete(labels = function(x) str_wrap(x, width = 35)) +
  guides(fill = guide_legend(nrow = 2))+
  theme_bw() +
  theme(legend.position = "bottom",
        legend.direction = "horizontal",
        legend.justification = "left",
        legend.text = element_text(size = 10),
        legend.key.size = unit(4, "mm"),
        axis.text = element_text(size = 10)) +
  
  labs(x = "count", y = NULL) 

enter image description here


 ggsave(filename="wrap35_font10.png", width = 180, height = 200, units = "mm")

创建于 2022 年 10 月 16 日 reprex v2.0.2

个人喜好:较小的绘图区域,将宽度减小到0.4,否则与上面相同:

 ggsave(filename="wrap35_font10_width_0.4.png", width = 150, height = 180, units = "mm")

enter image description here

附录:增加条形之间的间距

关于OP评论中指出的核心问题:如何增加条形之间的间隙(以允许y轴标签之间更好的分离)。这可以通过调整图的条形宽度和输出高度来实现:

为了说明这一点,这里有一个最小的例子:

对于离散标度,ggplot 中的离散值以 1 个单位的间隔放置。 要增加条形之间的间隙,您有几个水平条形选项:

a) 减少条形宽度,但这会保留给定图的条形之间的绝对距离,它只会增加条形之间的空白。 b) 增加总体地 block 高度。对于给定数量的条形,这会增加每个条形的相对厚度,同时也会增加每个条形之间的相对距离。

因此,为了增加条形之间的空间(从而为换行文本留出更多空间),请增加绘图的高度并减小条形的宽度。

希望这可以通过这些并排的图来说明,其中除了图的高度之外,所有东西都是相同的。

df1 <- data.frame(var = c("Discrete value 1", "Discrete value 2 description", "Discrete value 3 even longer description"),
                  val = 1:3)

p1 <- 
  ggplot(df1, aes(val, var))+
  geom_col(width = 0.1)+
  scale_y_discrete(labels = function(x) str_wrap(x, width = 15))+
  labs(x = NULL,
       y = NULL)+
  theme_bw()+
  theme(axis.text.y = element_text(size = 10))

ggsave(p1, filename="wrap15_font10_100x100.png", height = 100, width = 100, units = "mm")

ggsave(p1, filename="wrap15_font10_150x100.png", height = 150, width = 100, units = "mm")


enter image description here

黑客攻击

并不真正推荐这样做,但您可以在实际值之间插入虚拟离散值以强制条形之间的分离...

library(ggplot2)
library(stringr)
library(forcats)

df1 <- data.frame(var = fct_inorder(c("Discrete value 1", "gap1", "Discrete value 2 description", "gap2", "Discrete value 3 even longer description")),
                  val = c(1, 0, 2, 0, 3))


  ggplot(df1, aes(val, var))+
  geom_col(width = 0.9)+
  scale_y_discrete(labels = function(x) ifelse(str_detect(x, "gap"), "", str_wrap(x, width = 15)))+
  labs(title = "A hack: include dummy discrete values between actual values",
       x = NULL,
       y = NULL)+
  theme_bw()+
  theme(axis.text.y = element_text(size = 10))

创建于 2022 年 10 月 16 日 reprex v2.0.2

关于r - 在 R 中的 ggplot 条形图中包裹文本并增加 y 轴上的因素之间的空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74084322/

相关文章:

R:比这个for循环更有效的解决方案

R通过时间维度堆叠栅格数据

R - 返回矩阵中元素的位置?

r - 如何仅显示 geom_smooth 预测的一部分?

reporting-services - SSRS : Define custom X axis position on bar chart

r - 通过 XLConnect 从 R 更改 MS Excel 文件中的单元格格式

r - ggplot2: "Unknown parameters: probs"for fun.y = geom_line() 中的分位数

r - 与 cowplot 和 plot_grid 共享面板边框

charts - JMeter 按小时记录结果

android - 如何在android中使用渐变颜色制作水平条形图