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 8701133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:19:24+00:00 2026-06-13T02:19:24+00:00

I’m trying to use a named character vector to hold a custom color palette,

  • 0

I’m trying to use a named character vector to hold a custom color palette, so I can say, e.g. palette[‘red’] instead of repeating “#dc322f” all over the place.

However, I don’t seem to be able to use an element of that vector as an argument to par() (though it can be used elsewhere).

Here’s an example. It’ll create a graph with green dots, but the par() call fails and the background is white. Note that I can set parameters using the palette vector from within the plot() call:

> palette <- c('#002b36','#dc322f','#859900')
> names(palette) <- c('black','red','green')
> par(bg=palette['red'])                                                                                                                                
Warning message:
In par(bg = palette["red"]) : "bg.red" is not a graphical parameter
> plot(1:10,1:10,col=palette['green'])
> # (White graph with green dots appears) 

When I use a named numeric vector, however, it works:

> palette <- 1:3                                                                                                                                          
> names(palette) <- c('black','red','green')                                                                                                              
> par(bg=palette['red'])                                                                                                                             
> # (no error here -- it worked.)
> plot(1:10,1:10,col=palette['green'])
> # (Red graph with green dots appears)

I am fairly new to R, and it seems that I might be missing something fundamental. Any idea what’s happening here?

  • 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-13T02:19:25+00:00Added an answer on June 13, 2026 at 2:19 am

    Use unname, so that the item passed to par is just the character vector defining the colour, not a named element

    palette <- c('#002b36','#dc322f','#859900')
    names(palette) <- c('black','red','green')
    par(bg=unname(palette['red'])) 
    plot(1:10,1:10,col=palette['green'])
    

    enter image description here

    As to why?

    within par, if all the arguments are character vectors then

    if (all(unlist(lapply(args, is.character)))) 
                args <- as.list(unlist(args))
    

    The way as.list(unlist(args)) works if args is a named character vector is

    args <- list(bg = palette['red'])
    as.list(unlist(args))
    $bg.red
    [1] "#dc322f"
    

    and bg.red is not a valid par.

    if the line were something like

    setNames(as.list(unlist(args, use.names = F)), names(args))
    

    then it might work in some cases (although not if any of the named elements of arg had length >1)

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

Sidebar

Related Questions

I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported

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.