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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:56:14+00:00 2026-06-12T10:56:14+00:00

I know it is preferred if variable names do not have spaces in them.

  • 0

I know it is preferred if variable names do not have spaces in them. I have a situation where I need publication-quality charts, so axes and legends need to have properly formatted labels, ie with spaces. So, for example, in development I might have variables called “Pct.On.OAC” and Age.Group, but in my final plot I need “% on OAC” and “Age Group” to appear:

'data.frame':   22 obs. of  3 variables:
 $ % on OAC           : Factor w/ 11 levels "0","0.1-9.9",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ Age Group          : Factor w/ 2 levels "Aged 80 and over",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ Number of Practices: int  47 5 33 98 287 543 516 222 67 14 ...

But when I try to plot these:

ggplot(dt.m, aes(x=`% on OAC`,y=`Number of Practices`, fill=`Age Group`)) +
    geom_bar()
)

no problem with that. But when I add a facet:

ggplot(dt.m, aes(x=`% on OAC`,y=`Number of Practices`, fill=`Age Group`)) +
    geom_bar() +
    facet_grid(`Age Group`~ .) 

I get Error in[.data.frame(base, names(rows)) : undefined columns selected

If I change Age Group to Age.Group then it works fine, but as I said, I don’t want the dot to appear in the title legend.

So my questions are:

  1. Is there a workaround for the problem with the facet ?
  2. Is there a better general approach to dealing with the problem of spaces (and other characters) in variable names when I want the final plot to include them ? I suppose I can manually overide them, but that seems like a lot of faffing around.
  • 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-12T10:56:15+00:00Added an answer on June 12, 2026 at 10:56 am

    This is a “bug” in the package ggplot2 that comes from the fact that the function as.data.frame() in the internal ggplot2 function quoted_df converts the names to syntactically valid names. These syntactically valid names cannot be found in the original dataframe, hence the error.

    To remind you :

    syntactically valid names consists of letters, numbers and the dot or
    underline characters, and start with a letter or the dot (but the dot
    cannot be followed by a number)

    There’s a reason for that. There’s also a reason why ggplot allows you to set labels using labs, eg using the following dummy dataset with valid names:

    X <-data.frame(
      PonOAC = rep(c('a','b','c','d'),2),
      AgeGroup = rep(c("over 80",'under 80'),each=4),
      NumberofPractices = rpois(8,70)
      ) 
    

    You can use labs at the end to make this code work

    ggplot(X, aes(x=PonOAC,y=NumberofPractices, fill=AgeGroup)) +
      geom_bar() +
      facet_grid(AgeGroup~ .) + 
      labs(x="% on OAC", y="Number of Practices",fill = "Age Group")
    

    To produce

    enter image description here

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

Sidebar

Related Questions

I need to know if a variable in Python is a string or a
I Want to know which one is preferred while coding to use Static Methods
I know you can not set a key value dynamically, but what about the
I know there have been many questions on grid and pack in the past
Question I wish to know if this is a viable way to implement variable
I know email validation is one of those things which is not the funniest
I would like to know if it is acceptable/preferred to use self::method() and parent::method()
I'm pretty new to Scala but I like to know what is the preferred
I am trying to use rsync with python. I have read that the preferred
( Disclaimer: I changed/obfuscated some of the variable/table/column names here for security reasons. Please

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.