gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:39:57 26 4
gpt4 key购买 nike

出于某种原因,我无法在 ggplot 的 x 轴上添加标签。我正在使用过去有效的代码,但在这种情况下有些东西不起作用。

这是我的 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))

这是我的数据的头部:
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")

任何关于我做错了什么的想法都将不胜感激。在此先感谢您的帮助!

最佳答案

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

enter image description here

关于r - 无法使用 scale_x_discrete 向我的 ggplot 的 x 轴添加标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59974949/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com