Reputation: 417
For some reason, I am unable to add a labels to the x axis of my ggplot. I'm using code that has worked in the past, but something isn't working out in this circumstance.
Here's my ggplot:
RussetPlotMrk <-ggplot(mrkRus,aes(x=Rate,y=number)) +
geom_bar(stat="identity", fill="#52854C") +
theme_grey(base_size = 20) +
facet_wrap(~Variety,nrow=2) +
scale_x_discrete(labels=c("125%", "100%", "75%", "50%", "75%50%")) +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
scale_y_continuous(breaks = seq(0,100,by = 10), limits=c(0, 100))
And here's the head of my data:
dput(head(mrkRus))
structure(list(Variety = structure(c(1L, 1L, 1L, 1L, 1L, 3L), .Label = c("Burbank 2018",
"Burbank 2019", "Norkotah 2018", "Norkotah 2019", "Silverton 2018",
"Silverton 2019"), class = "factor"), Rate = c(1L, 2L, 3L, 4L,
5L, 1L), total = c(66.02375756, 62.50788744, 62.55239864, 69.73401417,
66.30070975, 64.552774), mrktotal = c(43.49463647, 38.12657119,
33.94832803, 44.94819267, 40.02596308, 49.07424897), per = structure(c(18L,
16L, 14L, 17L, 15L, 22L), .Label = c("17%", "19%", "21%", "23%",
"28%", "30%", "32%", "34%", "42%", "43%", "47%", "49%", "50%",
"54%", "60%", "61%", "64%", "66%", "70%", "72%", "73%", "76%",
"77%", "78%"), class = "factor"), number = c(66L, 61L, 54L, 64L,
60L, 76L)), row.names = c(NA, 6L), class = "data.frame")
Any ideas as to what I'm doing wrong would be very much appreciated. Thanks in advance for any help!
Upvotes: 2
Views: 489
Reputation: 16178
Because your variable rate are continuous (numeric) values, you need to pass them as a factor format in order to set your labels using scale_x_discrete
:
library(ggplot2)
ggplot(df,aes(x=as.factor(Rate),y=number)) +
geom_bar(stat="identity", fill="#52854C") +
theme_grey(base_size = 20) +
facet_wrap(~Variety,nrow=2) +
scale_x_discrete(labels=c("125%", "100%", "75%", "50%", "75%50%")) +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
scale_y_continuous(breaks = seq(0,100,by = 10), limits=c(0, 100))
Upvotes: 2