Missing aesthetics

Trying to make a stacked bar plot with my data and I this error keeps popping up:
Error: geom_bar requires the following missing aesthetics: x and y
Run rlang::last_error() to see where the error occurred.

My code is:

load("H:\\Personal\\provider1.csv")
d=provider1.csv("provider1.cvs", header=T)
library(ggplot2)
Provider<-d$Provider
Date<- d$Date
Appointments<- d$Appointments
ggplot(d, aes(fill=Date, y=Appointments, x=Provider)) +
	geom_bar(position="fill", stat="identity")

My data is pretty much in the same format as it is on this website:


with the only exception being that, what I have as my 'fill' and 'x' are swapped in position.

Your data needs to be in a dataframe. You have separated Provider, Date and Appointments into vectors. Try again without those three lines.

What does provider1.csv() do? should it be read.csv()? And are you sure the file shouldn't be "provider1.csv", rather than "provider1.cvs"? Assuming they are typos?

You don't need to use the load() function here either, as if you use read.csv() on the correctly spelled file in the second line, this should work.

You also don't need to extract the variables on their own (e.g. Provider <- d$Provider) as ggplot() will find the variables inside the d dataset.

All that said, I'm not sure why you're hitting the error as you are since these aesthetics are being provided to geom_bar() as needed. You might be better using geom_col() rather than geom_bar() and for this you don't need to specify stat = "identity".

Try the following:

library(ggplot2)
d <- read.csv("provider1.csv", header=T)
ggplot(d,aes(x=Provider, y=Appointments, fill=Date)) +
  geom_col(position="fill")

If this doesn't work, check that the d dataset actually has the right variables in it by running names(d).

If you're still having problems, come back to us with the results of sessionInfo(), that'll give us some details about what your system looks like right now (including loaded packages and versions, etc...)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.