Rebecca Rebecca - 2 months ago 6
R Question

Assign gg plot colours differently to those automatically assigned using fill=factor()

I am using ggplot and have created a geom_bar plot with the command fill=factor(Zone) in order to show 3 bars (Zone a,b,c) for each trial (trial 1, 2) on the x axis.

However, this also automatically assigns colours to my bars based on the 'Zone' (i.e. Zone a=red, b=blue, c=green) and not my manual Palette.
How do i keep the x-axis seperated by trial and zone (i.e. exactly as it is), but assign the bar colours manually or according to another parameter?

data:

dput(summarydata)
structure(list(trial = c(1, 1, 1, 2, 2, 2), zone = c("c", "b",
"a", "c", "b", "a"), N = c(77, 77, 77, 9, 9, 9), mean = c(24.339207007839,
24.3293834745274, 51.3314095176336, 4.97411297139253, 61.7131442273329,
33.3127428012746), median = c(24.2125207231389, 24.9084249084249,
50.7692307692308, 5.20833333333333, 67.6056338028169, 30.2083333333333
), sd = c(9.57972648488188, 10.5885530988102, 17.117966513353,
2.96053414557149, 12.2967321565455, 12.5049588941075), se = c(1.0917111525428,
1.20667761501389, 1.95077333167849, 0.986844715190498, 4.09891071884851,
4.16831963136916)), .Names = c("trial", "zone", "N", "mean",
"median", "sd", "se"), row.names = c(NA, -6L), class = "data.frame")


Code:

Palette1<-c("red", "red", "blue", "red", "green", "blue")

ggplot(summarydata, aes(x=trial, y=mean, fill=zone))+
geom_bar(position="dodge",stat="identity") +
scale_x_discrete (labels=c ("Trial 1", "Trial 2"))+
scale_fill_manual (values=Palette1)

Answer

Using the zone variable, you can have one colour per zone. If you want one colour per zone and trial, you have to create a new variable combining trial and zone. For example, here is how to do it.

library(ggplot2)
# Change trial to a factor (maybe it's already a factor in the original data
# and it was lost in the dput pasted above)
summarydata$trial <- factor(summarydata$trial)
# Create a new variable combining trial and zone
summarydata$trialzone <- paste0("Trial",summarydata$trial,
                                summarydata$zone)
Palette1<-c("red", "red", "blue", "red", "green", "blue")
ggplot(summarydata, aes(x=trial, y=mean, fill=trialzone))+
    geom_bar(position="dodge",stat="identity") + 
    scale_x_discrete(labels = c("Trial 1", "Trial 2")) +
    scale_fill_manual(values = Palette1)

plot using summarydata and factor trial

Edit following your comment

Add other variable so that legend colors correspond to red high, blue medium, green low

summarydata$variable <- factor(c("medium", "high", "high",
                                 "medium", "low", "high"),
                               levels = c("high", "medium", "low"),
                               ordered = TRUE)
Palette2 <- c("red","blue","green")

Use group to separate zones having the same fill/color in one trial

ggplot(summarydata, aes(x=trial, y=mean, 
                        fill=variable, group = zone))+
    geom_bar(position="dodge",stat="identity") + 
    scale_x_discrete(labels = c("Trial 1", "Trial 2")) +
    scale_fill_manual(values = Palette2)

highmediumlow