将标签添加到ggbarplot上的条的末端后删除无关的轴样条

wgeznvg7  于 2023-11-14  发布在  其他
关注(0)|答案(2)|浏览(112)

我有一个这样的框架:

Group Total  Type Count Percentage
1  Group1   166 type1    65      39.20
2  Group1   166 type2    13       7.83
3  Group1   166 type3    73      44.00
4  Group1   166 type4     3       1.81
5  Group1   166 type5     0       0.00
6  Group1   166 type6    12       7.23
7  Group2    53 type1    16      30.20
8  Group2    53 type2     2       3.77
9  Group2    53 type3    33      62.30
10 Group2    53 type4     0       0.00
11 Group2    53 type5     0       0.00
12 Group2    53 type6     2       3.77

字符串
Dataframe 的dput对象:

structure(list(Group = c("Group1", "Group1", "Group1", "Group1", 
"Group1", "Group1", "Group2", "Group2", "Group2", "Group2", "Group2", 
"Group2"), Total = c(166, 166, 166, 166, 166, 166, 53, 53, 53, 
53, 53, 53), Type = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 
3L, 4L, 5L, 6L), levels = c("type1", "type2", "type3", "type4", 
"type5", "type6"), class = "factor"), Count = c(65, 13, 73, 3, 
0, 12, 16, 2, 33, 0, 0, 2), Percentage = c(39.2, 7.83, 44, 1.81, 
0, 7.23, 30.2, 3.77, 62.3, 0, 0, 3.77)), class = "data.frame", row.names = c(NA, 
-12L))


我想创建一个数据框,在条形图的末端有标签,但有尾随轴线,我如何在100%刻度线后删除此无关轴线?


的数据
下面是我的代码:

library(ggpubr)
g <- ggbarplot(df, x='Group', y='Percentage',
               fill='Type', ylab=FALSE,
               order=rev(c('Group1', 'Group2'))) + 
  scale_fill_manual(values=ggsci::pal_aaas('default', 1)(6)) +
  geom_text(data=unique(df[, c('Group', 'Total')]), 
            aes(x=Group, y=115, label=paste0('n = ', Total)), 
            color="black", hjust=1, size=4) +
  theme(plot.margin = unit(c(1,3,1,1), "lines"), 
        axis.ticks.y = element_blank()) +
  scale_y_continuous(expand=c(0, 0), breaks=seq(0, 100, 10)) +
  coord_flip()

eoigrqb6

eoigrqb61#

艾伦的方法绝对是更务实和更快的方法。
但是第二种选择是使用 * 次轴技巧 *,即通过次轴添加标签。然而,除了一些数据争论之外,这需要使用ggplot2从头开始创建您的图,并使用连续的y尺度(因为离散不允许次轴):

library(ggplot2)
library(ggpubr)
library(dplyr, warn = FALSE)

df <- df |>
  mutate(
    Group_num = as.numeric(factor(Group,
      levels = rev(c("Group1", "Group2"))
    ))
  )

dat_labels <- df |>
  distinct(Group, Group_num, Total) |>
  arrange(Group_num)

ggplot(df, aes(Percentage, Group_num, fill = Type)) +
  geom_col(orientation = "y", color = "black") +
  scale_fill_manual(
    values = ggsci::pal_aaas("default", 1)(6)
  ) +
  theme(
    plot.margin = unit(c(1, 3, 1, 1), "lines"),
    axis.ticks.y = element_blank()
  ) +
  scale_y_continuous(
    breaks = dat_labels$Group_num,
    labels = dat_labels$Group,
    sec.axis = dup_axis(
      labels = paste0("n = ", dat_labels$Total)
    )
  ) +
  scale_x_continuous(
    expand = c(0, 0), breaks = seq(0, 100, 10)
  ) +
  ggpubr::theme_pubr() +
  theme(
    axis.line.y.right = element_blank(),
    axis.ticks.y.right = element_blank()
  ) +
  labs(y = NULL)

字符串


的数据

vc9ivgsu

vc9ivgsu2#

您需要在coord_flip内设置硬ylim,但旋转clip = 'off'并增加右侧边距以允许标签适合:

ggbarplot(df, x = 'Group', y = 'Percentage', fill = 'Type', ylab = FALSE,
          order = rev(c('Group1', 'Group2'))) + 
  geom_text(data = unique(df[, c('Group', 'Total')]), 
            aes(x = Group, y = 115, label = paste0('n = ', Total)), 
            color = "black", hjust = 1, size = 4) +
  scale_fill_manual(values = ggsci::pal_aaas('default', 1)(6)) +
  scale_y_continuous(expand = c(0, 0), breaks = seq(0, 100, 10)) +
  coord_flip(clip = 'off', ylim = c(0, 100)) +
  theme(plot.margin = unit(c(1, 6, 1, 1), "lines"), 
        axis.ticks.y = element_blank())

字符串


的数据

相关问题