R - ggplot2 "error: unexpected symbol in:"

11,755

Can't have spaces in variable names. Wrap them with graves/backticks (`) to make it clear to R that it is a single name.

geom_point(mapping = aes(x = Month, y = `Number of Fatalities`))
Share:
11,755
Davide Lorino
Author by

Davide Lorino

Analyst by day, programmer by night. Sydney, Australia.

Updated on June 12, 2022

Comments

  • Davide Lorino
    Davide Lorino about 2 years

    I'm trying to do what in theory should be the easiest thing ever but i'm making some mistake that I can't even understand.

    I would like to plot $Number of Fatalities by $Month on a scatterplot, (and eventually facet_wrap by $State but that's not where i'm having issues).

    Here is my attempt:

    library(tidyverse)
    
    fatalities <- read.csv(filename.csv)
    
    fatalities = as.data.frame(fatalities)
    
    ggplot(data = fatalities)+
    
        geom_point(mapping = aes(x = Month, y = Number of Fatalities))
    

    ...and here is the output:

    Error: unexpected symbol in: "ggplot(data = fatalities) + geom_point(mapping = aes(x = Month, y = Number of"

    This is the dataset i'm using: https://data.gov.au/dataset/5b530fb8-526e-4fbf-b0f6-aa24e84e4277/resource/0e3771a4-4783-4a12-89ac-b48892bb3ba0/download/bitrearddfatalcrashesfebruary2018.csv

    Your help with this or how I could rephrase the question is greatly appreciated - thank you!

  • Marius
    Marius about 6 years
    It's also probably best to just remove all spaces from column names right after reading in the data- make.names() can do this. Saves having to do this every time you use the column.
  • DuckPyjamas
    DuckPyjamas about 6 years
    Yes indeedy. I use janitor::clean_names(). It makes sure names are unique, handles symbols and accented characters, and can also change capitalisation for easy typing.