Re: [R] ggplot incorrect legend

2012-06-09 Thread John Kane
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: ja...@crosb.ie
> Sent: Fri, 8 Jun 2012 10:29:11 -0700 (PDT)
> To: r-help@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!

__
R-help@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.


Re: [R] ggplot incorrect legend

2012-06-08 Thread jcrosbie
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.

__
R-help@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.


Re: [R] ggplot incorrect legend

2012-06-06 Thread John Kane
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: ja...@crosb.ie
> Sent: Wed, 6 Jun 2012 09:29:24 -0700 (PDT)
> To: r-help@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@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!

__
R-help@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.


Re: [R] ggplot incorrect legend

2012-06-06 Thread jcrosbie
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@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.


Re: [R] ggplot incorrect legend

2012-06-06 Thread jcrosbie
I'm expecting the legend to be 1,2,4,5,10 not 2,4,6,8,10. 

Is there away I can set my own colour and legend tittles? 

--
View this message in context: 
http://r.789695.n4.nabble.com/ggplot-incorrect-legend-tp4632471p4632529.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] ggplot incorrect legend

2012-06-06 Thread John Kane
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: ja...@crosb.ie
> Sent: Tue, 5 Jun 2012 20:18:55 -0700 (PDT)
> To: r-help@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
> 11PB0  185351  185.351 4.355864e-03
> 22PB02599475325994.753 6.108928e-01
> 34PB0 1369528 1369.528 3.218476e-02
> 45PB01452252114522.521 3.412883e-01
> 5   10PB0  479915  479.915 1.127830e-02
> 62PB1   48229   48.229 5.736753e-02
> 75PB1  792473  792.473 9.426325e-01
> 82PB21179240211792.402 7.914544e-01
> 94PB2   27187   27.187 1.824672e-03
> 10   5PB2 2770769 2770.769 1.859619e-01
> 11  10PB2  309303  309.303 2.075906e-02
> 12   1PB3  480.048 3.983475e-05
> 13   2PB3  136844  136.844 1.135656e-01
> 14   4PB3   65079   65.079 5.400845e-02
> 15   5PB3  793226  793.226 6.582909e-01
> 16  10PB3  209781  209.781 1.740953e-01
> 17   1PB4   22113   22.113 4.340998e-02
> 18   2PB4   57843   57.843 1.135515e-01
> 19   4PB4   20315   20.315 3.988033e-02
> 20   5PB4  382714  382.714 7.513050e-01
> 21  10PB4   26414   26.414 5.185326e-02
> 22   1PB5   39173   39.173 3.232381e-02
> 23   2PB5  174913  174.913 1.443304e-01
> 24   4PB5  103542  103.542 8.543824e-02
> 25   5PB5  874764  874.764 7.218162e-01
> 26  10PB5   19501   19.501 1.609135e-02
> 27   1PB6   40.004 4.550429e-06
> 28   2PB6  175038  175.038 1.991245e-01
> 29   4PB6   91547   91.547 1.041445e-01
> 30   5PB6  610371  610.371 6.943625e-01
> 31  10PB620782.078 2.363948e-03
> 32   1PB7   36103   36.103 1.109041e-02
> 33   2PB7  210681  210.681 6.471866e-02
> 34   4PB7  739013  739.013 2.270159e-01
> 35   5PB7 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@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!

__
R-help@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.


[R] ggplot incorrect legend

2012-06-05 Thread jcrosbie
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
11PB0  185351  185.351 4.355864e-03
22PB02599475325994.753 6.108928e-01
34PB0 1369528 1369.528 3.218476e-02
45PB01452252114522.521 3.412883e-01
5   10PB0  479915  479.915 1.127830e-02
62PB1   48229   48.229 5.736753e-02
75PB1  792473  792.473 9.426325e-01
82PB21179240211792.402 7.914544e-01
94PB2   27187   27.187 1.824672e-03
10   5PB2 2770769 2770.769 1.859619e-01
11  10PB2  309303  309.303 2.075906e-02
12   1PB3  480.048 3.983475e-05
13   2PB3  136844  136.844 1.135656e-01
14   4PB3   65079   65.079 5.400845e-02
15   5PB3  793226  793.226 6.582909e-01
16  10PB3  209781  209.781 1.740953e-01
17   1PB4   22113   22.113 4.340998e-02
18   2PB4   57843   57.843 1.135515e-01
19   4PB4   20315   20.315 3.988033e-02
20   5PB4  382714  382.714 7.513050e-01
21  10PB4   26414   26.414 5.185326e-02
22   1PB5   39173   39.173 3.232381e-02
23   2PB5  174913  174.913 1.443304e-01
24   4PB5  103542  103.542 8.543824e-02
25   5PB5  874764  874.764 7.218162e-01
26  10PB5   19501   19.501 1.609135e-02
27   1PB6   40.004 4.550429e-06
28   2PB6  175038  175.038 1.991245e-01
29   4PB6   91547   91.547 1.041445e-01
30   5PB6  610371  610.371 6.943625e-01
31  10PB620782.078 2.363948e-03
32   1PB7   36103   36.103 1.109041e-02
33   2PB7  210681  210.681 6.471866e-02
34   4PB7  739013  739.013 2.270159e-01
35   5PB7 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@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.