0
votes

My question is an extension of this one but I have used 2 stat_summary calls in my ggplot. The first stat_summary shows the mean of each unit and the second stat_summary shows the overall mean for a group (time) of multiple units (see attached plot). Assigning the colours of the first stat_summary is no problem using colour=factor(unit) and specifying with scale_colour_manual(). I would like to assign colours in the second stat_summary by the 2 levels of time (pre and pst) e.g. colour=time, but i cannot then use scale_colour_manual again to specify colours. I have tried: colour=c("red", "blue"), colour=cbPalette2, aes(color=paste("mean", time))) (as suggested in the related question), but no luck. Any help would be much appreciated.

data:

 structure(list(time = c("pre", "pre", "pre", "pre", "pre", "pre","pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst", "pst"), unit = c(73, 72, 72, 73, 73, 75, 75, 75, 72, 72, 73,73, 75, 75, 72, 74, 71, 74, 74, 71, 71, 74, 74, 71, 72, 73, 72, 72, 73, 73, 72, 73, 72, 75, 75, 75, 75, 75, 71, 71, 71, 74, 74, 71, 74, 71, 70, 66, 68, 66, 68,70, 70, 66, 70, 68, 66, 68, 70, 67, 69, 69, 69, 67, 69, 67, 69, 66, 66, 66, 68, 68, 66, 68, 66, 68, 70, 70, 70, 70, 70, 67, 67, 69, 67, 69, 69, 67, 69, 67, 69, 76, 76, 76, 78, 78, 78, 76, 78, 78, 76, 77, 77, 77, 79, 79, 79, 77, 77, 79, 79, 78, 78, 78, 78, 76, 76, 76, 76, 76, 77, 79, 79, 77, 77, 79, 79, 77, 79), number = c(159, 152, 151, 157, 156, 169, 167, 166, 153, 155, 158, 160, 168, 170, 154, 164, 146, 162, 161, 150, 147, 165, 163, 149, 231, 236, 235, 233, 240, 238, 232, 237, 234, 246, 247, 248, 249, 250, 226, 230, 228, 245, 243, 227, 242, 229, 141, 123, 133, 122, 132, 143, 145, 121, 142, 131, 124, 134, 144, 130, 138, 140, 137, 126, 136, 129, 139, 201, 205, 203, 215, 213, 202, 212, 204, 214, 221, 222, 223, 224, 225, 206, 210, 216, 208, 220, 218, 207, 217, 209, 219, 174, 172, 171, 182, 184, 181, 173, 185, 183, 175, 177, 179, 176, 189, 186, 187, 180, 178, 190, 188, 262, 261, 265, 263, 251, 252, 253, 254, 255, 257, 269, 267, 256, 260, 266, 270, 258, 268), rep = c("A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C"), tied = c("C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "V", "V", "V", "V", "V", "V", "V", "V", "V", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "V", "V", "V", "V", "V", "V", "V", "V", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "V", "V", "V", "V", "V", "V", "V", "V", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "V", "V", "V", "V", "V", "V", "V", "V", "V", "V", "C", "C", "C", "C", "C", "C", "C", "C", "C", "V", "V", "V", "V", "V", "V", "V", "V", "V"), measure = c(0.65, 0.32, 0.29, 0.53, 1.18, 0.62, 0.2, 0.38, 0.64, 0.29,0.45, 0.66, 0.18, 0.74, 0.36, 0.97, 0.27, 0.2, 0.5, 0.34, 0.19, 0.73, 0.58, 0.29, 0.47, 0.17, 0.75, 0.47, 0.37, 0.44, 0.79, 0.37, 0.51, 0.19, 0.28, 0.21, 0.18, 0.2, 0.01, 0.69, 0.66, 0.32, -0.05, 0.69, -0.0899999999999999, 0.52, 0.2, 0.17, 0.21, 0.2, 0.17, 0.31, 0.41, 0.38, 0.21, 0.47, 0.37, 0.29, 0.52, 0.26, 0.47, 0.27, 0.35, 1.14, 1.23, 0.81, 0.56, 0.64, 0.35, 0.43, 0.51, 0.44, 0.84, 0.59, 0.14, 0.55, 0.16, 0.13, 0.16, 0.15, 0.17, 0.31, 0.6, 0.22, 0.57, 0.65, 0.51, 0.77, 0.56, 0.51, 0.52, 0.2, 0.18, 0.03, 0.12, 0.37, 0.14, 0.1, 0.31, 0.16, 0.39, 0.2, 0.27, 0.17, 0.27, 0.14, 0.13, 0.28, 0.5, 0.23, 0.17, 0.05, 0.67, 0.4, 0.24, 0.18, 0.33, 0.22, 0.12, 0.15, 0.0700000000000001, 0, 0.02, 0.39, 0.41, 0.88, 0.28, -0.0600000000000001, -0.01)), .Names = c("time", "unit", "number", "rep", "tied", "measure"), row.names = c(NA,-129L), class = "data.frame")

code:

 cbPalette2 <- c("ghostwhite", "lightsteelblue1", "lightyellow" )
 unitcolours1<-c("purple","gold", "#999999","#009E73","plum1","#E69F00","#56B4E9","black","lightgreen","red","#0072B2","darkolivegreen3","#CC79A7","deeppink4")


 v<-ggplot(stack, aes(x=tied, y=measure, fill=time)) + 

 geom_point(size=0.5, aes(fill=factor(time), colour=factor(unit)), position=position_jitterdodge(jitter.width = NULL, jitter.height = 0, dodge.width = 0.75))+

    stat_summary(mapping=aes(shape=95, fill=time, colour=factor(unit)), fun.y= "mean", geom="point", size=6, position=position_dodge(width=0.75))+

    stat_summary(mapping=aes(shape=95, fill=time) , fun.y= "mean", geom="point", size=16, position=position_dodge(width=0.75))+

 facet_grid(.~rep)+
 labs(fill="time",  x="tied", y="measure (cP)")+
 scale_fill_manual(values=cbPalette2) +
 scale_colour_manual(values=unitcolours1)+
 guides(colour=FALSE)+
 scale_shape_identity() +
 theme_bw()+
 scale_y_continuous(limits=c(-0.1,1.24), breaks=seq(0, 1.2, 0.1))
1
I'm a bit confused about what you're trying to do. You have fill=time in the main call to ggplot, but none of your geoms (which are all geom_point) take a fill aesthetic. Then you repeat fill=time within each stat_summary and you have fill=factor(time) within geom_point. These are unnecessary, if you have fill in the main call to ggplot, and the marker shapes don't take a fill aesthetic anyway. Also, you have shape=95 inside aes. Unless you're mapping shape to a column in your data, shape=95 should be outside aes.eipi10

1 Answers

1
votes

As I noted in my comment, I'm not sure how you want the plot to look, but you can get separate color mappings by using a filled marker (such as shapes 21 - 25) for one geom and a non-filled marker for another. Then you can map a variable to the fill aesthetic for the filled marker and map another variable to the colour aesthetic for the non-filled marker.

Here's an example, using a modified version of your code. This probably isn't quite what you had in mind, so let me know what you were actually trying to do and I'll modify as necessary.

pd = position_dodge(0.75)

ggplot(stack, aes(x=tied, y=measure)) + 
  geom_point(size=1, aes(fill=time), shape=21, colour=NA, 
             position=position_jitterdodge(jitter.width=NULL, jitter.height=0, dodge.width=0.75)) +
  stat_summary(shape=95, fun.y=mean, geom="point", size=12, position=pd) +
  stat_summary(shape=95, aes(colour=factor(unit)), fun.y=mean, geom="point", size=8, position=pd) +
  facet_grid(.~rep) +
  labs(fill="Time", x="tied", y="measure (cP)") +
  # scale_fill_manual(values=cbPalette2) +
  # scale_colour_manual(values=unitcolours1) +
  guides(colour=FALSE) +
  #scale_shape_identity() +
  theme_bw() +
  scale_y_continuous(limits=c(-0.1,1.24), breaks=seq(0, 1.2, 0.1))

enter image description here