How do I create a legend with ggplot? I think should be getting the FuelTypeNum in the legend. Plot: http://r.789695.n4.nabble.com/file/n4632471/Rplot.jpeg My code is: ggplot(data=tempTable, aes(x=Bands8, y=SubPercent, fill=FuelTypeNum)) + geom_bar(position="stack", stat="identity") + scale_colour_hue('my legend', breaks = levels(tempTable$FuelTypeNum), labels=c('Bio', 'Coal', 'Gas', 'Hydro','Other')) tempTable: FuelTypeNum Bands8 AvailableMW AvailableMWNewFormat SubPercent 1 1 PB0 185351 185.351 4.355864e-03 2 2 PB0 25994753 25994.753 6.108928e-01 3 4 PB0 1369528 1369.528 3.218476e-02 4 5 PB0 14522521 14522.521 3.412883e-01 5 10 PB0 479915 479.915 1.127830e-02 6 2 PB1 48229 48.229 5.736753e-02 7 5 PB1 792473 792.473 9.426325e-01 8 2 PB2 11792402 11792.402 7.914544e-01 9 4 PB2 27187 27.187 1.824672e-03 10 5 PB2 2770769 2770.769 1.859619e-01 11 10 PB2 309303 309.303 2.075906e-02 12 1 PB3 48 0.048 3.983475e-05 13 2 PB3 136844 136.844 1.135656e-01 14 4 PB3 65079 65.079 5.400845e-02 15 5 PB3 793226 793.226 6.582909e-01 16 10 PB3 209781 209.781 1.740953e-01 17 1 PB4 22113 22.113 4.340998e-02 18 2 PB4 57843 57.843 1.135515e-01 19 4 PB4 20315 20.315 3.988033e-02 20 5 PB4 382714 382.714 7.513050e-01 21 10 PB4 26414 26.414 5.185326e-02 22 1 PB5 39173 39.173 3.232381e-02 23 2 PB5 174913 174.913 1.443304e-01 24 4 PB5 103542 103.542 8.543824e-02 25 5 PB5 874764 874.764 7.218162e-01 26 10 PB5 19501 19.501 1.609135e-02 27 1 PB6 4 0.004 4.550429e-06 28 2 PB6 175038 175.038 1.991245e-01 29 4 PB6 91547 91.547 1.041445e-01 30 5 PB6 610371 610.371 6.943625e-01 31 10 PB6 2078 2.078 2.363948e-03 32 1 PB7 36103 36.103 1.109041e-02 33 2 PB7 210681 210.681 6.471866e-02 34 4 PB7 739013 739.013 2.270159e-01 35 5 PB7 2239824 2239.824 6.880469e-01 -- View this message in context: http://r.789695.n4.nabble.com/ggplot-incorrect-legend-tp4632471.html Sent from the R help mailing list archive at Nabble.com.
You are from the jpeg you link as far as I can see.. What exactly is the output that you expect in the legend John Kane Kingston ON Canada> -----Original Message----- > From: james at crosb.ie > Sent: Tue, 5 Jun 2012 20:18:55 -0700 (PDT) > To: r-help at r-project.org > Subject: [R] ggplot incorrect legend > > How do I create a legend with ggplot? > > I think should be getting the FuelTypeNum in the legend. > > Plot: > http://r.789695.n4.nabble.com/file/n4632471/Rplot.jpeg > > My code is: > ggplot(data=tempTable, aes(x=Bands8, y=SubPercent, fill=FuelTypeNum)) + > geom_bar(position="stack", stat="identity") + > scale_colour_hue('my legend', breaks = levels(tempTable$FuelTypeNum), > labels=c('Bio', 'Coal', 'Gas', 'Hydro','Other')) > > > tempTable: > FuelTypeNum Bands8 AvailableMW AvailableMWNewFormat SubPercent > 1 1 PB0 185351 185.351 4.355864e-03 > 2 2 PB0 25994753 25994.753 6.108928e-01 > 3 4 PB0 1369528 1369.528 3.218476e-02 > 4 5 PB0 14522521 14522.521 3.412883e-01 > 5 10 PB0 479915 479.915 1.127830e-02 > 6 2 PB1 48229 48.229 5.736753e-02 > 7 5 PB1 792473 792.473 9.426325e-01 > 8 2 PB2 11792402 11792.402 7.914544e-01 > 9 4 PB2 27187 27.187 1.824672e-03 > 10 5 PB2 2770769 2770.769 1.859619e-01 > 11 10 PB2 309303 309.303 2.075906e-02 > 12 1 PB3 48 0.048 3.983475e-05 > 13 2 PB3 136844 136.844 1.135656e-01 > 14 4 PB3 65079 65.079 5.400845e-02 > 15 5 PB3 793226 793.226 6.582909e-01 > 16 10 PB3 209781 209.781 1.740953e-01 > 17 1 PB4 22113 22.113 4.340998e-02 > 18 2 PB4 57843 57.843 1.135515e-01 > 19 4 PB4 20315 20.315 3.988033e-02 > 20 5 PB4 382714 382.714 7.513050e-01 > 21 10 PB4 26414 26.414 5.185326e-02 > 22 1 PB5 39173 39.173 3.232381e-02 > 23 2 PB5 174913 174.913 1.443304e-01 > 24 4 PB5 103542 103.542 8.543824e-02 > 25 5 PB5 874764 874.764 7.218162e-01 > 26 10 PB5 19501 19.501 1.609135e-02 > 27 1 PB6 4 0.004 4.550429e-06 > 28 2 PB6 175038 175.038 1.991245e-01 > 29 4 PB6 91547 91.547 1.041445e-01 > 30 5 PB6 610371 610.371 6.943625e-01 > 31 10 PB6 2078 2.078 2.363948e-03 > 32 1 PB7 36103 36.103 1.109041e-02 > 33 2 PB7 210681 210.681 6.471866e-02 > 34 4 PB7 739013 739.013 2.270159e-01 > 35 5 PB7 2239824 2239.824 6.880469e-01 > > -- > View this message in context: > http://r.789695.n4.nabble.com/ggplot-incorrect-legend-tp4632471.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > 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.____________________________________________________________ FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family! Visit http://www.inbox.com/photosharing to find out more!
Thank you for your help. I would expect FuelTypeNum to make up the legend. But in the legend there is an extra value of called '3' and in the chart there is an extra FuelTypeNum. code: ggplot(data=tempTable, aes(x=Bands8, y=AvailableMWNewFormat, fill=FuelTypeNum)) + geom_bar(position="stack", stat="identity") + coord_flip() Plot http://r.789695.n4.nabble.com/file/n4632552/Rplot.jpeg Data: http://r.789695.n4.nabble.com/file/n4632552/dataRtest.csv dataRtest.csv -- View this message in context: http://r.789695.n4.nabble.com/ggplot-incorrect-legend-tp4632471p4632552.html Sent from the R help mailing list archive at Nabble.com.
Try making FuelTypeNum into a factor like this: p <- ggplot(data=tempTable, aes(x=Bands8, y=AvailableMWNewFormat, fill=as.factor(FuelTypeNum))) + geom_bar(position="stack", stat="identity")+ coord_flip() p John Kane Kingston ON Canada> -----Original Message----- > From: james at crosb.ie > Sent: Wed, 6 Jun 2012 09:29:24 -0700 (PDT) > To: r-help at r-project.org > Subject: Re: [R] ggplot incorrect legend > > Thank you for your help. > > I would expect FuelTypeNum to make up the legend. But in the legend > there > is an extra value of called '3' and in the chart there is an extra > FuelTypeNum. > > code: > ggplot(data=tempTable, aes(x=Bands8, y=AvailableMWNewFormat, > fill=FuelTypeNum)) + > geom_bar(position="stack", stat="identity") + > coord_flip() > > Plot > http://r.789695.n4.nabble.com/file/n4632552/Rplot.jpeg > > Data: > http://r.789695.n4.nabble.com/file/n4632552/dataRtest.csv dataRtest.csv > > -- > View this message in context: > http://r.789695.n4.nabble.com/ggplot-incorrect-legend-tp4632471p4632552.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > 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.____________________________________________________________ FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!
Thank you, that was very helpful. Would it be possible to rename the legend values as well. Example 1 as Biomass, 2 as coal, 4 as gas, 5 as hydro? ggplot(data=tempTable, aes(x=Bands8, y=AvailableMWNewFormat, fill=as.factor(FuelTypeNum))) + geom_bar(position="stack", stat="identity")+ guides(fill = guide_legend(title = "Fuel Type", title.position = "top"))+ xlab( "Price Bands")+ ylab("Available MW") -- View this message in context: http://r.789695.n4.nabble.com/ggplot-incorrect-legend-tp4632471p4632843.html Sent from the R help mailing list archive at Nabble.com.
library(ggplot2) p <- ggplot(data=tempTable, aes(x=Bands8, y=AvailableMWNewFormat, fill=as.factor(FuelTypeNum))) + geom_bar(position="stack", stat="identity")+ coord_flip() + scale_fill_discrete("test") + scale_fill_manual("FuelTypeNum",values= c("1" = "blue", "2" = "red", "4" ="green", "5"="orange"), breaks=c("1", "2", "4", "5"), labels=c("biomass", "coal", "gas", "hydro")) Hydro? Canadian, eh? By the way, I just stole the solution from the Cookbook for R http://wiki.stdout.org/rcookbook/Graphs/Legends%20%28ggplot2%29/ Here is a slightly different approach using the RColorBrewer package. The colours are a bit less garish than the ones I randomly picked. library(ggplot2) p <- ggplot(data=tempTable, aes(x=Bands8, y=AvailableMWNewFormat, fill=as.factor(FuelTypeNum))) + geom_bar(position="stack", stat="identity")+ coord_flip() + scale_fill_brewer(palette="Set1", name="Energy\nSources", breaks=c("1", "2", "4", "5"), labels=c("biomass", "coal", "gas", "hydro")) p John Kane Kingston ON Canada> -----Original Message----- > From: james at crosb.ie > Sent: Fri, 8 Jun 2012 10:29:11 -0700 (PDT) > To: r-help at r-project.org > Subject: Re: [R] ggplot incorrect legend > > Thank you, that was very helpful. > > Would it be possible to rename the legend values as well. Example 1 as > Biomass, 2 as coal, 4 as gas, 5 as hydro? > > ggplot(data=tempTable, aes(x=Bands8, y=AvailableMWNewFormat, > fill=as.factor(FuelTypeNum))) + > geom_bar(position="stack", stat="identity")+ > guides(fill = guide_legend(title = "Fuel Type", title.position > "top"))+ > xlab( "Price Bands")+ > ylab("Available MW")____________________________________________________________ FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family! Visit http://www.inbox.com/photosharing to find out more!
Possibly Parallel Threads
- Percent of a given subset
- is there an equivalent of prop.table but for counts
- Add columns in a dataframe and fill them from another table according to a criteria
- Fill dataframe from a table according to a criteria
- Fast update of a lot of records in a database?