[R] Canadian politcal party colours in ggplot2

John Kane jrkrideau at inbox.com
Fri Apr 5 14:21:13 CEST 2013


Thanks, works just fine now.

I used to always set options(stringsAsFactors=FALSE) but found that since most people don't it was hard to run example code that I found on the net.  Or else I'd forget what I'd done when posting a requruest for help to R-help.  

If I was doing a lot of intensive analysis, as I was then, I'd go back to  set options(stringsAsFactors=FALSE).  


John Kane
Kingston ON Canada


> -----Original Message-----
> From: istazahn at gmail.com
> Sent: Thu, 4 Apr 2013 11:18:53 -0400
> To: jrkrideau at inbox.com
> Subject: Re: [R] Canadian politcal party colours in ggplot2
> 
> Hi John,
> 
> On Thu, Apr 4, 2013 at 8:25 AM, John Kane <jrkrideau at inbox.com> wrote:
>> No wiki was right: That is, the NDP party code #F4A460 is correct.
> 
> Ah, OK, I thought it was wrong because the NDP party color did not
> match the color listed at http://www.ndp.ca/logos.
> 
>> 
>> I have not looked at the ggplot code that closely yet but is there a
>> chance that scale_color_manual(values = fed.party.colors)  is cycling
>> through the vector?
> 
> I don't think so. But I did find that running my example in a new R
> session did not produce the expected results. The reason I think is
> that I always set options(stringsAsFactors=FALSE). Try this example
> and see if it works as expected:
> 
> library(ggplot2)
> library(XML)
> 
> party.info <-
> readHTMLTable("http://en.wikipedia.org/wiki/Wikipedia:WikiProject_Political_parties_and_politicians_in_Canada/list_of_parties")
> fed.party.info <- party.info[[3]]
> fed.party.colors <- as.character(fed.party.info[, 2])
> names(fed.party.colors) <- gsub("^.*\\|", "", fed.party.info[, 4])
> 
> tmp <- data.frame(x=1:15,
>                   y=1:15,
>                   z=rep(c("Canada Party", "NDP", "Socialist"), each=5))
> 
> ggplot(tmp, aes(x=x, y=y)) +
>   geom_point(aes(color=z)) +
>   scale_color_manual(values = fed.party.colors)
> 
> The difference between this and the original version is that I ensure
> that fed.party.colors is a character as opposed to a factor.
> 
>> 
>> At the moment my second shot at the graph has dropped the orignal
>> colours and looks like it's graphing Conservative, 'some funny yellow
>> party", Conservative.  or Blue, Yellow, Blue for the uninitiated.
>> 
>> I've noticed that occasionally RStudio does strange things on my machine
>> and I need to start a new session or switch to gedit to check things.
>> 
>> I have to run now but I'll try this again later and see what happenss.
>> In any case you've given me the colours which is what I had hoped for.
>> 
>> Thanks again.
>> 
>> John Kane
>> Kingston ON Canada
>> 
>> 
>>> -----Original Message-----
>>> From: istazahn at gmail.com
>>> Sent: Thu, 4 Apr 2013 08:10:07 -0400
>>> To: jrkrideau at inbox.com
>>> Subject: Re: [R] Canadian politcal party colours in ggplot2
>>> 
>>> Hey John,
>>> 
>>> Yeah, I guess my blind trust in wikipedia got me in trouble this time.
>>> The wikipedia colors are not the official colors, or maybe they just
>>> contain errors. If you can find another website with a more
>>> authoritative list you could adapt the technique. Or maybe wikipedia
>>> is close enough.
>>> 
>>> Best,
>>> Ista
>>> 
>>> On Thu, Apr 4, 2013 at 8:04 AM, John Kane <jrkrideau at inbox.com> wrote:
>>>> Excellent. I never thought of something like that.   Clearly I need to
>>>> look into the  XML package more closely.
>>>> 
>>>> The peculiar NDP orange comes through just great though for other
>>>> readers,  your example seems to assign the  Canadian Alliance colours
>>>> to
>>>> the NDP.  Quelle horreur!
>>>> 
>>>> John Kane
>>>> Kingston ON Canada
>>>> 
>>>> 
>>>>> -----Original Message-----
>>>>> From: istazahn at gmail.com
>>>>> Sent: Wed, 3 Apr 2013 09:43:25 -0400
>>>>> To: jrkrideau at inbox.com
>>>>> Subject: Re: [R] Canadian politcal party colours in ggplot2
>>>>> 
>>>>> Hi John,
>>>>> 
>>>>> How about this:
>>>>> 
>>>>> library(XML)
>>>>> 
>>>>> party.info <-
>>>>> readHTMLTable("http://en.wikipedia.org/wiki/Wikipedia:WikiProject_Political_parties_and_politicians_in_Canada/list_of_parties")
>>>>> fed.party.info <- party.info[[3]]
>>>>> fed.party.colors <- fed.party.info[, 2]
>>>>> names(fed.party.colors) <- gsub("^.*\\|", "", fed.party.info[, 4])
>>>>> 
>>>>> tmp <- data.frame(x=1:15,
>>>>>                   y=1:15,
>>>>>                   z=factor(rep(c("Canada Party", "NDP", "Socialist"),
>>>>> each=5)))
>>>>> 
>>>>> ggplot(tmp, aes(x=x, y=y)) +
>>>>>   geom_point(aes(color=z)) +
>>>>>   scale_color_manual(values = fed.party.colors)
>>>>> 
>>>>> Best,
>>>>> Ista
>>>>> 
>>>>> On Wed, Apr 3, 2013 at 9:08 AM, John Kane <jrkrideau at inbox.com>
>>>>> wrote:
>>>>>> A stupid question but does anyone know how to express the actual
>>>>>> colours
>>>>>> used by the main Canadian political parties?   I want to do a couple
>>>>>> of
>>>>>> ggplot2 plots and have lines or rectangles that accurately reflect
>>>>>> the
>>>>>> party colours.
>>>>>> 
>>>>>> I can probably play around with RColorBrewer or something to figure
>>>>>> it
>>>>>> out but if some some already has got them  it would save me some
>>>>>> time
>>>>>> especially with the NDP orange.
>>>>>> 
>>>>>> Thanks
>>>>>> 
>>>>>> John Kane
>>>>>> Kingston ON Canada
>>>>>> 
>>>>>> ____________________________________________________________
>>>>>> FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!
>>>>>> 
>>>>>> ______________________________________________
>>>>>> 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!
>>>> Check it out at http://www.inbox.com/marineaquarium
>>>> 
>>>> 
>> 
>> ____________________________________________________________
>> GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at
>> http://www.inbox.com/smileys
>> Works with AIM®, MSN® Messenger, Yahoo!® Messenger, ICQ®, Google Talk™
>> and most webmails
>> 
>>

____________________________________________________________
GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at http://www.inbox.com/smileys
Works with AIM®, MSN® Messenger, Yahoo!® Messenger, ICQ®, Google Talk™ and most webmails



More information about the R-help mailing list