如何手动定位重叠条形图和条宽?

How to position overlapping bar plot and bar width manually?

我想生成 3 个重叠的条形图,如下图所示:

但无法找到执行此操作的简单方法。

这是我尝试过的:

d1 <- c(1.0, 2.0, 3.0, 4.0, 4.8)
d2 <- c(0.15, 0.5, 1.0, 1.5, 3.3)
d3 <- c(0.16,0.7,0.7,1,2.5)

barplot(d1, # calling data
        ylim = c(0,5), # set y-limit
        width = c(1,1,1,1,1), # change barwidth,
        space = 0.2,
        col = yarrr::transparent("gray", trans.val = 0.9)) # change bar colour and transparency

par(new=T) # allow second chart to overlay the previous chart. Alternatively can use add=TRUE, e.g. barplot(df,add=TRUE)

barplot(d2, ylim=c(0,5), width = c(0.5,0.5,0.5,0.5,0.5), 
        space=c(0.4,rep(1.4,4)),
        density = c(5,5), angle=c(45,45), # assign angle and density of lines within bar
        axes=F)

par(new=T)

barplot(d3, ylim=c(0,5), width = c(0.5,0.5,0.5,0.5,0.5), 
        space= 10,
        density = c(15,15,15), angle=c(0,0,0), # assign angle and density of lines within bar
        axes=F)

这是我能达到的最接近的结果:

我面临的第一个问题是,即使我为第二个(宽度=0.5)和第三个图(宽度=0.5)分配了不同的宽度(宽度=1),第一个条形图的宽度也没有改变.

第二个问题是我找不到在第一个图中正确定位第二个和第三个图的简单方法。我想要在第一个条形图中并排放置第二个和第三个条形图,并且它们的总宽度等于第一个条形图的宽度。

有人可以帮忙吗?我也接受 ggplot 解决方案。谢谢

这样的事情怎么样:

d1 <- c(1.0, 2.0, 3.0, 4.0, 4.8)
d2 <- c(0.15, 0.5, 1.0, 1.5, 3.3)
d3 <- c(0.16,0.7,0.7,1,2.5)

df <- data.frame(d1, d2, d3)

library(dplyr)
library(ggplot2)

df %>%
  mutate(index = seq_along(d1)) %>%
  ggplot() +
  geom_col(
    aes(x = index, y = d1),
    width = 0.8,
    col = "black",
    fill = "forestgreen"
  ) +
  geom_col(
    aes(x = index, y = d2),
    width = 0.4,
    col = "black",
    fill = "red",
    position = ggplot2::position_nudge(x = 0.2)
  ) +
  geom_col(
    aes(x = index, y = d3),
    width = 0.4,
    col = "black",
    fill = "blue",
    position = ggplot2::position_nudge(x = -0.2)
  ) +
  labs(y = NULL)

reprex package (v2.0.1)

于 2022-03-30 创建

更新:图案而不是颜色

对于 ggplot2 本身,这实际上并不简单。如果您可以在 CRAN 之外下载软件包,那么您可以下载 ggpattern 并执行如下操作:

df %>%
  mutate(index = seq_along(d1)) %>%
  ggplot() +
  geom_col(
    aes(x = index, y = d1),
    width = 0.8,
    col = "black",
    fill = "forestgreen"
  ) +
  ggpattern::geom_col_pattern(
    aes(x = index, y = d2),
    width = 0.4,
    color = "black",
    fill = "white",
    pattern = 'stripe',
    pattern_size = 0.1,
    pattern_fill = "black",
    position = ggplot2::position_nudge(x = 0.2)
  ) +
  ggpattern::geom_col_pattern(
    aes(x = index, y = d3),
    width = 0.4,
    color = "black",
    fill = "lightgray",
    pattern = 'crosshatch',
    pattern_fill = "black",
    pattern_angle = 0,
    pattern_size = 0.1,
    position = ggplot2::position_nudge(x = -0.2)
  ) +
  labs(y = NULL)

reprex package (v2.0.1)

创建于 2022-03-31

ggpattern很好documented,所以你应该可以根据你的需要调整上面的例子。

如果你不想依赖 CRAN 之外的包,那么你可以查看这些旧问题中的任何一个以获得可能的解决方法:

  • How to add texture to fill colors in ggplot2

我看到像每两个条的背面和正面层。要做到这一点,宽度和微移的结合是至关重要的,我相信这应该可以做到:

library(dplyr)
library(ggplot2)


group_var <- 1:5
d1 <- c(1.0, 2.0, 3.0, 4.0, 4.8)
d2 <- c(0.15, 0.5, 1.0, 1.5, 3.3)
d3 <- c(0.16,0.7,0.7,1,2.5)
d4 <- c(1.162,0.7,0.7,1,3.5)

df <- data.frame(group_var, d1, d2, d3, d4)

p <- ggplot(df) + 
  geom_col(aes(x = group_var, y = d1), width = 0.4, col = "black", 
           fill = "grey", position = ggplot2::position_nudge(x = 0.2)) +
  geom_col(aes(x = group_var, y = d4), width = 0.4, col = "black", 
           fill = "black", position = ggplot2::position_nudge(x = -0.2)  ) +
  geom_col(aes(x = group_var, y = d2), width = 0.2, col = "black", 
           fill = "red",    position = ggplot2::position_nudge(x = 0.1)) +
  geom_col(aes(x = group_var, y = d3), width = 0.2, col = "black",
           fill = "blue",   position = ggplot2::position_nudge(x = -0.3))
print(p)