How to deal with spaces in column names?


#1

Hi,

I could not plot these table below, as there is space at "Number of deliveries" and number on time.

Month Number of deliveries Number On Time Percent
Jan-10 1086 1069 98.4%
Feb-10 1101 1080 98.1%
Mar-10 1116 1089 97.6%
Apr-10 1216 1199 98.6%

I am trying to plot all but I am not sure how should i remove the space
May I know how should I remove or keep the space same as excel table?

my code as below:

> #Read "On-time delivery " excel file
> OnTimeDelivery <- read_excel("Q2.xlsx", range ="OnTimeDeliv!A3:D63")

> #GGplot (facelet) for dealer survey count 
> DeliveryPlot <-ggplot(data = OnTimeDelivery, aes(x = Number$of$deliveries, y = Number$On$Time, fill = Percent)) + geom_bar(stat = "identity", position = "dodge") + geom_text(aes(label=Count),size =4, colour="Blue") + facet_grid(Year ~ Region) + labs(title="Dealer Satisfaction" , x="Scale", y="Count", fill="Region") +theme(plot.title=element_text(hjust=0.5))

I got this error

Error: At least one layer must contain all faceting variables: `Year`.
* Plot is missing `Year`
* Layer 1 is missing `Year`
* Layer 2 is missing `Year`

I tried to use plot function, still cannot plot due to "space" in excel column


#2

Names with spaces can be specified using backticks. So it'll look something like this:

DeliveryPlot <- ggplot(data = OnTimeDelivery, 
                       aes(x = `Number of deliveries`, 
                           y = `Number On Time`, 
                           fill = Percent)) + ...

Keep in mind that your Percent column is likely needs to be formatted before you can use it as fill since it has % in it.


#3

Hi Mish,

I converted but I still cannot plot.

PercentConv = factor(OnTimeDelivery$Percent) #convert percent to factor
#GGplot (facelet) for dealer survey count 
DeliveryPlot <-ggplot(data = OnTimeDelivery, aes(x = `Number of deliveries`, y = `Number On Time`, fill = PercentConv )) + geom_bar(stat = "identity", position = "dodge") + facet_grid(PercentConv ~ `Number On Time`) + labs(title="On Time Delivery" , x="Number of delivery", y="Number on time", fill="PercentConv") +theme(plot.title=element_text(hjust=0.5))

I got this error

Error: At least one layer must contain all faceting variables: `PercentConv`.
* Plot is missing `PercentConv`
* Layer 1 is missing `PercentConv

#4

This is because you're referencing PercentConv, which is a vector existing outside of your data frame, since you have the code below on its own.

PercentConv = factor(OnTimeDelivery$Percent)

The variables you're referencing in your ggplot call are coming from the dataframe in the initial data argument (OnTimeDelivery).


#5

@may - I'll jump in and plug the fantastic clean_names() function from the janitor package. It has some documentation in the package's README.md on GitHub. I teach my students to use this at the outset to clean up variable names in a single swoop. This gets you around having to refer to variables with names wrapped in back ticks.