[R] ggplot2 scale_shape_manual with large numbers instead of shapes

Marian Talbert mtalbert at usgs.gov
Thu Aug 27 23:48:21 CEST 2015


I'm trying to produce a plot with climate data in which colors describe one
aspect of the data (emissions scenario) and numbers rather than shapes show
the model used (there are 36 models for one emissions scenario and 34 for
the other).  I'm trying to use numbers rather than symbols because there are
36 climate models and thus not enough symbols.  Numbering seems more
consistent than some combo of letters and symbols.  I couldn't figure out
how to define my own shapes as numbers 1 to 36 using scale_shape_manual so
I'm adding the numbers with annotate.  The problem is that I'd like a second
legend linking the numbering to the long model names but am having a hard
time with this.  I've created a toy example below to make this more clear.
p1 below was my original plot and I'd like p2 only with the second legend
linking numbers to long model names any suggestions? 

library(ggplot2)


Dat<-data.frame(Temp=c(rnorm(36,0,1),rnorm(36,1.5,1)),Precp=c(rnorm(36,0,1),rnorm(36,1,1)),
     
model=factor(rep(paste("LongModelName",c(letters,1:10),sep="_"),times=2)),
      Emissions=factor(rep(c("RCP 4.5","RCP 8.5"),each=36)))
 EmissionsCol<-c("goldenrod2","red")
 Pquants <- aggregate(Dat$Precp,list(RCP=Dat$Emissions),
               quantile,c(.25,.5,.75),na.rm=TRUE)
 Tquants <- aggregate(Dat$Temp,list(RCP=Dat$Emissions),
               quantile,c(.25,.5,.75),na.rm=TRUE)
 Quants<-data.frame(Emissions=Tquants$RCP,Tmin=Tquants[[2]][,1],
          TMedian=Tquants[[2]][,2],Tmax=Tquants[[2]][,3],
         
Pmin=Pquants[[2]][,1],PMedian=Pquants[[2]][,2],Pmax=Pquants[[2]][,3])

#Original Plot
Labels<-Dat$model
 p1 <- ggplot()+geom_point(Dat,mapping=aes(x=Temp,y=Precp,colour=Emissions),
     size=.1)+
 scale_colour_manual(values=c("#EEB422BE","#FF0000BE"),guide="none")+
  annotate("text", label=Labels, x=Dat$Temp,
y=Dat$Precp,colour=c("#EEB422BE","#FF0000BE")[Dat$Emissions]) +
  guides(fill=guide_legend(reverse=TRUE))+theme(axis.title =
element_text(size = 2)) +
  
geom_segment(data=Quants,mapping=aes(x=Tmin,y=PMedian,xend=Tmax,yend=PMedian),size=2,colour="black")+
 
geom_segment(data=Quants,mapping=aes(x=TMedian,y=Pmin,xend=TMedian,yend=Pmax),size=2,colour="black")+
 
geom_segment(data=Quants,mapping=aes(x=Tmin,y=PMedian,xend=Tmax,yend=PMedian,colour=Emissions),size=1)+
 
geom_segment(data=Quants,mapping=aes(x=TMedian,y=Pmin,xend=TMedian,yend=Pmax,colour=Emissions),size=1)+
 
geom_point(data=Quants,mapping=aes(x=TMedian,y=PMedian,fill=Emissions),size=6,pch=21,colour="black")+
  scale_fill_manual(values=EmissionsCol)
p1

#with numbers instead of model names
Labels<-as.numeric(factor(Dat$model))
 p2<-
ggplot()+geom_point(Dat,mapping=aes(x=Temp,y=Precp,colour=Emissions),size=.1)+
 scale_colour_manual(values=c("#EEB422BE","#FF0000BE"),guide="none")+
  annotate("text", label=Labels, x=Dat$Temp,
y=Dat$Precp,colour=c("#EEB422BE","#FF0000BE")[Dat$Emissions])+
  guides(fill=guide_legend(reverse=TRUE))+theme(axis.title =
element_text(size = 2)) +
 
geom_segment(data=Quants,mapping=aes(x=Tmin,y=PMedian,xend=Tmax,yend=PMedian),size=2,colour="black")+
 
geom_segment(data=Quants,mapping=aes(x=TMedian,y=Pmin,xend=TMedian,yend=Pmax),size=2,colour="black")+
 
geom_segment(data=Quants,mapping=aes(x=Tmin,y=PMedian,xend=Tmax,yend=PMedian,colour=Emissions),size=1)+
 
geom_segment(data=Quants,mapping=aes(x=TMedian,y=Pmin,xend=TMedian,yend=Pmax,colour=Emissions),size=1)+
 
geom_point(data=Quants,mapping=aes(x=TMedian,y=PMedian,fill=Emissions),size=6,pch=21,colour="black")+
  scale_fill_manual(values=EmissionsCol)
  
p2





--
View this message in context: http://r.789695.n4.nabble.com/ggplot2-scale-shape-manual-with-large-numbers-instead-of-shapes-tp4711580.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list