如何在R中重叠两个图表?

Posted

技术标签:

【中文标题】如何在R中重叠两个图表?【英文标题】:How to overlap two diagrams in R? 【发布时间】:2021-12-16 19:00:20 【问题描述】:

我有以下数据框,我用它来使用下面给出的代码创建图表 -

数据 -

```structure(list(percents = c(52, 40, 34, 55, 48, 38, 17), label = c("Type 1", 
"Type 2", "Type 3", "Type 4", "Type 5", "Type 6", "Type 7")), class = "data.frame", row.names = c(NA, 
-7L))```

2018年图代码-

```df %>% mutate(r = sqrt(percents), x = r + cumsum(lag(2 * r, default = 0))) %>%
  ggplot() + 
  geom_circle(aes(x0 = x, r = r, y0 = r), size = 3, color = "gray") +
  geom_text(aes(x = x, y = r, label = paste0(percents, "%"), size = percents),
            fontface = "bold", color = "#643291") +
  geom_text(aes(x = x, y = 20, label = label), vjust = 0,
            fontface = "bold", color = "gray20", size = 3) +
  geom_segment(aes(x = x, xend = x, y = r + 3, yend = 18),
               color = "#643291", size = 2) +
  coord_equal() +
  scale_y_continuous(limits =c(-5, 25)) +
  scale_size_continuous(range = c(4, 8)) +
  theme_void() +
  theme(legend.position = "none") +
  labs(title ='2018')```


Then I have the following data for 2018 group B -

```structure(list(percents = c(48, 60, 66, 45, 52, 62, 83), label = c("Type 1", 
"Type 2", "Type 3", "Type 4", "Type 5", "Type 6", "Type 7")), class = "data.frame", row.names = c(NA, 
-7L))```

我使用与上面类似的代码(但不同的颜色来创建另一个图表)

 ```df %>% mutate(r = sqrt(percents), x = r + cumsum(lag(2 * r, default = 0))) %>%
  ggplot() + 
  geom_circle(aes(x0 = x, r = r, y0 = r), size = 3, color = "black") +
  geom_text(aes(x = x, y = r, label = paste0(percents, "%"), size = percents),
            fontface = "bold", color = "#643291") +
  geom_text(aes(x = x, y = 20, label = label), vjust = 0,
            fontface = "bold", color = "gray20", size = 3) +
  geom_segment(aes(x = x, xend = x, y = r + 3, yend = 18),
               color = "#643291", size = 2) +
  coord_equal() +
  scale_y_continuous(limits =c(-5, 25)) +
  scale_size_continuous(range = c(4, 8)) +
  theme_void() +
  theme(legend.position = "none") +
  labs(title ='2018')```

我的问题是,有没有一种方法可以重叠这两个图表,以像我现在所做的那样将两个线性圆圈集一起显示与并排显示?

谢谢!

【问题讨论】:

我将不胜感激任何形式的回应!谢谢! 您希望最终产品看起来像什么?你想要一排 14 个圆圈,你想要两个单独的面板一起显示,你想要每种类型的圆圈重叠吗?无论如何,最好将您的数据集与bind_rows(a, b, .id = "source") 之类的东西合并为一个。 感谢您的宝贵时间!我希望圆圈与一个共同的中点重叠。 【参考方案1】:

我认为这可能会让你接近你正在寻找的东西,但我不确定它是否是一个很好的可视化。

a <- 
  structure(
    list(
      percents = c(52, 40, 34, 55, 48, 38, 17), 
      label = c("Type 1", "Type 2", "Type 3", "Type 4", "Type 5", "Type 6", "Type 7")
    ), 
    class = "data.frame", 
    row.names = c(NA, -7L)
  )

b <- 
  structure(
    list(
      percents = c(48, 60, 66, 45, 52, 62, 83), 
      label = c("Type 1", "Type 2", "Type 3", "Type 4", "Type 5", "Type 6", "Type 7")
    ), 
    class = "data.frame", 
    row.names = c(NA, -7L)
  )

df <- 
  bind_rows(a, b, .id = "source") %>% 
  group_by(label) %>% 
  mutate(
    r = sqrt(percents), 
    max_r = max(r) # get the bigger circle of the pair
  ) %>% 
  group_by(source) %>% 
  mutate(
    x = max_r + cumsum(dplyr::lag(2 * max_r, default = 0)))
  )

ggplot(df) +
  ggforce::geom_circle(
    aes(
      x0 = x, 
      r = r, 
      y0 = 0, 
      color = source
    ),
    size = 1
  ) + 
  geom_text(
    data = filter(df, source == 1), 
    aes(
      x = x, 
      y = 2.5, 
      label = str_c(percents, "%"), 
      color = source
    ), 
    show.legend = FALSE
  ) +
  geom_text(
    data = filter(df, source == 2), 
    aes(
      x = x, 
      y = -2.5, 
      label = str_c(percents, "%"), 
      color = source
    ), 
    show.legend = FALSE
  ) +
  geom_text(
    aes(
      x = x, 
      y = -15, 
      label = label
    )
  ) + 
  labs(color = NULL) +
  coord_equal(clip = "off") + # so "Type" labels aren't clipped
  theme_void()

【讨论】:

以上是关于如何在R中重叠两个图表?的主要内容,如果未能解决你的问题,请参考以下文章

如何防止 splitLayout、Shiny、R 中的两个输入标签重叠?

在 devexpress 图表中找到两个系列的重叠点

如何在 R 中使用“组”创建分段图表

如何在 R 中创建显示预测模型、数据和残差的图表

基于选定的单选按钮选项,在闪亮的R中单击提交按钮后如何显示图表和数据?

如何在 Matlab 或 R 中进行重叠聚类分析?