R语言 GGPlot轴自定义(空间/分割)

webghufk  于 2022-12-25  发布在  其他
关注(0)|答案(2)|浏览(181)

我有一个图,我想将其分为"基线"和"6个月"

  • 6个月变量标记为x1/x2
  • 基线是自命名的。

我想自定义绘图,以便:

  • 两个子集之间存在分离/间隔(基线+6个月)
  • 并且潜在地使得两个子集具有不同的条宽度。

希望了解如何在R中对此进行编码以及所使用的包类型。
谢谢。

library(ggplot2)
library(ggnewscale)

x1 = rep(1:3, times = 2)
x2 = rep(c("Y", "N"), each = 3)
baseline = rep(0:1, times = 3)
y <- data.frame(x1,x2,baseline)

Dat <- y |> 
  mutate(across(everything(), as.character)) |> 
  pivot_longer(everything(), names_to = "var")

ggplot(Dat, aes(y = var)) +
  coord_flip() +
  geom_bar(data = ~subset(.x, var %in% c("x1")), aes(fill = value), position = "fill") +
  scale_fill_viridis_d(option = "B", guide = guide_legend(order = 3))+ 
  new_scale_fill() +
  geom_bar(data = ~subset(.x, var %in% c("x2")), aes(fill = value), position = "fill") +
  scale_fill_brewer(palette = "Dark2", guide = guide_legend(order = 2)) +
  new_scale_fill() +
  geom_bar(data = ~subset(.x, var %in% c("baseline")), aes(fill = value), position = "fill") +
  scale_fill_brewer(palette = "Paired", guide = guide_legend(order = 1))

wdebmtf2

wdebmtf21#

在两个子集之间添加一些空间并具有不同条宽度的一个选项是创建两个单独的图并使用例如patchwork将它们粘合在一起。

library(ggplot2)
library(ggnewscale)
library(tidyr)
library(dplyr)

x1 = rep(1:3, times = 2)
x2 = rep(c("Y", "N"), each = 3)
baseline = rep(0:1, times = 3)
y <- data.frame(x1,x2,baseline)

Dat <- y |> 
  mutate(across(everything(), as.character)) |> 
  pivot_longer(everything(), names_to = "var")

p1 <- ggplot(Dat, aes(x = var)) +
  geom_bar(data = ~subset(.x, var %in% c("x1")), aes(fill = value), position = "fill") +
  scale_fill_viridis_d(option = "B", guide = guide_legend(order = 3))
  
p2 <- ggplot(Dat, aes(x = var)) +
  geom_bar(data = ~subset(.x, var %in% c("x2")), aes(fill = value), position = "fill") +
  scale_fill_brewer(palette = "Dark2", guide = guide_legend(order = 2)) +
  new_scale_fill() +
  geom_bar(data = ~subset(.x, var %in% c("baseline")), aes(fill = value), position = "fill") +
  scale_fill_brewer(palette = "Paired", guide = guide_legend(order = 1)) +
  theme(axis.line.y = element_blank(),
        axis.ticks.length.y = unit(0, "pt"),
        axis.ticks.y = element_blank(),
        axis.text.y = element_blank(),
        axis.title.y = element_blank())
  
library(patchwork)

p1 + p2 + plot_layout(guides = "collect")

z9smfwbn

z9smfwbn2#

另一个选项是使用自定义标签将“x”轴设置为数字,并手动调整宽度:

ggplot(Dat, aes(y = 5)) +
  coord_flip() +
  geom_bar(data = ~subset(.x, var %in% c("x1")), width = 1,
           aes(fill = value), position = "fill") +
  scale_fill_viridis_d(option = "B", guide = guide_legend(order = 3)) + 
  new_scale_fill() +
  geom_bar(data = ~subset(.x, var %in% c("x2")), aes(fill = value, y = 6), 
           position = "fill") +
  scale_fill_brewer(palette = "Dark2", guide = guide_legend(order = 2)) +
  new_scale_fill() +
  geom_bar(data = ~subset(.x, var %in% c("baseline")), width = 2,
           aes(fill = value, y = 1), position = "fill") +
  scale_fill_brewer(palette = "Paired", guide = guide_legend(order = 1)) +
  scale_y_continuous(breaks = c(1, 5, 6), labels = c('Baseline', 'x1', 'x2'))

相关问题