Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How how to make a histogram with two colors for each bin?

I have a data frame with the names of articles, number of samples overall for each article, number of responders for a drug and number of non-responders. All togther there are 9 articles:

Articles <- c("Nadeem Riaz", "David Braun","immotion150", "IMVIGOR210", "Alexander Lozano",
             "Van Allen", "Alexandra Pender", "David Lui", "Jae Cho")
Samples_number <- c(49, 311, 247, 298, 47, 39, 82, 121, 16)
With_Benefit <- c(26, 89, 168, 131, 27, 13,17,65, 5)
No_Benefit <- c(13, 102, 79, 167, 20, 26, 65, 56, 11)

MyData <- data.frame(Articles, Samples_number, With_Benefit, No_Benefit)

I need to make a histogram with the Articles Names on the x axis, the overall samples number on the y axis, and color each bin so that for example responders would be blue and non-responders red, for each article.

I built a histogram I just don’t know what to type in the fill segment: (here I just typed the No_Benefit column but I know it’s a wrong fill)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

myplot <- ggplot(MyData, aes(x = Articles, y = Samples_number, fill= No_Benefit)) + theme_bw() + geom_col(position = "stack")
print(myplot)

Thank you.

>Solution :

I believe the main problem is with the format of the dataframe; your data is in ‘wide’ format, but ggplot2 works a lot better with ‘long’ format. You can pivot your dataframe from ‘wide’ to ‘long’ with the pivot_longer() function from the tidyr package, e.g.

library(ggplot2)
library(tidyr)

Articles <- c("Nadeem Riaz", "David Braun","immotion150", "IMVIGOR210", "Alexander Lozano",
              "Van Allen", "Alexandra Pender", "David Lui", "Jae Cho")
Samples_number <- c(49, 311, 247, 298, 47, 39, 82, 121, 16)
With_Benefit <- c(26, 89, 168, 131, 27, 13,17,65, 5)
No_Benefit <- c(13, 102, 79, 167, 20, 26, 65, 56, 11)

MyData <- data.frame(Articles, Samples_number, With_Benefit, No_Benefit)

MyData_long <- pivot_longer(MyData, -c(Articles, Samples_number), names_to = "response")

ggplot(MyData_long, aes(x = Articles, y = Samples_number, fill= response)) +
  theme_bw() +
  geom_col(position = "stack")

Created on 2022-02-09 by the reprex package (v2.0.1)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading