could not find function "alpha"?

5,008 views
Skip to first unread message

Jim Porzak

unread,
Mar 14, 2012, 12:24:35 PM3/14/12
to ggp...@googlegroups.com
using code from pp96-97 of ggplot2 book, running on 64-bit Win7SP1 with 8GB ram:


R version 2.14.2 (2012-02-29)
Copyright (C) 2012 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-pc-mingw32/x64 (64-bit)
...cut...

> library(ggplot2)  #v 0.9.0
> (unemp <- qplot(date, unemploy, data=economics, geom="line", 
+                 xlab = "", ylab = "No. unemployed (1000s)"))
> presidential <- presidential[-(1:3), ]
> yrng <- range(economics$unemploy)
> xrng <- range(economics$date)
> unemp + geom_vline(aes(xintercept = start), data = presidential)
> unemp + geom_rect(aes(NULL, NULL, xmin = start, xmax = end, fill = party),
+                   ymin = yrng[1], ymax = yrng[2], 
+                   data = presidential) + 
+         scale_fill_manual(values = alpha(c("blue", "red"), 0.2))
Error in scale$palette(n) : could not find function "alpha"

Jean-Olivier Irisson

unread,
Mar 14, 2012, 4:32:50 PM3/14/12
to Jim Porzak, ggplot2
On 2012-Mar-14, at 17:24 , Jim Porzak wrote:
>
> using code from pp96-97 of ggplot2 book, running on 64-bit Win7SP1 with 8GB ram:

the function is now in the scales package. Use

library(scales)
library(ggplot2)

before your code

Jean-Olivier Irisson
---
Observatoire Océanologique
Station Zoologique, B.P. 28, Chemin du Lazaret
06230 Villefranche-sur-Mer
Tel: +33 04 93 76 38 04
Mob: +33 06 21 05 19 90
http://jo.irisson.com/

Dennis Murphy

unread,
Mar 14, 2012, 5:34:40 PM3/14/12
to Jim Porzak, ggp...@googlegroups.com
Hi:

I moved the alpha transparency snippet to geom_rect() rather than
inside scale_fill_manual() and that works:

#library('ggplot2')


(unemp <- qplot(date, unemploy, data=economics, geom="line",

xlab = "", ylab = "No. unemployed (1000s)"))

presidential <- presidential[-(1:3), ]

yrng <- range(economics$unemploy)
xrng <- range(economics$date)
unemp + geom_vline(aes(xintercept = start), data = presidential)
unemp + geom_rect(aes(NULL, NULL, xmin = start, xmax = end, fill = party),

ymin = yrng[1], ymax = yrng[2], alpha = 0.2,
data = presidential) +
scale_fill_manual(values = c("blue", "red"))

Another option is to load the scales package first since the alpha()
function has been moved there; unfortunately, the transparency
rendered is 1 rather than the desired 0.2; try

library('scales')


unemp + geom_rect(aes(NULL, NULL, xmin = start, xmax = end, fill = party),

ymin = yrng[1], ymax = yrng[2],

data = presidential) +


scale_fill_manual(values = alpha(c("blue", "red"), 0.2))

I get the correct rectangles in red and blue, but they overwrite the
line plot in the region where the rectangles are defined with
transparency value 1. I don't know whether the problem is in
scale_fill_manual(), alpha() or their interaction, but it should be
looked at.

To me, the cleaner approach is to set alpha in geom_rect(); in
scale_fill_manual(), the code should work in principle, but I think
something is amiss at present. I'll raise an issue in the scales
package repo so that it will be in the pipeline for consideration.

Dennis

> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

Reply all
Reply to author
Forward
0 new messages