Modifying the colors with commands scale_fill_manual() and scal_fill_discrete() on a ggplot2 histogram

Carlos Jimenez

My data frame (df) consists of two columns, one with random values (continuous measurements) and another with three levels (c1, c2, c3):

df <- data.frame(col1= round(rnorm(150),2),col2 =       c(rep("c1",times=50),rep("c2",times=50),rep("c3",times=50)))

My ggplot code looks like that:

library(ggplot2)

ggplot(df) +

geom_histogram(aes(x = col1, fill = col2),
             position = "stack",col="black", bins = 30, alpha = 0.6) +

scale_fill_manual(values = c("steelblue1", "yellow", "darkolivegreen2"),breaks = c("c1", "c2", "c3")) +

scale_fill_discrete("Factor", labels = c(expression(italic("c1")), 
                                         expression(italic("c2")),
                                         expression(italic("c3")))) +

theme_classic()

The colours that I define in scale_fill_manual() do not actually correspond to the colors in the histogram. It seems that it is an issue with the two arguments scale_fill_manual() and scale_fill_discrete() as it appears this message:

Scale for fill is already present. Adding another scale for fill, which will replace the existing scale.

How can I specify the colors for each level?

I've tried with the possible solutions that are already in here but nothing happens.

stefan

In vanilla ggplot2 you can have only one scale per aesthetic, i.e. adding the scale_fill_discrete will overwrite (or drop) the scale_fill_manual. But you could simply set your labels and or names via scale_fill_manual:

Note: If you just want to change the font face of the legend labels there is no need to use expression. You could do so using + theme(legend.text = element_text(face = "italic")).

set.seed(123)

df <- data.frame(
  col1 = round(rnorm(150), 2),
  col2 = c(rep("c1", times = 50), rep("c2", times = 50), rep("c3", times = 50))
)

library(ggplot2)

ggplot(df) +
  geom_histogram(aes(x = col1, fill = col2),
    position = "stack", col = "black", bins = 30, alpha = 0.6
  ) +
  scale_fill_manual(
    values = c("steelblue1", "yellow", "darkolivegreen2"),
    breaks = c("c1", "c2", "c3"),
    labels = c(
      expression(italic("c1")),
      expression(italic("c2")),
      expression(italic("c3"))
    ),
    name = "Factor"
  ) +
  theme_classic()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Manually colouring plots with `scale_fill_manual` in ggplot2 not working

Using Multiple scale_fill_manual in ggplot2

Manually assigning colors with scale_fill_manual only works for certain hexagon sizes

Using ggplot2 and viridis, fill histogram based on other variable

ggplot `scale_fill_manual()` alternate colors infinitely

ggplot not respectig order of colours in scale_fill_manual()?

scale_fill_manual() will not change colors on legend?

ggplot2 scale_fill_gradient() function not changing point colors R

R: ggplot2 histogram fill dodge - prevent partial overlap

R: how to get the character representation of a ScaleDiscrete object derived from a scale_fill_manual() call to resolve correctly in ggplot2

Custom reorder histogram fill categories in ggplot2

R ggplot scale_fill_manual using colors for values within a range

ggplot scale_fill_manual within groups

scale_fill_manual based on another factor in ggplot2

Change colors in ggplot2 to manual colors

ggplot2 continuous colors for discrete scale and delete a legend

scale_fill_manual with contradicting results

recreate scale_fill_brewer with scale_fill_manual

Display color in ggplot 2 with scale_fill_manual and scale_fill_discrete in stacked bar graph

Always use the same fill colors barplot ggplot2 in R

ggplot2 - opposite colors for fill without recoding

plotly overrules ggplot2's scale_fill_manual's labels

How to use scale_fill_manual to manually define bar plot colors

Ggplot2: Create legend using scale_fill_manual() for geom_rect() and geom_line() in one plot?

ggplot2: scale_fill_manual with symbol and shape

R scale_fill_manual how to indicate three colors

ggplot2 function scale_fill_stepsn colors do not match

How to change bars colors with scale_fill_manual() in R?

ggplot: scale fill manual does not change the colors of the columns