Marian Talbert
2015-Aug-27 21:48 UTC
[R] ggplot2 scale_shape_manual with large numbers instead of shapes
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.
Hadley Wickham
2015-Aug-27 21:59 UTC
[R] ggplot2 scale_shape_manual with large numbers instead of shapes
Something like this? df <- data.frame( x = runif(30), y = runif(30), z = factor(1:30) ) ggplot(df, aes(x, y)) + geom_point(aes(shape = z), size = 5) + scale_shape_manual(values = c(letters, 0:9)) Hadley On Thu, Aug 27, 2015 at 4:48 PM, Marian Talbert <mtalbert at usgs.gov> wrote:> 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. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- http://had.co.nz/
Marian Talbert
2015-Aug-27 21:59 UTC
[R] ggplot2 scale_shape_manual with large numbers instead of shapes
Not exactly I was trying to only numbers for symbols instead of a mix of letters and numbers just to be consistent. I'm pretty sure someone will nag me if I use both letters and numbers as symbols -- View this message in context: http://r.789695.n4.nabble.com/ggplot2-scale-shape-manual-with-large-numbers-instead-of-shapes-tp4711580p4711582.html Sent from the R help mailing list archive at Nabble.com.