0

I am currently trying to create a simple boxplot for my university project and can't seem to figure out what I'm doing wrong.

My current code is as follows:

ggplot(wait_c_long, aes(x='Period', y='Days waited at the 50th percentile')) + geom_point()
wait_c_long = My dataframe

Period = Ordered factor class variable of time periods e.g. '2014-15'...'2018-19'.

Days waited at the 50th percentile = Numerical class variable of average wait times.

Here is a screenshot of my dataset for reference:Dataset Image Whenever I run the previously stated command it results in the following output Output with only a single datapoints and advises me that the variable on the y axis is discrete although it is numeric.

Ideally, I would like to create a simple scatterplot with the geom_point function using the following aes(x='Period',y='Days waited at the 50th percentile', color = 'State', size = 'Admissions') to create a data visualisation from my dataset that I can use in my assignment so any help would be really appreciated.

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • At a guess, I'd say you're referencing your variables' *llabels* not their *names*. Posting a screenshot of your data isn't helpful. We need (for example) output from `dput()`. [This post](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) may be helpful. Oh yes: welcome to SO! – Limey Jun 16 '20 at 08:07
  • 1
    Does it work if you remove the quotation marks in aes() ? – P.Weyh Jun 16 '20 at 08:15
  • 1
    As an addon to @P.Weyh : Remove the quotation marks around Period. Put backticks "`" around your weird var name "Day ... " instead of quotation marks – stefan Jun 16 '20 at 08:30
  • Removing the quotation marks from the Period variable provides 1 point for each time period on the x axis but there is still no points or scale on the y axis. The error suggests that my numerical variables are discrete instead of continuous. – Nicholas Creed Jun 16 '20 at 08:33
  • @stefan you're a god, had no idea about the backticks from everything I've learnt in datacamp. I'll make sure to use shorter variable names in future. – Nicholas Creed Jun 16 '20 at 08:34
  • Thanks for the advice @Limey I will check that out. – Nicholas Creed Jun 16 '20 at 08:59

2 Answers2

0

Normally within aes() you don't quote the name of your dataframe columns, however when they've got spaces, this doesn't work so you need to quote using open single quotes. You end up with the code:

library(ggplot2)

#Create an example dataset in the same format
wait_c_long <- data.frame(Period=1:5, Days=1:5)
names(wait_c_long)[2] <- 'Days waited at the 50th percentile'

#Use no quotes/forward quotes to reference the columns
ggplot(wait_c_long, aes(x=Period, y=`Days waited at the 50th percentile`)) + geom_point()

enter image description here

Miff
  • 7,486
  • 20
  • 20
0

Try to use backticks in place of apostrophe for your variables.

ggplot(wait_c_long, aes(x=`Period`, y=`Days waited at the 50th percentile`)) +
    geom_point()

wait_c_long = My dataframe

Period = Ordered factor class variable of time periods e.g. '2014-15'...'2018-19'.
Aytan
  • 136
  • 1
  • 9