rolf.kemper at renesas.com
2013-Oct-02 18:12 UTC
[R] ggplot2 multiple legends modification (resent witout attachments)
I resent this as I recognized that attachments are not allowed. Dear ALL, please find attached a small test file. (code is copied in this mail too) Basically (as shown) I would like to combine several results in one graph. I could succeed to plot the data correctly, but the legend generation does not fit. For the dfStartPoints and the dfEndpoints I just want to be able to select the color AND (if possible the shape of each one) The legend I'm getting could be modified for its title , but I was not able to to change the label value itself. My final wish is to get 3 Legends (the one for dfXY is fully OK) Legend one (Titled Cell/Area...) is fully OK as it is Legend two should have Title "Start Point"s" color "red" AND the count of data points as value Legend three should have Tile Ënd Points color "green"AND the count of data points as value. I think I can easily get the count , but I have no idea how to modify the related legends in code There are many examples of this (e.g. http://docs.ggplot2.org/current/guide_legend.html ) , but nothing fits really. Hope someone can help on this Rolf ######################## code as attached ############################### require(sqldf) options(gsubfn.engine = "R") require(ggplot2) rm(list=ls(all=TRUE)) maxCells = max(primitiveDensityMap$c) #craete a 100 * 100 grid with random values(cellCount) dfXY <- data.frame(x=rep(0:99, each=100) , y=rep(0:99, each=1) , cellCount=sample(0:99, 10000 , replace=T) ) dfStartPoints <- data.frame(x=sample(10:20, 3 , replace=T) , y=sample(30:40, 3 , replace=T) ) dfEndPoints <- data.frame(x=sample(70:80, 3 , replace=T) , y=sample(90:100, 3 , replace=T) ) nrow(dfXY) #get max and min maxCells = max(dfXY$cellCount) ggplot(dfXY, aes(x=x,y=y)) + geom_tile(aes(fill= cellCount), as= 1) + labs(title="Primitive Cell Density", x="mm", y="mm") + scale_fill_continuous( space= "Lab" , low="white", high="black" , breaks=seq(0,maxCells ,by=round(maxCells/10) ), guide = guide_legend( title "Cells/Area\n(100um^2)" ) ) + coord_fixed() + geom_point(data=dfStartPoints , aes( colour="red" ) ) + labs(color="Start Points" ) + geom_point(data=dfEndPoints , aes( colour="green" ) ) + labs(color="End Points" ) ####################################################################### Rolf Kemper, Manager, Mixed Signal Design, Networking, Renesas Electronics Europe GmbH, , Arcadiastr. 10, 40472, Duesseldorf, Germany, Phone:+49 211 6503-1475, Fax:+49 211 6503-1540, mailto:Rolf.Kemper@renesas.com, http://www.renesas.eu This message is intended only for the use of the address...{{dropped:24}}
David Winsemius
2013-Oct-02 18:57 UTC
[R] ggplot2 multiple legends modification (resent witout attachments)
On Oct 2, 2013, at 11:12 AM, rolf.kemper at renesas.com wrote:> > I resent this as I recognized that attachments are not allowed.Attachments are allowed, but only certain types . To attach data it should be MIME-text and the usual way to get attachments recognized as such jointly by your mail-client and the R mail-server is to name the file with a '.txt' extension. Extensions such as '.csv', '.dat', '.fil', '.doc' will not be forwarded to R-help subscribers. I'm not sure if '.rtf' files pass muster, so I'll just try in this reply: -------------- next part -------------- I wonder if the googlegroups discussion forum for ggplot2 would allow attachmets of whatever type yours was? https://groups.google.com/forum/?fromgroups#!forum/ggplot2> Dear ALL, > > please find attached a small test file. (code is copied in this mail too) > Basically (as shown) I would like to combine several results in one graph. > I could succeed to plot the data correctly, but the legend generation does not fit. > For the dfStartPoints and the dfEndpoints I just want to be able to select the color AND (if possible the shape of each one) > > The legend I'm getting could be modified for its title , but I was not able to to change the label value itself. > > My final wish is to get 3 Legends (the one for dfXY is fully OK) > Legend one (Titled Cell/Area...) is fully OK as it is > Legend two should have Title "Start Point"s" color "red" AND the count of data points as value > Legend three should have Tile ?nd Points color "green"AND the count of data points as value. > > I think I can easily get the count , but I have no idea how to modify the related legends in code > > There are many examples of this (e.g. http://docs.ggplot2.org/current/guide_legend.html ) , but nothing fits really. > > Hope someone can help on this > > Rolf > > ######################## code as attached ############################### > require(sqldf) > options(gsubfn.engine = "R") > require(ggplot2) > > rm(list=ls(all=TRUE))Personally I consider it impolite to place that as uncommented code. I have utility functions that get blown away by that code. It would be more considerate to place it as a comment # ------- To be uncommented to be more reproducible # rm(list=ls(all=TRUE))> maxCells = max(primitiveDensityMap$c) > > #craete a 100 * 100 grid with random values(cellCount) > dfXY <- data.frame(x=rep(0:99, each=100) , y=rep(0:99, each=1) , cellCount=sample(0:99, 10000 , replace=T) ) > > dfStartPoints <- data.frame(x=sample(10:20, 3 , replace=T) , y=sample(30:40, 3 , replace=T) ) > dfEndPoints <- data.frame(x=sample(70:80, 3 , replace=T) , y=sample(90:100, 3 , replace=T) ) > > > > nrow(dfXY) > #get max and min > maxCells = max(dfXY$cellCount) > > ggplot(dfXY, aes(x=x,y=y)) + > geom_tile(aes(fill= cellCount), as= 1) + > labs(title="Primitive Cell Density", x="mm", y="mm") + > scale_fill_continuous( space= "Lab" , low="white", high="black" , breaks=seq(0,maxCells ,by=round(maxCells/10) ), guide = guide_legend( title > "Cells/Area\n(100um^2)" ) ) + > coord_fixed() + > geom_point(data=dfStartPoints , aes( colour="red" ) ) + > labs(color="Start Points" ) + > geom_point(data=dfEndPoints , aes( colour="green" ) ) + > labs(color="End Points" ) > > #######################################################################David Winsemius Alameda, CA, USA