2017-12-15 6 views
2

Ich habe zwei Datensätze, alle in einem Datenrahmen. Der erste Satz bezieht sich auf Daten, die an Standort 1 gesammelt wurden, und der zweite Satz wird an Standort 2 gesammelt. Jeder Standort hat verschiedene Zähldaten (Spalte value) für 5 Monate.Visualisierung von Gruppen von Poisson-Stichproben mit ggridge

# 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") 

ich verwendet, um dieses Beispiel unten, wie auf der ggridges examples webpage, dargestellt in den verschiedenen Monaten die verschiedenen Zählwerte angezeigt werden soll.

# 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)) 

Ergebnis für Plot 1:

enter image description here

Meine Frage ist, wie kann ich diese beiden Grundstücke kombinieren, Art wie ein Overlay Plot als shown in this example:

enter image description here

Ich möchte sie nicht in zwei getrennten Parzellen darstellen.

Antwort

2

Sie müssen eine Gruppierungsvariable erstellen, die sowohl Month als auch location enthält. Sie können dies tun, indem Sie paste0(Month, location) verwenden. Für den Moment lasse ich die Text-Labels weg, obwohl sie auch mit ein wenig mehr Gedanken möglich sind. (Aber ich denke, sie würde die Figur zu sehr damit beschäftigt machen.)

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

Edit: Jetzt mit Beschriftungen.

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

Wieder nicht sicher, es ist eine gute Idee, aber es gehen Sie.