r - 使用 ggridges 可视化泊松随机样本组

标签 r ggplot2 visualization data-visualization ggridges

我有两组数据,全部在一个数据框中。第一组与位置 1 收集的数据相关,第二组与位置 2 收集的数据相关。每个位置都有 5 个月的不同计数数据(列)。

# DataSet
-----------------
rp_data <-    structure(list(Month = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("1", 
"2", "3", "4", "5"), class = "factor"), location = c("1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "2", "2", "2", "2", 
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", 
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", 
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", 
"2", "2", "2", "2", "2", "2", "2"), value = c(0L, 1L, 1L, 1L, 
2L, 1L, 0L, 0L, 1L, 1L, 3L, 2L, 1L, 4L, 1L, 3L, 1L, 1L, 1L, 1L, 
2L, 2L, 1L, 0L, 2L, 4L, 3L, 5L, 5L, 0L, 4L, 3L, 3L, 4L, 2L, 5L, 
2L, 3L, 10L, 6L, 5L, 6L, 4L, 6L, 4L, 5L, 6L, 5L, 3L, 7L, 1L, 
1L, 1L, 1L, 0L, 0L, 2L, 1L, 2L, 0L, 2L, 3L, 4L, 1L, 2L, 1L, 2L, 
0L, 2L, 2L, 4L, 4L, 5L, 1L, 4L, 5L, 4L, 5L, 1L, 4L, 3L, 7L, 7L, 
4L, 2L, 5L, 4L, 1L, 5L, 3L, 7L, 3L, 4L, 8L, 5L, 7L, 1L, 1L, 6L, 
3L)), .Names = c("Month", "location", "value"), row.names = c(NA, 
-100L), class = "data.frame")

我在下面使用了这个示例,如 ggridges examples webpage, 所示显示不同月份的各种计数值。

# Plot 1 , filtering data related to location = 1
#---------------

ggplot(rp_data[rp_data$location == '1',], aes(x = value, y = Month, group = Month)) +
  geom_density_ridges2(aes(fill = Month), stat = "binline", binwidth = 1, scale = 0.95) +
  geom_text(stat = "bin",
            aes(y = group + 0.95*(..count../max(..count..)),
                label = ifelse(..count..>0, ..count.., "")),
            vjust = 1.4, size = 3, color = "white", binwidth = 1) +
  scale_x_continuous(breaks = c(0:12), limits = c(-.5, 13), expand = c(0, 0),
                     name = "random value") +
  scale_y_discrete(expand = c(0.01, 0), name = "Month",
                   labels = c("5.0", "4.0", "3.0", "2.0", "1.0")) +
  scale_fill_cyclical(values = c("#0000B0", "#7070D0")) +
  labs(title = "Poisson random samples location 1 different Month",
       subtitle = "sample size n=10") +
  guides(y = "none") +
  theme_ridges(grid = FALSE) +
  theme(axis.title.x = element_text(hjust = 0.5),
        axis.title.y = element_text(hjust = 0.5))

# Plot 2 , filtering data related to location = 2
#---------------

ggplot(rp_data[rp_data$location == '2',], aes(x = value, y = Month, group = Month)) +
  geom_density_ridges2(aes(fill = Month), stat = "binline", binwidth = 1, scale = 0.95) +
  geom_text(stat = "bin",
            aes(y = group + 0.95*(..count../max(..count..)),
                label = ifelse(..count..>0, ..count.., "")),
            vjust = 1.4, size = 3, color = "white", binwidth = 1) +
  scale_x_continuous(breaks = c(0:12), limits = c(-.5, 13), expand = c(0, 0),
                     name = "random value") +
  scale_y_discrete(expand = c(0.01, 0), name = "Month",
                   labels = c("5.0", "4.0", "3.0", "2.0", "1.0")) +
  scale_fill_cyclical(values = c("#0000B0", "#7070D0")) +
  labs(title = "Poisson random samples location 2 different Month",
       subtitle = "sample size n=10") +
  guides(y = "none") +
  theme_ridges(grid = FALSE) +
  theme(axis.title.x = element_text(hjust = 0.5),
        axis.title.y = element_text(hjust = 0.5))

图 1 的结果:

enter image description here

我的问题是如何组合这两个图,有点像 shown in this example 的叠加图:

enter image description here

我不想将它们绘制在两个单独的图中。

最佳答案

您需要创建一个包含月份位置的分组变量。您可以使用 paste0(Month, location) 来完成此操作。现在,我省略了文本标签,尽管多考虑一下它们也可能是可能的。 (但我认为他们会让人物变得太忙碌。)

ggplot(rp_data,
       aes(x = value, y = Month,
           group = paste0(Month, location),
           fill = paste0(Month, location))) +
  geom_density_ridges2(stat = "binline", binwidth = 1,
                       scale = 0.95, alpha = 0.7) +
  scale_x_continuous(breaks = c(0:12), limits = c(-.5, 13),
                     expand = c(0, 0), name = "random value") +
  scale_y_discrete(expand = c(0.01, 0), name = "Month",
                   labels = c("5.0", "4.0", "3.0", "2.0", "1.0")) +
  scale_fill_cyclical(values = c("#0000B0", "#B00000",
                                 "#7070D0", "#FC5E5E")) +
  labs(title = "Poisson random samples location 1 different Month",
       subtitle = "sample size n=10") +
  guides(y = "none") +
  theme_ridges(grid = FALSE, center = TRUE)

enter image description here

编辑:现在带有文本标签。

ggplot(rp_data, aes(x = value, y = Month, group = paste0(Month, location), fill = paste0(Month, location))) +
  geom_density_ridges2(stat = "binline", binwidth = 1, scale = 0.95, alpha = 0.7) +
  geom_text(stat = "bin",
            aes(y = ceiling(group/2) + 0.95*(..count../max(..count..)),
                label = ifelse(..count..>0, ..count.., ""), color = location),
            vjust = 1.4, size = 3, binwidth = 1, fontface = "bold") +
  scale_x_continuous(breaks = c(0:12), limits = c(-.5, 13), expand = c(0, 0),
                     name = "random value") +
  scale_y_discrete(expand = c(0.01, 0), name = "Month",
                   labels = c("5.0", "4.0", "3.0", "2.0", "1.0")) +
  scale_fill_cyclical(values = c("#0000B0", "#B00000", "#7070D0", "#FC5E5E")) +
  scale_color_cyclical(values = c("white", "black")) +
  labs(title = "Poisson random samples location 1 different Month",
       subtitle = "sample size n=10") +
  guides(y = "none") +
  theme_ridges(grid = FALSE, center = TRUE)

enter image description here

同样,不确定这是一个好主意,但就是这样。

关于r - 使用 ggridges 可视化泊松随机样本组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47825427/

相关文章:

r - 我正在尝试在 R (ggplot2) 中重新创建一个特定的情节

r - 计算以观察 == 1 为条件按受试者 ID 聚合的游程长度

r - ggsave() : Error in UseMethod ("grid.draw") : no applicable method for 'grid.draw' applied to an object of class "character"

R,ggplot2,热图

包含在 HTML 页面中的 Javascript D3 树可视化

r - 改变 purrr block 中的数据列表列,并通过对数字变量进行分组来获得静态最小值

r - 在ggplot中添加适当的标题

C# 和 R.Net 不使用 ggplot2 显示任何图形

iOS:播放时从 iTunes 音乐轨道中读取实时音频数据

javascript - 如何在D3力模拟中实现圆盘形状?