【问题标题】:How can I get stack bar plot for list of data.frame where keeping or removing duplicated rows?如何获取 data.frame 列表的堆栈条图,其中保留或删除重复的行?
【发布时间】:2017-05-16 18:58:41
【问题描述】:

我有需要按阈值分类的 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 包来实现这一点?请问有什么想法吗?在此先感谢:)

【问题讨论】:

  • arrange(Name, Catg, desc(elm) 会引发错误,因为您的数据中没有 NameCatg 列。可能想检查一下。
  • @rawr 我打错了,现在应该可以了。谢谢你带来这个问题。

标签: r dataframe ggplot2


【解决方案1】:

可能是这样的?:

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)

【讨论】:

  • 亲爱的 Axeman, 感谢您的深刻帮助。有没有机会在单页中获得文件栏的堆栈栏图?是否有可能获得更多的编程解决方案,每个 data.frame 的堆栈条图也包含在单个网格中?非常感谢:)
【解决方案2】:

如果您想对列表的每个元素执行此操作,您可以使用tidyverse 包并将@Axeman 的答案包装到一个函数中。我修改了@Axeman 的代码以获得你想要的外观,虽然我不使用cowplot,所以我替换了gridExtra

编辑:轻松修复以获得您想要的情节,只需 grid.arrangemap 的结果与单行。我还调整了情节以更符合您想要的输出。我使用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)

【讨论】:

  • 抱歉,有一些版本控制错误。修复了它们,它现在应该可以工作了
  • 如何注释文件栏的堆栈条图的观察次数?我将此代码插入您的解决方案geom_text(aes(label=n), position=position_stack(vjust = 0.85)),但不能工作?请问有什么办法吗?
  • 你的代码没有计算n,所以它不会工作。编辑了我的答案以添加标签。
  • 您可以将其清理为您想要的内容并自行尝试主题。那里有很多资源。
  • theme 调用中删除panel.grid。必须为两个绘图之一启用图例,然后将其添加到绘图的底部中间。在 p2 上启用 legend.position = "right" 会给你一个很好的右对齐图例,而且很少大惊小怪。我已经回答完 cmets 中的其他项目。如果您有另一个,请搜索该网站或提出新问题。谢谢。
猜你喜欢
  • 2017-04-09
  • 2017-05-01
  • 1970-01-01
  • 2017-05-13
  • 1970-01-01
  • 2020-09-29
  • 1970-01-01
  • 2021-05-19
  • 2021-08-07
相关资源
最近更新 更多