简体   繁体   中英

Ggplot scale_fill_manual with dynamic returned values

I have a dataframe with multiple columns of factor type (with 5 levels: Dark Green, Green, Orange, Yellow, Grey) although each contains different values as per the screenshot below.

在此处输入图像描述

I tried to define a function to make a bar chart with colors of the legend manually set by the values contained in the selected column like so

library(tidyverse)
make_graph <- function(df, col){
  data = select(df, group, col)
  G = data$group
  C = data$col
  tbl = prop.table(table(G, C))
  DF = data.frame(tbl) %>% filter(freq != 0)
ggplot(data, aes(x=G,y=Freq, fill=C)) +
 geom_bar(stat="identity", position="dodge") + theme_minimal() +
 scale_fill_manual(values=c(?????), labels=c(?????))  # needs to be automatically filled with the values contained in the selected column

My question is: how can I set the parameters values and labels dynamically depending on the column provided to the function make_graph ? For example, if I do this make_graph(df, 'col_2') it will display a graph of 3 bars with colors dark green, green and orange and values of legend are 'great', 'good' and 'ok'

PS: below is the result of the code provided by akrun where the legend values were not aligned with the colors (for example darkgreen should be labelled as 'great' instead of 'terrible')

在此处输入图像描述

library(dplyr)
library(ggplot2)
make_graph <- function(df, col){
 keyval <- setNames(c("great", "good", "ok", "poor", "terrible"), 
      c("dark green", "green", "orange", "yellow", "grey"))
 fill_m <- keyval[df[[col]]]
df %>%
   select(group, all_of(col)) %>%
   count(across(everything())) %>%
   mutate(prop = n/sum(n)) %>%
   filter(n != 0) %>%
   ggplot(aes(x = group, y = n, fill = .data[[col]])) +
     geom_bar(stat= "identity", position = "dodge") + 
     theme_minimal() +
     scale_fill_manual(values = names(fill_m), labels = fill_m)



}

-testing

make_graph(df1, "col_2")

data

df1 <- structure(list(group = structure(c(1L, 2L, 1L, 2L), levels = c("1", 
"2"), class = "factor"), col_1 = c("dark green", "dark green", 
"dark green", "dark green"), col_2 = c("dark green", "green", 
"orange", "orange"), col_3 = c("green", "yellow", "dark green", 
"grey"), col_4 = c("grey", "dark green", "orange", "yellow"), 
    col_5 = c("dark green", "grey", "grey", "grey")), row.names = c(NA, 
-4L), class = c("tbl_df", "tbl", "data.frame"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM