SlowLearner
SlowLearner

Reputation: 8017

R: Using the assigned value of variables in ggplot calls instead of the variable names

I have a ggplot chart that uses faceting and a manual colour assignment. I would like to define the variables outside the call itself so that I don't have to go digging in scale_colour_manual and other areas to change the values used in breaks or values call of scale_colour_manual.

Let's say these are the variables.

var1 <- 'series.a'
var1.label <- "Series A result"
var2 <- 'series.b'
var2.label <- "Series B result"

In the values or breaks I can use something like this:

c('Series A result' = alpha("blue", 0.5),'Series B result' = alpha("red",0.5))

This plots fine, because the code above generates the following output:

Series A result Series B result 
    "#0000FF80"     "#FF000080"

Whereas this code:

c(var1.label = alpha("blue", 0.5), var2.label = alpha("red",0.5))

does not plot because it produces this, in which the names of the elements do not match the values we are using for colour i.e. 'Series A result' and 'Series B result':

 var1.label  var2.label 
"#0000FF80" "#FF000080"

So the question seems to be: how I can get c() to use the values of the variables rather than their names?

I have looked at eval, parse and even force but they don't seem to do what I want. I suspect this is a really simple piece of R but it's not a problem I have yet bumped into. Example code follows if needed.

require(ggplot2)
require(reshape)
require(scales)

# Create data
set.seed(78910)
mydf <- data.frame(
                   mydate = seq(as.Date('2013-01-01'), 
                       as.Date('2013-01-10'), by = 'day'),
                   series.a = runif(10, 100, 200),
                   series.b = runif(10, 2000, 3000))
tail(mydf)
mymelt <- melt(mydf, id.var = 'mydate')

# Define user variables
var1 <- 'series.a'
var1.label <- "Series A result"
var2 <- 'series.b'
var2.label <- "Series B result"

# Which makes creating labels for the faceting easier
mymelt$label.col <- ifelse(mymelt$variable == var1, var1.label, var2.label)
tail(mymelt)

# Plots without problems
ggplot(mymelt, aes(y = value, x = mydate)) +
    geom_line(aes(colour = label.col)) +
    scale_colour_manual("",
                        breaks = c('Series A result','Series B result'),
                        values = c('Series A result' = alpha("blue", 0.5),
                        'Series B result' = alpha("red",0.5))) +
    facet_wrap(~ label.col, ncol = 1, scale = "free_y")

# Does not plot
ggplot(mymelt, aes(y = value, x = mydate)) +
    geom_line(aes(colour = label.col)) +
    scale_colour_manual("",
                        breaks = c(var1.label, var2.label),
                        values = c(var1.label = alpha("blue", 0.5),
                        var2.label = alpha("red",0.5))) +
    facet_wrap(~ label.col, ncol = 1, scale = "free_y")

# Not plotting because names in second case are not correct
c('Series A result' = alpha("blue", 0.5),'Series B result' = alpha("red",0.5))
c(var1.label = alpha("blue", 0.5), var2.label = alpha("red",0.5))

The desired result is this kind of image:

screenshot

Upvotes: 0

Views: 1163

Answers (1)

Matthew Lundberg
Matthew Lundberg

Reputation: 42689

v <- c(alpha("blue", 0.5), alpha("red",0.5))
names(v) <- c(var1.label, var2.label)

> v
## Series A result Series B result 
##     "#0000FF80"     "#FF000080" 

then use values = v in the code.

Upvotes: 2

Related Questions