R语言 如何在ggplot2中控制哪些值是哪些颜色

9avjhtql  于 2023-11-14  发布在  其他
关注(0)|答案(1)|浏览(116)

我正在创建一个堆叠条形图。数据中的年份被重新编码成组,这些组用于绘制图表。我的图表颜色与图例颜色不匹配,我不明白为什么。图例是正确的,图表上的注解数据值也是正确的,这些数据条以正确的顺序显示,但颜色相反。


的数据

library(dplyr);
library(tidyverse);

library(ggplot2);

# Define UI for application that draws a histogram
ui <- fluidPage(
  
  # Show a plot of the generated distribution
  mainPanel(
    plotOutput("distPlot")
  )
  
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  
  thepast <- paste0(as.character(as.integer(format(Sys.Date(), "%Y"))-2),"-")
  lastyear <- as.character(as.integer(format(Sys.Date(), "%Y"))-1)
  thisyear <- as.character(as.integer(format(Sys.Date(), "%Y")))
  nextyear <- as.character(as.integer(format(Sys.Date(), "%Y"))+1)
  nextyear2 <- as.character(as.integer(format(Sys.Date(), "%Y"))+2)
  future <- paste0(as.character(as.integer(format(Sys.Date(), "%Y"))+3),"+")
  
  bardata <- data.frame(BAR_CHOICES = c('None',thepast,lastyear,thisyear,nextyear),
total_count=c(1530,300,156,393,272)) %>%
    mutate(
      BAR_CHOICES = factor(
        BAR_CHOICES ,
        levels=c( 'None', thepast,lastyear, thisyear, nextyear, nextyear2, future )
      )
    ) %>%
    arrange(
      factor(
        BAR_CHOICES ,                      
        levels=c( 'None', thepast,lastyear, thisyear, nextyear, nextyear2, future )
      )
    )
  
  output$distPlot <- renderPlot({
    ggplot(        
      bardata ,
      aes(x = NA, y = total_count, fill = rev(BAR_CHOICES), label = total_count)) +
      geom_col() +
      scale_fill_manual(values = setNames(c('black','#6f7171','#ff8027','#ffe97a','#006fc1','#dcf5e6','#008331'),c('None', thepast,lastyear, thisyear, nextyear,nextyear2,future))) +
      geom_text(position = position_stack(vjust = 0.5), size = 4.5, color = "black", fontface="bold") +
      guides(fill = guide_legend(reverse=FALSE , nrow=2)) +
      coord_flip() +
      scale_x_discrete(expand = c(0, 0)) +
      scale_y_discrete(expand = c(0, 0)) +
      theme(axis.line=element_blank(),
            axis.text.x=element_blank(),
            axis.text.y=element_blank(),
            axis.ticks=element_blank(),
            axis.title.x=element_blank(),
            axis.title.y=element_blank(),
            legend.position="bottom",
            legend.title=element_blank() ,
            legend.text=element_text(size=12),
            panel.background=element_blank(),
            panel.border=element_blank(),
            panel.grid.major=element_blank(),
            panel.grid.minor=element_blank(),
            plot.background=element_blank()) +
      ggtitle("Estimated Solution Completion")
    
  }, height=150)
  
}

# Run the application
shinyApp(ui = ui, server = server)

字符串

sd2nnvve

sd2nnvve1#

问题是您将rev(BAR_CHOICES)Map到fill上。这样您就恢复了BAR_CHOICES列的顺序,并且将类别分配给了错误的计数。通过在ggplot()之外执行此步骤可以清楚地看到:

bardata |> 
  mutate(
    BAR_CHOICES = rev(BAR_CHOICES)
  )
#>   BAR_CHOICES total_count
#> 1        2024        1530
#> 2        2023         300
#> 3        2022         156
#> 4       2021-         393
#> 5        None         272

字符串
相反,如果你想颠倒BAR_CHOICES的顺序,那么在转换为factor()时颠倒级别的顺序,或者使用forcats::fct_rev()position_stack(reverse=TRUE)颠倒堆栈的顺序:

library(dplyr, warn = FALSE)
library(ggplot2)

bardata <- data.frame(
  BAR_CHOICES = c("None", thepast, lastyear, thisyear, nextyear),
  total_count = c(1530, 300, 156, 393, 272)
) %>%
  mutate(
    BAR_CHOICES = factor(
      BAR_CHOICES,
      levels = rev(
        c("None", thepast, lastyear, thisyear, nextyear, nextyear2, future)
      )
    )
  )

pal_color <- setNames(
  c("black", "#6f7171", "#ff8027", "#ffe97a", "#006fc1", "#dcf5e6", "#008331"),
  c("None", thepast, lastyear, thisyear, nextyear, nextyear2, future)
)

ggplot(
  bardata,
  aes(
    x = NA, y = total_count, fill = BAR_CHOICES,
    label = total_count
  )
) +
  geom_col() +
  scale_fill_manual(values = pal_color) +
  geom_text(
    position = position_stack(vjust = 0.5),
    size = 4.5, color = "white", fontface = "bold"
  ) +
  guides(fill = guide_legend(reverse = TRUE, nrow = 2)) +
  coord_flip() +
  scale_x_discrete(expand = c(0, 0)) +
  scale_y_discrete(expand = c(0, 0)) +
  theme(
    axis.line = element_blank(),
    axis.text.x = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks = element_blank(),
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    legend.position = "bottom",
    legend.title = element_blank(),
    legend.text = element_text(size = 12),
    panel.background = element_blank(),
    panel.border = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    plot.background = element_blank()
  ) +
  ggtitle("Estimated Solution Completion")


的数据

相关问题