Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9161521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:59:06+00:00 2026-06-17T13:59:06+00:00

I have a ggplot chart that uses faceting and a manual colour assignment. I

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:59:07+00:00Added an answer on June 17, 2026 at 1:59 pm
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ggplot graph that I would like to insert custom string below
I have a dataframe that I would like to plot in a similar way
I have an ggplot2 point chart created like this qplot(Index, Popularity ,data = data.slopeone.agg)
I have this ggplot ggplot(dt.1, aes(x=pctOAC,y=NoP, fill=Age)) + geom_bar(stat=identity,position=position_dodge()) + geom_smooth(aes(x=pctOAC,y=NoP, colour=Age), se=F, method=loess,show_guide
I have a series of ggplot graphs that I'm repeating with a few small
I have generated a figure that combines ggplot and base graphics: t <- c(1:(24*14))
I have a code to plot multiple variables and factor in ggplot. I wanted
I want to make a radial stacked barchart. I have something like this: ggplot(diamonds,
I have the following problem: I would like to visualize a discrete and a
I have a dataframe like the one below: obj date colour measurement A 01

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.