0

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.

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.

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
Gab
  • 1
  • 2
  • please check the `geom_col` examples at the end of the document https://ggplot2.tidyverse.org/reference/geom_bar.html – Roman Jan 22 '19 at 10:40
  • Please provide a reproducible example: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. Make sure that other people can easily read your example data into R. One possibility is using `dput`. – ziggystar Jan 22 '19 at 10:53
  • Looks odd: `aes(x=original_datafile_$`disease`, y=original_datafile_$days))`; should probably be: `aes(x=original_datafile$disease, y=original_datafile$days))` – Chris Ruehlemann Jan 22 '19 at 11:59
  • @ChrisRuehlemann No! Has to be `aes(x=disease, y=days)` – Roman Jan 22 '19 at 14:15

1 Answers1

0

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. 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. Then, I included two possible ggplot combinations that I believe get close to what you're after.

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

enter image description here

enter image description here

Sam
  • 1,353
  • 18
  • 22