简体   繁体   English

使用ggplot的R barplot

[英]R barplot using ggplot

I have data of disease and days. 我有疾病和天数的数据。 I have to plot a barplot for each disease and its days of infection. 我必须为每种疾病及其感染天数绘制一个地势图。 I tried it using . 我尝试使用 However, it combines the days for the same disease which l don't want. 但是,它结合了我不想要的相同疾病的天数。 l am interested to plot each column for each day irrespective of disease type. 我有兴趣每天绘制每列而不管疾病类型如何。 l used the following code. l使用了以下代码。

original_datafile <-
structure(list(disease = structure(c(1L, 2L, 
3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 6L, 6L, 6L), 
.Label = c("AA", "BB", "CC", "DD", "EE", "FF"), 
class = "factor"), days = c(5L, 5L, 9L, 2L, 
3L, 4L, 4L, 5L, 7L, 15L, 3L, 7L, 7L, 15L)), 
class = "data.frame", row.names = c(NA, -14L))  


library(ggplot2)

ggplot(data = original_datafile, aes(x = disease, y = days)) + 
  geom_bar(stat = "identity") + 
  theme(axis.text.x = element_text(angle = 40, hjust = 1))

Any suggestion would be appreciated. 任何建议,将不胜感激。

here are a couple solutions that I worked up. 这是我工作的几个解决方案。 Not 100% sure if this is what you're after, but hopefully this should get you close. 不能100%确定这是否是您要追求的目标,但希望这可以使您与您保持联系。 To create a bar for each individual row, rather than combine them, I created a new column called id which just acts as a counter for each row for each disease. 为了为每个单独的行创建一个条,而不是将它们组合在一起,我创建了一个名为id的新列,该列仅用作每种疾病的每一行的计数器。 Then, I included two possible ggplot combinations that I believe get close to what you're after. 然后,我加入了两种可能的ggplot组合,我相信它们会接近您想要的。

original_datafile <-
  structure(list(disease = structure(c(1L, 2L, 
                                       3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 6L, 6L, 6L), 
                                     .Label = c("AA", "BB", "CC", "DD", "EE", "FF"), 
                                     class = "factor"), days = c(5L, 5L, 9L, 2L, 
                                                                 3L, 4L, 4L, 5L, 7L, 15L, 3L, 7L, 7L, 15L)), 
            class = "data.frame", row.names = c(NA, -14L))  


library(ggplot2)

# Modified data file adds an 'id' column to split each row individually.

modified_datafile <- original_datafile %>% 
                        group_by(disease) %>% 
                        mutate(id = row_number())

# Facetted ggplot - each disease has its own block

ggplot(data = modified_datafile, aes(x = id, y = days)) + 
  geom_bar(stat = 'identity', position = 'dodge') + 
  theme(axis.text.x = element_text(angle = 40, hjust = 1)) +
  facet_wrap(. ~ disease, nrow = 2) +
  theme(axis.text.x = element_blank()) +
  labs(x = '', y = 'Days')

# Non facetted ggplot - closer to original, but each row has a bar.

ggplot(data = modified_datafile, aes(x = disease, y = days, group = id)) + 
  geom_bar(stat = 'identity', position = position_dodge2(preserve = 'single')) + 
  theme(axis.text.x = element_text(angle = 40, hjust = 1))

在此处输入图片说明

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM