简体   繁体   English

无法使用 scale_x_discrete 向我的 ggplot 的 x 轴添加标签

[英]Unable to add labels to the x axis of my ggplot using scale_x_discrete

For some reason, I am unable to add a labels to the x axis of my ggplot.出于某种原因,我无法在 ggplot 的 x 轴上添加标签。 I'm using code that has worked in the past, but something isn't working out in this circumstance.我正在使用过去有效的代码,但在这种情况下有些东西不起作用。

Here's my ggplot:这是我的 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!在此先感谢您的帮助!

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 :因为您的可变比率是连续(数字)值,您需要将它们作为因子格式传递,以便使用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))

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM