Rearrange X-axis

Hi! I am trying to rearrange my x-axis in order of date and I want the words instead of numbers; however, it keeps putting it in alphabetical order. My code is as follows:

load("H:\\Personal\\provider1.csv")
d=read.csv("provider1.csv", header=T)
library(ggplot2)
library(dplyr)
library(forcats)

d <- arrange(d,
             Date)
d$Date <- forcats::as_factor(as.character(d$Date,format="%b-%Y"))

totals<- d %>%
	group_by(Date) %>%
	summarise(total=sum(Appointments))
d %>%
	dplyr:mutate(Date = factor(Date,
		levels = c("Jan-20", "Feb-20", "Jun-20", "Jul-20"))) %>%

ggplot(d, aes(fill=Provider, y=Appointments, x=Date)) +
	geom_bar(position="stack", stat="identity") +
	geom_text(data=totals, aes(x=Date, label=total, y=total, fill=NULL), nudge_y=10) + 
	theme(text=element_text(size=18))

I tried to specify the levels argument, but I keep getting an error:
Error in function_list[[i]](value) : object 'dplyr' not found

I am unsure how to solve this, so any help would be great!

Here is a sample of my data:

'data.frame':   32 obs. of  3 variables:
 $ Provider    : chr  "MW, MD" "MW, MD" "MW, MD" "MW, MD" ...
 $ Date        : chr  "Jan-20" "Feb-20" "Jun-20" "Jul-20" ...
 $ Appointments: int  81 69 80 93 80 66 81 63 64 54 ...

Either use dplyr::mutate() or leave out the package specification, which is not required as you have already loaded dplyr, i.e. just use mutate().

Getting rid of the dplyr specification gives me this error:

Error: Mapping should be created with `aes() or `aes_()`.
Run `rlang::last_error()` to see where the error occurred.

That's a separate problem. You have already piped your dataframe d into the ggplot() function, so you need to remove that, i.e. … %>% ggplot(aes(fill=Provider, y=Appointments, x=Date)) + ...

you use the %>% on the way into ggplot, but have left the original d, which is being mistaken for your aes call, because the %>% pipe already put the manipulated d in the data slot for ggplot

Thank you so much for your help!

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