汇总条形图

时间:2018-04-27 11:12:01

标签: r bar-chart

^ 有一个看起来像这样的数据集:

    typestudy   dloop cytb coi  other microsat  SNP
    methods     no  no  no  no  yes no
    methods     yes no  no  no  no  yes
    methods     no  no  no  no  yes no
    methods     no  no  no  no  yes no
    wildcrime   no  no  no  yes no  no
    taxonomy    no  no  no  no  yes no
    methods     yes no  no  no  no  no
    methods     no  no  no  no  yes no
    taxonomy    no  no  no  no  yes no
    wildcrime   yes no  no  no  no  no
    methods     yes no  no  no  no  no
    taxonomy    no  no  no  no  yes yes
    taxonomy    no  no  no  no  yes no

除了有10列是/否对应于其他遗传元素,并且有超过200行。

在Excel中,图形摘要选项为我提供了很好的堆积条形图,但我需要能够在R中重新创建它以符合我的报告的大学标准

    > summary(dframe1$type.of.study)
         methods development                        other 
                          49                            5 
population genetic structure                     taxonomy 
                          91                           86 
              wildlife crime 
                           6 
    > barplot(as.matrix(dframe1))
     There were 11 warnings (use warnings() to see them)
    > warnings()
    Warning messages:
    1: In apply(height, 2L, cumsum) : NAs introduced by coercion
    2: In apply(height, 2L, cumsum) : NAs introduced by coercion
    3: In apply(height, 2L, cumsum) : NAs introduced by coercion
    4: In apply(height, 2L, cumsum) : NAs introduced by coercion
    5: In apply(height, 2L, cumsum) : NAs introduced by coercion
    6: In apply(height, 2L, cumsum) : NAs introduced by coercion
    7: In apply(height, 2L, cumsum) : NAs introduced by coercion
    8: In apply(height, 2L, cumsum) : NAs introduced by coercion
    9: In apply(height, 2L, cumsum) : NAs introduced by coercion
   10: In apply(height, 2L, cumsum) : NAs introduced by coercion
   11: In apply(height, 2L, cumsum) : NAs introduced by coercion

给了我这个

enter image description here

我也设法制作了这个,但找不到我用过的脚本

enter image description here

我的目标与此类似:

它非常可怜,但它已经基于在线资源和其他问题进行了近一周的故障排除,以达到这一点。我无法弄清楚如何计算研究类型,以便他们重新计算以使条形图的高度对应于不同的遗传标记。 我知道这对于stackoverflow的标准来说太过模糊,但是我很绝望,所以如果有人提出任何建议,我会把它留下来

^ (我会尽可能地简洁,但是因为你会看到我对R的流利是残暴的,我不会寻求帮助但是我已经花了DAYS来处理这些数据而且我已经吓呆了,我永远找不到解决方案,没有人可以在我的研究位置寻求帮助。

3 个答案:

答案 0 :(得分:1)

我们可以通过循环遍历每列的table,然后执行barplot

barplot(sapply(df1[-1], function(x) table(factor(x, 
     levels = c("yes", "no")))), col = c("red", "blue"))
legend("topright", legend = c("yes", "no"), fill = c("red", "blue"))

答案 1 :(得分:1)

以防万一你想要一些看起来像例子的东西:

df <- read.table(text = "typestudy   dloop cytb coi  other microsat  SNP
    methods     no  no  no  no  yes no
                 methods     yes no  no  no  no  yes
                 methods     no  no  no  no  yes no
                 methods     no  no  no  no  yes no
                 wildcrime   no  no  no  yes no  no
                 taxonomy    no  no  no  no  yes no
                 methods     yes no  no  no  no  no
                 methods     no  no  no  no  yes no
                 taxonomy    no  no  no  no  yes no
                 wildcrime   yes no  no  no  no  no
                 methods     yes no  no  no  no  no
                 taxonomy    no  no  no  no  yes yes
                 taxonomy    no  no  no  no  yes no", 
                 header = T, stringsAsFactors = F)

library(tidyr)
library(ggplot2)
library(dplyr)
df %>% gather(key = key, value = value, -typestudy) %>% 
  filter(value == "yes") %>% 
  ggplot(aes(x = key, fill = typestudy)) +
  geom_bar() + 
  coord_flip() + 
  theme_minimal() +
  theme(legend.position = "bottom",
        panel.grid.minor = element_blank(),
        panel.grid.major.y = element_blank()) +
  xlab(NULL) +
  ylab(NULL)

答案 2 :(得分:-1)

我不知道你是否只是在yes之后,但是这里有可能使你能够使用no,以防万一你想要通过响应类型的条形图(是/否)。

df %>%
  gather(var, value, -typestudy) %>%
  group_by(typestudy, var, value) %>%
  count() %>%
  filter(value == "yes") %>%
  ggplot(aes(var, n, group = typestudy, fill = typestudy)) +
  geom_bar(stat = "identity") +
  scale_fill_brewer(palette = "Dark2", direction = -1) +
  coord_flip() +
  theme(
    axis.title.x=element_blank(),
    axis.title.y=element_blank(),
    legend.position = "bottom",
    panel.grid.minor = element_blank(),
    panel.grid.major.y = element_blank(),
    legend.title=element_blank())

enter image description here

数据

df <- structure(list(typestudy = c("methods", "methods", "methods", 
"methods", "wildcrime", "taxonomy", "methods", "methods", "taxonomy", 
"wildcrime", "methods", "taxonomy", "taxonomy"), dloop = c("no", 
"yes", "no", "no", "no", "no", "yes", "no", "no", "yes", "yes", 
"no", "no"), cytb = c("no", "no", "no", "no", "no", "no", "no", 
"no", "no", "no", "no", "no", "no"), coi = c("no", "no", "no", 
"no", "no", "no", "no", "no", "no", "no", "no", "no", "no"), 
    other = c("no", "no", "no", "no", "yes", "no", "no", "no", 
    "no", "no", "no", "no", "no"), microsat = c("yes", "no", 
    "yes", "yes", "no", "yes", "no", "yes", "yes", "no", "no", 
    "yes", "yes"), SNP = c("no", "yes", "no", "no", "no", "no", 
    "no", "no", "no", "no", "no", "yes", "no")), .Names = c("typestudy", 
"dloop", "cytb", "coi", "other", "microsat", "SNP"), class = "data.frame", row.names = c(NA, 
-13L))
相关问题