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

  • Home
  • SEARCH
  • 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 7511279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:30:16+00:00 2026-05-29T23:30:16+00:00

I want to use a variable for the legend key label in a ggplot

  • 0

I want to use a variable for the legend key label in a ggplot chart that uses two geom_line plots and scale_colour_manual. If I explicitly use strings to define the legend key label, everything works fine, just as per section 6.4.4. in hadley’s book, p.109.

enter image description here

If on the other hand I pop the label in a variable and use the variable in the geom_line and scale_colour_manual calls, nothing happens.

enter image description here

As you can see, plot ‘p1’ works just fine. Plot ‘p2’ does not show the two line plots, although it does show the correct key labels. The code used to generate the two plots is given below. (The background to this request is that I want to pass legend key labels to a wrapper function.)

Q. Why does the approach used in p2 below not work? How can I use variables to define the key labels and to link them to the correct colours in the key labels?

require(ggplot2)
require(lubridate)

set.seed(12345)
# create dummy time series data
monthsback <- 60
startdate <- as.Date(paste(year(now()),month(now()),"1",sep="-")) - months(monthsback)
x <- data.frame(mydate=seq(as.Date(startdate), by="month", length.out=monthsback),
          myval1=runif(monthsback, min=600, max=800), 
          myval2=runif(monthsback, min=400, max=600))
var1 <- "foo-var"
var2 <- "bar-var"

p1 <- ggplot(x, aes( mydate, myval1)) +
    geom_line( aes( x = mydate, y = myval1, colour = "foo"), size = 0.5) +
    geom_line( aes( x = mydate, y = myval2, colour = "bar"), size = 0.5) +
    scale_colour_manual("Legend", values = c("foo" = "red", "bar" = "blue"))
print (p1)

p2 <- ggplot(x, aes( mydate, myval1)) +
    geom_line( aes( x = mydate, y = myval1, colour = var1), size = 0.5) +
    geom_line( aes( x = mydate, y = myval2, colour = var2), size = 0.5) +
    scale_colour_manual("Legend", values = c(var1 = "red", var2 = "blue"))
#print (p2)
  • 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-05-29T23:30:18+00:00Added an answer on May 29, 2026 at 11:30 pm

    I do this a lot, as I use a lot of “standardized” plot options. I’m sure there’s a more elegant way to dig in to the proto object but the result is the same take a look at str(tmp) and you’ll have a better understanding of what is being mapped:

    tmp <-  scale_colour_manual("Legend", values = c(var1 = "red", var2 = "blue"))
    # str(tmp)
    names(tmp$values) <- c(var1,var2)
    
    p2 <- ggplot(x, aes( mydate, myval1)) +
    geom_line( aes( x = mydate, y = myval1, colour = var1), size = 0.5) +
    geom_line( aes( x = mydate, y = myval2, colour = var2), size = 0.5) + tmp
    

    enter image description here

    As an aside, you can save ggplot + ggplot elements to a list and then edit the list items programatically (kind of like what I’ve done above).

    For example:

    z <- list(geom_line( aes( x = mydate, y = myval1, colour = var1), size = 0.5),
    geom_line( aes( x = mydate, y = myval2, colour = var2), size = 0.5),
    tmp)
    
    ggplot(x, aes( mydate, myval1)) + z
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i declare a variable in one class and want to use that variable in
I want to use a variable outside my foreach. how can I solve that
I want to use a variable in a regex, like this: variables = ['variableA','variableB']
I want to use Perl's sprintf to zerofill a variable. sprintf(%08d, $var); But I
I have a transaction log file in CSV format that I want use to
I (i'll try) explain my problem :p I want to use a variable javascript
I want to use variable in MySQL limit, but it is not valid. Why?
I'm designing a declarative language for defining signal networks. I want to use variable
I am actually trying to use a variable that is initialized in a header
I WANT to use NSinteger variable *strength in my code with if condition but

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.