Quantcast

ggplot2 -- scale_colour_manual()

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

ggplot2 -- scale_colour_manual()

Robert Baer
According to Hadley's ggplot book (p. 109), both the graphs below should have a legend, and yet none appears in my hands.  

Any suggestions?  I can't see a typo.  Is there a bug?

library(ggplot2)
data(LakeHuron)
huron = data.frame(year=1875:1972,level=LakeHuron)
p = ggplot(huron, aes(year)) +
  geom_line(aes(y= level - 5), colour = 'blue') +
  geom_line(aes(y= level + 5), colour = 'red')
print(p)

key =  c('below' = "blue", 'above' = "red")
p = p + scale_color_manual("Direction", values = key)
print(p)

> sessionInfo()
R version 2.12.1 (2010-12-16)
Platform: i386-pc-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252  
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods  
[8] base    

other attached packages:
[1] ggplot2_0.8.9 proto_0.3-8   reshape_0.8.4 plyr_1.4    

loaded via a namespace (and not attached):
[1] tools_2.12.1
>
# on Windows XP
> version
               _                            
platform       i386-pc-mingw32              
arch           i386                        
os             mingw32                      
system         i386, mingw32                
status                                      
major          2                            
minor          12.1                        
year           2010                        
month          12                          
day            16                          
svn rev        53855                        
language       R                            
version.string R version 2.12.1 (2010-12-16)
>
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: ggplot2 -- scale_colour_manual()

dicko ahmadou
Hi robert,
It's not a typo, i have the book and the right code is :

huron <- data.frame(year=1875:1972,level=LakeHuron)
p <- ggplot(huron, aes(year)) +
  geom_line(aes(y= level - 5, colour = 'blue')) +
  geom_line(aes(y= level + 5, colour = 'red'))
print(p)
So colour must be inside aes if you want to have the legend. And i think you have to read carefully this part again because the goal of those example in the book is to explain how scale work and specially scale_colour_manual().
to change the default color you can use scale_colour_manual() like this :
ggplot(huron, aes(year)) +
geom_line(aes(y = level - 5, colour = "below")) +
geom_line(aes(y = level + 5, colour = "above")) +
scale_colour_manual("Direction", c("below" = "blue", "above" = "red"))
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: ggplot2 -- scale_colour_manual()

djmuseR
In reply to this post by Robert Baer
Hi:

I get the same result as you did. Perhaps the problem is the failure to
define breaks in the scale code. The issue, of course, is to figure out what
should be the breaks...

One approach is to go back into the data, add variables below and above,
melt the data frame to stack below and above into one variable, and plot.
Why would one want to do that? In order to create a factor variable with
levels lower and upper to pass as an aesthetic in the ggplot() call. The
function melt() from the reshape package is useful for this task. A useful
trick is to define the variable and level names to match the legend titles
you want.

For some reason, melt doesn't like Time-Series objects in a data frame, so I
converted level to numeric.

# library(ggplot2)
huron2 <- huron
huron2$level <- as.numeric(huron2$level)
# Add levels below and above
huron2 <- transform(huron2, below = level - 5, above = level + 5)

# Melt the data to stack below and above. The names go into
# a factor variable named Direction;
# the values are in a new variable called (oddly enough) value
# variable_name is an argument in very recent versions of reshape(2)
huron3 <- melt(huron2, measure = c('below', 'above'), variable_name =
'Direction')
head(huron3)

# Direction is a factor variable, so it makes a nice choice for an
aesthetic:
ggplot(huron3, aes(x = year, y = value, colour = Direction)) +
   ylab('Water level') + geom_line() +
   scale_colour_manual(breaks = levels(huron3$Direction),
                       values = c('blue', 'red'))

With the levels of Direction defined, the breaks are easy to identify, and
we can use values = to specify the desired colors. If you're just learning
ggplot2, try to minimize the amount of work you have to do to specify a
scale (personal experience :)  ggplot2 is easiest when you do the hard work
in arranging the data so that the call to ggplot() Is straightforward. In
other words, it's not just about ggplot2, it's also about plyr and
reshape(2). Keep reading; you'll get there (Chapter 9 :)

I prefer to think of this problem as an evolution of the package. I'll let
Hadley be the judge of whether or not the B word applies.

HTH,
Dennis

On Sun, Jan 30, 2011 at 9:54 AM, Robert Baer <[hidden email]> wrote:

> According to Hadley's ggplot book (p. 109), both the graphs below should
> have a legend, and yet none appears in my hands.
>
> Any suggestions?  I can't see a typo.  Is there a bug?
>
> library(ggplot2)
> data(LakeHuron)
> huron = data.frame(year=1875:1972,level=LakeHuron)
> p = ggplot(huron, aes(year)) +
>  geom_line(aes(y= level - 5), colour = 'blue') +
>  geom_line(aes(y= level + 5), colour = 'red')
> print(p)
>
> key =  c('below' = "blue", 'above' = "red")
> p = p + scale_color_manual("Direction", values = key)
> print(p)
>
> > sessionInfo()
> R version 2.12.1 (2010-12-16)
> Platform: i386-pc-mingw32/i386 (32-bit)
>
> locale:
> [1] LC_COLLATE=English_United States.1252
> [2] LC_CTYPE=English_United States.1252
> [3] LC_MONETARY=English_United States.1252
> [4] LC_NUMERIC=C
> [5] LC_TIME=English_United States.1252
>
> attached base packages:
> [1] grid      stats     graphics  grDevices utils     datasets  methods
> [8] base
>
> other attached packages:
> [1] ggplot2_0.8.9 proto_0.3-8   reshape_0.8.4 plyr_1.4
>
> loaded via a namespace (and not attached):
> [1] tools_2.12.1
> >
> # on Windows XP
> > version
>               _
> platform       i386-pc-mingw32
> arch           i386
> os             mingw32
> system         i386, mingw32
> status
> major          2
> minor          12.1
> year           2010
> month          12
> day            16
> svn rev        53855
> language       R
> version.string R version 2.12.1 (2010-12-16)
> >
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: ggplot2 -- scale_colour_manual()

Robert Baer
Well time to confess ...

The problem is with the learner (me) not the teacher (Hadley).  There is no bug.  I did not reproduce what Hadley wrote, but sorting it out got me a step closer to the top of the mountain.  I had two mistakes in the code I posted: 1) color must be inside the aes() function as Dicko kindly pointed out; and 2) the color argument in  geom_line() must refer to the label of the color ('below' or 'above') and not the color itself.  

I'm on my second time through the book, and it scary how much I didn't get the first time, but yes Dennis, I can hardly wait to get back to Chapter 9. <G>  There will likely be a third read after more every day use.

#  The correct code (& as Hadley actually has it) for those who might care:
library(ggplot2)
data(LakeHuron)
huron = data.frame(year=1875:1972,level=LakeHuron)
p = ggplot(huron, aes(year)) +
  geom_line(aes(y= level - 5, colour = 'below')) +
  geom_line(aes(y= level + 5, colour = 'above')) +
  scale_colour_manual("Direction",
      c("below" = "blue", "above" = "red"))
print(p)
 


I get the same result as you did. Perhaps the problem is the failure to define breaks in the scale code. The issue, of course, is to figure out what should be the breaks...

One approach is to go back into the data, add variables below and above, melt the data frame to stack below and above into one variable, and plot. Why would one want to do that? In order to create a factor variable with levels lower and upper to pass as an aesthetic in the ggplot() call. The function melt() from the reshape package is useful for this task. A useful trick is to define the variable and level names to match the legend titles you want.

For some reason, melt doesn't like Time-Series objects in a data frame, so I converted level to numeric.

# library(ggplot2)
huron2 <- huron
huron2$level <- as.numeric(huron2$level)
# Add levels below and above
huron2 <- transform(huron2, below = level - 5, above = level + 5)

# Melt the data to stack below and above. The names go into
# a factor variable named Direction;
# the values are in a new variable called (oddly enough) value
# variable_name is an argument in very recent versions of reshape(2)
huron3 <- melt(huron2, measure = c('below', 'above'), variable_name = 'Direction')
head(huron3)

# Direction is a factor variable, so it makes a nice choice for an aesthetic:
ggplot(huron3, aes(x = year, y = value, colour = Direction)) +
   ylab('Water level') + geom_line() +
   scale_colour_manual(breaks = levels(huron3$Direction),
                       values = c('blue', 'red'))

With the levels of Direction defined, the breaks are easy to identify, and we can use values = to specify the desired colors. If you're just learning ggplot2, try to minimize the amount of work you have to do to specify a scale (personal experience :)  ggplot2 is easiest when you do the hard work in arranging the data so that the call to ggplot() Is straightforward. In other words, it's not just about ggplot2, it's also about plyr and reshape(2). Keep reading; you'll get there (Chapter 9 :)

I prefer to think of this problem as an evolution of the package. I'll let Hadley be the judge of whether or not the B word applies.

HTH,
Dennis


On Sun, Jan 30, 2011 at 9:54 AM, Robert Baer <[hidden email]> wrote:

  According to Hadley's ggplot book (p. 109), both the graphs below should have a legend, and yet none appears in my hands.

  Any suggestions?  I can't see a typo.  Is there a bug?

  library(ggplot2)
  data(LakeHuron)
  huron = data.frame(year=1875:1972,level=LakeHuron)
  p = ggplot(huron, aes(year)) +
   geom_line(aes(y= level - 5), colour = 'blue') +
   geom_line(aes(y= level + 5), colour = 'red')
  print(p)

  key =  c('below' = "blue", 'above' = "red")
  p = p + scale_color_manual("Direction", values = key)
  print(p)

  > sessionInfo()
  R version 2.12.1 (2010-12-16)
  Platform: i386-pc-mingw32/i386 (32-bit)

  locale:
  [1] LC_COLLATE=English_United States.1252
  [2] LC_CTYPE=English_United States.1252
  [3] LC_MONETARY=English_United States.1252
  [4] LC_NUMERIC=C
  [5] LC_TIME=English_United States.1252

  attached base packages:
  [1] grid      stats     graphics  grDevices utils     datasets  methods
  [8] base

  other attached packages:
  [1] ggplot2_0.8.9 proto_0.3-8   reshape_0.8.4 plyr_1.4

  loaded via a namespace (and not attached):
  [1] tools_2.12.1
  >
  # on Windows XP
  > version
                _
  platform       i386-pc-mingw32
  arch           i386
  os             mingw32
  system         i386, mingw32
  status
  major          2
  minor          12.1
  year           2010
  month          12
  day            16
  svn rev        53855
  language       R
  version.string R version 2.12.1 (2010-12-16)
  >
         [[alternative HTML version deleted]]

  ______________________________________________
  [hidden email] 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.


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Loading...