Aqsa Hafeez
Aqsa Hafeez

Reputation: 11

Change default color of bar plot and legend in ggplot2

This is the code for the animated ggplot2 bar graph. I want to change the default color of legend and bar plot into cool green-blue shades. Here the default color of plots and legend is blue.Can someone help, please?

library(ggplot2)
library(shiny)
library(gganimate)
library(gifski)



undergradDATA <- read.csv(file="1-10 Undergraduates.csv", head=TRUE, sep=",")

ui <- fluidPage(
  mainPanel(imageOutput("plot"))
)

server <- function(input, output) {
  output$plot <- renderImage(
    {
      undergrad_plot <- ggplot(data = undergradDATA, aes(x = HEI, y = Undergrad, fill = Undergrad)) +
        
        geom_col(colour = "white")
      undergrad_plot + ggtitle("Ranking of Top 10 Pakistani HEI's w.r.t Undergraduates") +
        theme(
          plot.title = element_text(
            hjust = 0.5,
            colour = "darkolivegreen",
            size = 17,
            family = "mono"
          )
        )
      
      
      
      anim <- undergrad_plot +
        transition_states(Undergrad, wrap = FALSE) +
        shadow_mark() +
        enter_grow() +
        enter_fade()
      
      animate(anim, height = 500, width =600, fps = 5)
      
      
      anim_save("underGradplot.gif") # New
      
      # Return a list containing the filename
      list(src = "underGradplot.gif", contentType = "image/gif")
    },
    deleteFile = TRUE
  )
}

shinyApp(ui, server)

Upvotes: 0

Views: 315

Answers (1)

pascal
pascal

Reputation: 1085

I suggest to have a look at scale_fill_gradient(), where you can set the color range. In the below example I chose a range from blue to red, because I was unsure what you meant with "cool green-blue shades".

undergrad_plot <- ggplot(data = undergradDATA, aes(x = HEI, y = Undergrad, fill = Undergrad)) +
        geom_col() + 
        ggtitle("Ranking of Top 10 Pakistani HEI's w.r.t Undergraduates") +
        scale_fill_gradient(low="blue", high="red") +
        theme(
          plot.title = element_text(
            hjust = 0.5,
            colour = "darkolivegreen",
            size = 17,
            family = "mono"
          )
        )

Upvotes: 1

Related Questions