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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:42:37+00:00 2026-06-14T13:42:37+00:00

If the column names of a data.frame are started with numbers, or have spaces,

  • 0

If the column names of a data.frame are started with numbers, or have spaces, aes_string() fails to handle them:

foo=data.frame("1st Col"=1:5, "2nd Col"=5:1, check.names=F)
bar=colnames(foo)
ggplot(foo, aes_string(x=bar[1],y=bar[2])) + geom_point()
# Error in parse(text = x) : <text>:1:2: unexpected symbol
# 1: 1st
#     ^

foo=data.frame("First Col"=1:5, "Second Col"=5:1, check.names=F)
bar=colnames(foo)
ggplot(foo, aes_string(x=bar[1],y=bar[2])) + geom_point()
# Error in parse(text = x) : <text>:1:7: unexpected symbol
# 1: First Col
#          ^

foo=data.frame("First_Col"=1:5, "Second_Col"=5:1, check.names=F)
bar=colnames(foo)
ggplot(foo, aes_string(x=bar[1],y=bar[2]))+geom_point()
# Now it works

enter image description here

Is there any way to have spaces in the column names, or they are started with numbers, and we can use them in ggplot2? Please consider we might don’t know the column name, so please avoid to provide examples with constant column names – something like below:

aes_string(x=`1st Col`, y=`2nd Col`)
  • 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-14T13:42:38+00:00Added an answer on June 14, 2026 at 1:42 pm

    As far as I can tell, this method should work programmatically:

    foo=data.frame("1st Col"=1:5, "2nd Col"=5:1, check.names=F)
    
    #Save the colnames
    bar=colnames(foo)
    
    #change the names to something usable
    names(foo) <- c("col1", "col2")
    
    #Plot with arbitrary labs
    ggplot(foo, aes(x=col1, y=col2)) + geom_point()+
      labs(x=bar[1], y=bar[2])
    

    enter image description here

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

Sidebar

Related Questions

Is there any way to preserve spaces in a data.frame's column names when calling
I have a three column workbook with the following data: Col A: Names Col
I have a data frame that's ~ 50,000 X 200. The column names are
I want make changes in big data.frame column names (names(mp)) with the help of
Is there a way to use data.frame without it ruining the column names? I
I want to rename column names in data.frame , > x=data.frame(name=c(n1,n2),sex=c(F,M)) > colnames(x[1])=Name >
Possible Duplicate: R: losing column names when adding rows to an empty data frame
I have a 114 row by 16 column data frame where the rows are
I have a data.frame with entries like: variable importance order 1 foo 0.06977263 1
How to retrieve data from two tables whose column names are the same?

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.