【问题标题】:Change default color of bar plot and legend in ggplot2更改 ggplot2 中条形图和图例的默认颜色
【发布时间】:2021-09-17 01:54:07
【问题描述】:

这是动画 ggplot2 条形图的代码。我想将图例和条形图的默认颜色更改为凉爽的绿蓝色阴影。这里绘图和图例的默认颜色是蓝色。有人可以帮忙吗?

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)

【问题讨论】:

    标签: r ggplot2 plot shiny bar-chart


    【解决方案1】:

    我建议看一下scale_fill_gradient(),在那里你可以设置颜色范围。在下面的示例中,我选择了从蓝色到红色的范围,因为我不确定您所说的“冷绿蓝色调”是什么意思。

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

    【讨论】:

      猜你喜欢
      • 2020-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多