简体   繁体   中英

ggplot manual legend scale_fill_manual for separate color factors

I've searched several threads but have yet to find a solution.

I have a geom_bar plot with 40+ variables. I have created a separate df to tag each variable according to a specific category, and assigned a colour to the category. Across 40+ variables, there are 4 colours/categories included in the plot.

I would like the legend of the plot to show the colours of the categories, not the individual variables. I know I can accomplish this by having the colours/categories in the original df, however, I would like to be able to use the colour/category reference df in many different projects and avoid always having to add columns to the plotting dfs to tag categories and colours.

Here is an example where df is the data plotted, and df_cols is akin to my category/colour df. Ideally the legend would have "A=red, B=Blue, C=orange" and not variable names.

variable = c("abc", "def", "ghi", "jkl","mno", "pqr", "stu")
tag = c("A", "B", "C", "A","B", "A", "B")
colours = as.character(c("red", "blue", "orange", "red", "blue", "red", "blue"))

# Create colour reference df 
df_cols = data.frame(variable, tag, colors = as.character(colours))
cols = df_cols$colors
cols = as.character(cols)
names(cols) = as.character(names(cols))

# Plotting df
df = data.frame(variable, value=c(1:7))

ggplot(df)+
  geom_bar(aes(x=variable, y=value, fill=variable),stat = "identity")+
  scale_fill_manual(values = cols)

Here is a copy of the actual plot that I'm making: 在此处输入图像描述

I think this might be what you are after. I've simplified your code to take advantage of 'tag' as a discrete variable to control the fill colour.

library(ggplot2)


# Plotting df
df <- data.frame(variable = c("abc", "def", "ghi", "jkl","mno", "pqr", "stu"),
                 tag = c("A", "B", "C", "A","B", "A", "B"),
                 value = c(1:7))

As you are plotting values on the y axis you can simplify your geom to geom_col which is designed for this case and avoids the call to stat

ggplot(df)+
  geom_col(aes(x = variable, y = value, fill = tag)) +
  scale_fill_discrete(breaks = c("A", "B", "C"),
                      values = c("red", "blue", "orange"),
                      labels  = c("red", "blue", "orange"),
                      name = "Colour")

Created on 2020-05-20 by the reprex package (v0.3.0)

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