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 to change colour for barplot in ggplot2 using viridis package?

enter image description hereI am trying to make a ggplot with the viridis package (magma option) for colouring the bars. I keep going to the default colour option.

here is my code:

library(ggplot2)
library(viridis)
barplotforraredisease <- data. frame(family = c(" Singleton", "Family of 2", "Family of 3", "Family of 4", "Family of 5"), total members=c(108,52,121,9,2))

p <- ggplot (data=barplotforraredisease, aes (x=family, y=total members))+geom bar(stat = "identity") +geomtext(aes (label=total members), vjust= -0.3, size = 3.5) +theme_minimal() +ggtitle( "Number of families tested")+scale color viridis(discrete = TRUE, option = "viridis")

I’ve attached a screenshot of what my plot looks like, I would appreciate any help.

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

>Solution :

The code you posted has some underscores replaced with spaces.

Once that is fixed, this should work. Note that you need to set the fill value to the column that you want, change scale_color_viridis() to scale_fill_viridis() and set option = "magma".

ggplot(
    data=barplotforraredisease, aes(
        x=family, y=total_members, fill=family)
    ) + 
    geom_bar(stat = "identity") + 
    geom_text(aes(label=total_members), vjust= -0.3, size = 3.5) +
    theme_minimal() + 
    ggtitle( "Number of families tested") + 
    scale_fill_viridis(discrete = TRUE, option = "magma")

enter image description here

Data:

barplotforraredisease  <- structure(list(family = c(" Singleton", "Family of 2", "Family of 3",
"Family of 4", "Family of 5"), total_members = c(108, 52, 121,
9, 2)), class = "data.frame", row.names = c(NA, -5L))
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