Can't plot variables from a data frame

Hello,

I am trying to plot a variable from a data frame, but I keep getting an unexpected symbol error.
When I scroll back to the top of my R code, when I read my csv I got the output below, and all the variables that have ' ' around it are the ones that I cannot plot, or do anything too in R.

Missing column names filled in: 'X1' [1]Parsed with column specification:
cols(
  X1 = col_double(),
  Date = col_character(),
  AveragePrice = col_double(),
  `Total Volume` = col_double(),
  `4046` = col_double(),
  `4225` = col_double(),
  `4770` = col_double(),
  `Total Bags` = col_double(),
  `Small Bags` = col_double(),
  `Large Bags` = col_double(),
  `XLarge Bags` = col_double(),
  type = col_character(),
  year = col_double(),
  region = col_character()
)

I think the problem is that those column names contain spaces or are composed entirely of numbers. Can you edit the csv file so that spaces in the column names are replaced with _ and every column name starts with a letter? Otherwise, you can use the colnames() function to view and to set the column names

df <- read.csv("MyFile.csv")
colnames(df)
colnames(df) <- c("ID", "Date", "AveragePrice", "Total_Volume", "X4046", ...)

Also, when working with non properly named variables you have to reference them between backticks e.g. `Total Volume`, `4046`

set.seed(1)
library(tidyverse)
df <- tibble(`Total Volume` = rnorm(10),
             `4046` = rnorm(10))
df %>% 
    ggplot(aes(`Total Volume`, `4046`)) + 
    geom_point()

Created on 2019-04-23 by the reprex package (v0.2.1.9000)

1 Like

Thank you! this worked perfectly!