0

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)
Rebecca
  • 25
  • 1
  • 2
  • 8
  • Can you post the data as well? Here is how to make a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5965451#5965451). – Paul Rougieux Oct 06 '16 at 11:51
  • 1
    Hard to test without you providing example data. But just looking at the code above, you're missing a `+` for the `scale_fill_manual`to be taken into account. – Haboryme Oct 06 '16 at 12:06
  • My basic data added Paul Rougieux31. Thanks Haboryme but the missing + was just a copying error, this is not the problem. – Rebecca Oct 06 '16 at 12:24

2 Answers2

0

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

Paul Rougieux
  • 10,289
  • 4
  • 68
  • 110
  • How would i then combine the legend so that i only have each colour appearing once? – Rebecca Oct 06 '16 at 12:58
  • I don't understand, please give an example of how you want the legend to look like. red trial1a, trial1b, trial2a then blue trial 1c trial2c, ...? – Paul Rougieux Oct 06 '16 at 13:00
  • red high, blue medium, green low....for example. The a,b,c doesn't really matter at this stage, i do not need it labelled, for now i only use it to seperate the bars. – Rebecca Oct 06 '16 at 13:10
  • What is high medium and low? Does this represent another variable not in the dataset? In that case you'd better encode that variable with summarydata$var <- c("high","high","medium","high","low","medium") then use that variable to plot, mapping high, medium low to the corresponding colours in the palette. – Paul Rougieux Oct 06 '16 at 13:15
  • I can do that, and then i get a nice legend, but then i end up with the columns merging again as before – Rebecca Oct 06 '16 at 14:04
  • This is because 2 zones in trial 1 have the same fill/colour. Add `group = zone` in the aesthetic as I explained in the answer. – Paul Rougieux Oct 06 '16 at 14:25
  • YES! this is what i've been after. Thank you soo much. – Rebecca Oct 06 '16 at 15:00
0

If I use fill=Palette1, then I get your desired output:

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

output

shosaco
  • 5,915
  • 1
  • 30
  • 48