修改组合的水平条形图(紧密设计)

发布于 2025-01-16 07:46:21 字数 2399 浏览 0 评论 0原文

我有以下示例数据:

library(tidyverse)

df <- data.frame(col=rep(c("A_B", "A_C", "A_D", 
                           "B_A", "C_A", "D_A",
                           "B_C", "B_D", 
                           "C_B", "D_B",
                           "C_D", "D_C"), 2),
                 level=c(rep("lower_level", 12), rep("higher_level", 12)),
                 value=abs(rnorm(24, mean=5, sd=2)))%>% tibble()
df[c('origin', 'target')] <- str_split_fixed(df$col, '_', 2)
df <- df %>% select(c(origin, target, level, value))

我现在想为每个目标创建水平堆叠条形图 (df %>% filter(target=="A"))。我使用以下代码执行此操作:

# plot
p1 <- ggplot(data = df %>% filter(target=="A"), 
            aes(x = factor(level), y = value, fill = factor(origin)))+
  geom_bar(stat="identity", position="fill", width = .1) + 
  scale_fill_manual(values = c("A"="yellow", "B" = "green", "C"="red", "D"="blue")) +
  coord_flip()

由于我想稍后合并多个此类图(如下),因此我想

  1. 删除 y 轴和条形之间的空白空间(或将其操纵为值 X)

  2. fill 标签显示在右侧

  3. 左侧有一个值,表示“target: A”

  4. 并且在所有绘图之间共享填充图例和 y 轴。

请参阅带注释的图:

barplot

作为参考,我使用此代码创建了其他绘图:

p2 <- ggplot(data = df %>% filter(target=="B"), 
       aes(x = factor(level), y = value, fill = factor(origin)))+
  geom_bar(stat="identity", position="fill", width = .1) +
  scale_fill_manual(values = c("A"="yellow", "B" = "green", "C"="red", "D"="blue")) +
  coord_flip()

p3 <- ggplot(data = df %>% filter(target=="C"), 
             aes(x = factor(level), y = value, fill = factor(origin)))+
  geom_bar(stat="identity", position="fill", width = .1) +
  scale_fill_manual(values = c("A"="yellow", "B" = "green", "C"="red", "D"="blue")) +
  coord_flip()

p4 <- ggplot(data = df %>% filter(target=="D"), 
             aes(x = factor(level), y = value, fill = factor(origin)))+
  geom_bar(stat="identity", position="fill", width = .1) +
  scale_fill_manual(values = c("A"="yellow", "B" = "green", "C"="red", "D"="blue")) +
  coord_flip()

并将它们与此代码结合起来(但如果需要,很乐意使用其他方式来组合它们)。

library("gridExtra")
grid.arrange(p1, p2, p3, p4, ncol = 1, nrow = 4)

I have the following sample data:

library(tidyverse)

df <- data.frame(col=rep(c("A_B", "A_C", "A_D", 
                           "B_A", "C_A", "D_A",
                           "B_C", "B_D", 
                           "C_B", "D_B",
                           "C_D", "D_C"), 2),
                 level=c(rep("lower_level", 12), rep("higher_level", 12)),
                 value=abs(rnorm(24, mean=5, sd=2)))%>% tibble()
df[c('origin', 'target')] <- str_split_fixed(df$col, '_', 2)
df <- df %>% select(c(origin, target, level, value))

I now want to create horizontal stacked barplots for each target (df %>% filter(target=="A")). I do this using the following code:

# plot
p1 <- ggplot(data = df %>% filter(target=="A"), 
            aes(x = factor(level), y = value, fill = factor(origin)))+
  geom_bar(stat="identity", position="fill", width = .1) + 
  scale_fill_manual(values = c("A"="yellow", "B" = "green", "C"="red", "D"="blue")) +
  coord_flip()

Since I want to combine multiple such plots later (s. below), I would like to

  1. remove the empty space between y-axis and the bars (or manipulate it to value X)

  2. have the fill label displayed on the right side

  3. have one value on the left, saying "target: A"

  4. and have fill legend and y axis shared between all plots.

See annotated plot:

barplot

For reference, I create additional plots with this code:

p2 <- ggplot(data = df %>% filter(target=="B"), 
       aes(x = factor(level), y = value, fill = factor(origin)))+
  geom_bar(stat="identity", position="fill", width = .1) +
  scale_fill_manual(values = c("A"="yellow", "B" = "green", "C"="red", "D"="blue")) +
  coord_flip()

p3 <- ggplot(data = df %>% filter(target=="C"), 
             aes(x = factor(level), y = value, fill = factor(origin)))+
  geom_bar(stat="identity", position="fill", width = .1) +
  scale_fill_manual(values = c("A"="yellow", "B" = "green", "C"="red", "D"="blue")) +
  coord_flip()

p4 <- ggplot(data = df %>% filter(target=="D"), 
             aes(x = factor(level), y = value, fill = factor(origin)))+
  geom_bar(stat="identity", position="fill", width = .1) +
  scale_fill_manual(values = c("A"="yellow", "B" = "green", "C"="red", "D"="blue")) +
  coord_flip()

And combine them with this code (but happy to use other ways of combining them if needed).

library("gridExtra")
grid.arrange(p1, p2, p3, p4, ncol = 1, nrow = 4)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑饮青盏花 2025-01-23 07:46:22

听起来很像您只是想通过 target 进行分面。无需在这里拼接多个图。

ggplot(data = df %>% mutate(target = paste('Target:', target)), 
             aes(x = factor(level), y = value, fill = factor(origin)))+
  geom_col(position = "fill", width = 0.9) + 
  scale_fill_manual(values = c("A"="yellow", "B" = "green", 
                               "C"="red", "D"="blue"), name = 'origin') +
  facet_grid(target~., switch = 'y') +
  coord_flip() +
  theme(strip.placement = 'outside',
        strip.background = element_blank(),
        axis.title.y = element_blank())

输入图片此处描述

It sounds very much as though you simply want to facet by target. No need for stitching multiple plots here.

ggplot(data = df %>% mutate(target = paste('Target:', target)), 
             aes(x = factor(level), y = value, fill = factor(origin)))+
  geom_col(position = "fill", width = 0.9) + 
  scale_fill_manual(values = c("A"="yellow", "B" = "green", 
                               "C"="red", "D"="blue"), name = 'origin') +
  facet_grid(target~., switch = 'y') +
  coord_flip() +
  theme(strip.placement = 'outside',
        strip.background = element_blank(),
        axis.title.y = element_blank())

enter image description here

自由范儿 2025-01-23 07:46:22

两个建议_

  1. 要消除轴和条之间的偏移,请将轴扩展设置为零
    scale_x_continuous(..., Expand = c(0,0))
  2. 不要对数据框进行繁琐的子集化,而是使用 facet_wrapfacet_grid 选项ggplot 的:
ggplot(data = df, 
             aes(x = factor(level), y = value, fill = factor(origin))) +
       ## other plot instructions
       facet_wrap( ~target)

请参阅 ?facet_wrap 了解各种布局选项,例如绘图列数
3.无论如何,条形之间的垂直间距将根据输出尺寸进行调整(此处:图形高度)

two suggestions_

  1. to remove the offset between axis and bar, set the axis expansion to zero
    scale_x_continuous(..., expand = c(0,0))
  2. instead of tediously subsetting the data frame, use the facet_wrap or facet_grid option of ggplot:
ggplot(data = df, 
             aes(x = factor(level), y = value, fill = factor(origin))) +
       ## other plot instructions
       facet_wrap( ~target)

see ?facet_wrap for various layout options like number of plot columns
3. the vertical spacing between bars will be adjusted to the output dimensions (here: figure height) anyway

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文