簡體   English   中英

如何獲取保留或刪除重復行的data.frame列表的堆棧條圖?

[英]How can I get stack bar plot for list of data.frame where keeping or removing duplicated rows?

我有需要按閾值分類的data.frame列表,最后需要按文件欄的不同類別獲取堆棧欄圖。 但是,在我的data.frame列表中,有些行是重復的,因此我需要在某些圖中顯示這些重復的行,但是這些重復的行也應該刪除並顯示另一個圖。 因為,保留,刪除這些不同類別中的重復行,可能會帶來不同的見解以了解結果。 基於堆棧條形圖的名稱,我打算保留並刪除某些類別中的這些重復行。 我很難獲得期望的情節。 誰能指出我如何輕松實現這一目標? 如何准備樣地數據以獲得所需的樣地? 任何想法 ?

可復制的data.frame:

Qualified <- list(
    hotan = data.frame( begin=c(7,13,19,25,31,37,43,49,55,67,79,103,31,49,55,67), 
                        end=  c(10,16,22,28,34,40,46,52,58,70,82,106,34,52,58,70), 
                        pos.score=c(11,19,8,2,6,14,25,10,23,28,15,17,6,10,23,28)),
    aksu = data.frame( begin=c(12,21,30,39,48,57,66,84,111,30,48,66,84), 
                       end=  c(15,24,33,42,51,60,69,87,114,33,51,69,87), 
                       pos.score=c(5,11,15,23,9,13,2,10,16,15,9,2,10)),
    korla = data.frame( begin=c(6,14,22,30,38,46,54,62,70,78,6,30,46,70), 
                        end=c(11,19,27,35,43,51,59,67,75,83,11,35,51,75), 
                        pos.score=c(9,16,12,3,20,7,11,13,14,17,9,3,7,14))
)

unQualified <- list(
    hotan = data.frame( begin=c(21,33,57,69,81,117,129,177,225,249,333,345,33,81,333), 
                        end=  c(26,38,62,74,86,122,134,182,230,254,338,350,38,86,338), 
                        pos.score=c(7,34,29,14,23,20,11,30,19,17,6,4,34,23,6)),
    aksu = data.frame( begin=c(13,23,33,43,53,63,73,93,113,123,143,153,183,33,63,143), 
                       end=  c(19,29,39,49,59,69,79,99,119,129,149,159,189,39,69,149), 
                       pos.score=c(5,13,32,28,9,11,22,12,23,3,6,8,16,32,11,6)),
    korla = data.frame( begin=c(23,34,45,56,67,78,89,122,133,144,166,188,56,89,144), 
                        end=c(31,42,53,64,75,86,97,130,141,152,174,196,64,97,152), 
                        pos.score=c(3,10,19,17,21,8,18,14,4,9,12,22,17,18,9))
)

編輯

我確實以這種方式對數據進行了分類:

singleDF <- 
    bind_rows(c(Qualified = Qualified, Unqualified = unQualified), .id = "id") %>% 
    tidyr::separate(id, c("group", "list")) %>%
    mutate(elm = ifelse(pos.score >= 10, "valid", "invalid")) %>% 
    arrange(list, group, desc(elm))

res <- singleDF %>% split(list(.$list, .$elm, .$group))

這是我想要的情節:

在此處輸入圖片說明

請注意,在validinvalid類別中,我需要對data.frame進行重復刪除,而在QualifiedUnQualified類別中,我將保留這些重復的行。

如何獲得理想的情節? 如何通過使用ggplot2軟件包來實現此ggplot2 有什么想法嗎? 提前致謝 :)

也許是這樣的:

library(tidyverse)
library(cowplot)
theme_set(theme_grey())

p1 <- ggplot(filter(singleDF, list == "aksu"), 
             aes(group, fill = elm)) +
  geom_bar() +
  ylim(0, 16) +
  theme(legend.position = 'top', legend.title = element_blank(), axis.title.x = element_blank())

p2 <- ggplot(filter(singleDF, list == "aksu") %>% distinct(), 
             aes(elm, fill = group)) +
  geom_bar() +
  scale_fill_discrete(h.start = 90) +
  ylim(0, 16) +
  theme(legend.position = 'top', legend.title = element_blank(), axis.title.x = element_blank())

plot_grid(p1, p2, align = 'v', nrow = 1)

在此處輸入圖片說明

如果要對列表的每個元素執行此操作,則可以使用tidyverse包並將tidyverse的答案包裝到函數中。 我修改了@Axeman的代碼來獲得所需的外觀,盡管我不使用cowplot所以我替換了gridExtra

編輯:輕松修復即可獲得所需的繪圖,只需簡單地將grid.arrange the map的結果單行排列即可。 我還調整了情節,使其與您所需的輸出更加一致。 我使用geom_label來獲取計數,使用stat="count"並使用..count..特殊變量。 您可以根據需要將其切換為geom_text

library(tidyverse)
library(grid) #for grid.draw
library(gridExtra) #for grid.arrange

split_plot <- function(x) {

  p1 <- ggplot(x, aes(x = group)) +
    geom_bar(aes(fill = elm), color = "black") +
    geom_label(aes(label = ..count.., color = elm), stat = "count", position = position_stack()) +
    ylim(0, 16) +
    labs(y = NULL, x = NULL) +
    theme_minimal() +
    theme(legend.position = 'none',
          panel.grid = element_blank(),
          legend.title = element_blank(),
          axis.ticks.y = element_blank(),
          axis.text.y = element_blank())

  p2 <- ggplot(distinct(x), aes(x = elm)) +
    geom_bar(aes(fill = group), color = "black") +
    geom_label(aes(label = ..count.., color = group), stat = "count", position = position_stack()) +
    scale_fill_discrete(h.start = 90) +
    scale_color_discrete(h.start = 90) +
    labs(y = NULL, x = NULL) +
    ylim(0, 16) +
    theme_minimal() +
    theme(legend.position = 'none',
          panel.grid = element_blank(),
          legend.title = element_blank(),
          axis.ticks.y = element_blank(),
          axis.text.y = element_blank())

  arrangeGrob(p1, p2, nrow = 1, top = unique(x$list)) 
  }

# Call the function over `singleDF`, split by list and plot each

res <- singleDF %>% 
  split(.$list) %>% 
  map(~split_plot(.x))

# Use grid.arange to draw the grobs 
grid.arrange(grobs = res, nrow = 1)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM