why isn't scale fill manual working?

4,709 views
Skip to first unread message

j0hnCathey

unread,
Sep 12, 2021, 11:19:49 PM9/12/21
to ggplot2
I just get grey bars. I need the one value red, the others deepskyblue. fill in the geom_bar works ok of course.

ggplot(df0193, aes(abv, samp)) + geom_bar(stat = 'identity') +
        labs(x= "Absorbance value", y ="Samples") +
        scale_fill_manual(values=c("red", 'deepskyblue', 'deepskyblue', 'deepskyblue', 'deepskyblue', 'deepskyblue', 'deepskyblue'))


Thanks in advance.

Tim Richter-Heitmann | Universitaet Bremen

unread,
Sep 13, 2021, 2:05:58 AM9/13/21
to ggp...@googlegroups.com

Hi,

the code you are showing is missing the 'fill' argument within 'aes'
of either the ggplot- or geom-call.
See the examples in your link.
Cheers, Tim


Zitat von j0hnCathey <jtca...@gmail.com>:
> --


--
Dr. Tim Richter-Heitmann

University of Bremen
Microbial Ecophysiology Group (AG Friedrich)
FB02 - Biologie/Chemie
Leobener Straße (NW2 A2130)
D-28359 Bremen
Tel.: 0049(0)421 218-63062
Fax: 0049(0)421 218-63069

j0hnCathey

unread,
Sep 13, 2021, 4:20:14 AM9/13/21
to ggplot2
thanks for response. Still just all blue

ggplot(df0193, aes(abv, samp, fill='abv')) + geom_bar(stat = 'identity') +
        labs(x= "Absorbance value", y ="Samples") +
        scale_fill_manual(values=c("deepskyblue", 'red', 'deepskyblue', 'deepskyblue', 'deepskyblue', 'deepskyblue', 'deepskyblue'))

j0hnCathey

unread,
Sep 13, 2021, 4:21:22 AM9/13/21
to ggplot2
all code:
abv <- c(0.25, 2.45, 0.35, 0.25, 1.0, 0.69, 0.45)
samp <- c("Cut-off", "Positive", "26", "47", '54', '67', '74')

df0193 <- data.frame(samp, abv)
df0193
ggplot(df0193, aes(abv, samp, fill='abv')) + geom_bar(stat = 'identity') +
        labs(x= "Absorbance value", y ="Samples") +
        scale_fill_manual(values=c("deepskyblue", 'red', 'deepskyblue', 'deepskyblue', 'deepskyblue', 'deepskyblue', 'deepskyblue'))


On Monday, September 13, 2021 at 1:05:58 PM UTC+7 trichter wrote:

Tim Richter-Heitmann | Universitaet Bremen

unread,
Sep 13, 2021, 5:32:18 AM9/13/21
to ggp...@googlegroups.com
Hi,

As far as i understood, you just want to have the colors of the bars
in colors you want to manually fix.

this is not done within aes, but outside aes. Fill/color arguments
within aes tells ggplot to apply color palettes to different levels of
a factor.

So, the first curation is:

ggplot(df0193, aes(abv, samp)) + geom_bar(stat = 'identity',
fill=c("deepskyblue", 'red', 'deepskyblue', 'red', 'deepskyblue',
'red', 'deepskyblue')) +
labs(x= "Absorbance value", y ="Samples")

Then, if you feel like the ordering of samp is messed up, you need to
convert samp into a factor,e.g.

samp <- factor(samp, levels=rev(samp), ordered = T)

ggplot(df0193, aes(abv, samp)) + geom_bar(stat = 'identity',
fill=c("deepskyblue", 'red', 'deepskyblue', 'red', 'deepskyblue',
'red', 'deepskyblue')) +
labs(x= "Absorbance value", y ="Samples")

Notice that, with samp now coded as a factor, you can now set the fill
argument within aes:

ggplot(df0193, aes(abv, samp, fill=samp)) + geom_bar(stat = 'identity')+
labs(x= "Absorbance value", y ="Samples") +
scale_fill_manual(values=c("deepskyblue", 'red', 'deepskyblue',
'deepskyblue', 'deepskyblue',
'deepskyblue', 'deepskyblue'))

So, the way you did it first, was not working because samp wasnt a
factor. You can either choose to leave it like that, in which case you
need to give the fill argument outside of aes
OR you could recode samp as a factor, in which case you _can_ set fill
within aes.





Zitat von j0hnCathey <jtca...@gmail.com>:

j0hnCathey

unread,
Sep 13, 2021, 4:52:08 PM9/13/21
to ggplot2
Danke. The 'red' in the color string had to be reordered, but that worked. I was using abv for fill, not samp so I was trying factorizing on abv. Something is different about ggplot2 i think. The graph is printing horizontally (landscape), something that requires coord_flip before, but now adding coord_flip flips it to vertical (portrait) - the abv axis on the vertical. Actually better with abv as x coordinate line.
Reply all
Reply to author
Forward
0 new messages