ggplot和scales软件包未正确计算/打印百分比

发布于 2025-02-02 04:48:36 字数 1247 浏览 2 评论 0原文

我在将条形图转换为百分比(Y轴)时遇到了困难,因此尝试使用秤包。它起作用了,但是图表中代表的百分比不正确。我不知道它用来确定百分比的分母。有人知道如何/在哪里找到它并检查或更改它吗?这是我正在使用的代码:

OSU_Pre_Student_Ex$employ <- factor(OSU_Pre_Student_Ex$employ, levels = 
c("Undergraduate student", "Pre-licensure student", "Master's student", "Doctoral student"))

OSU_Pre_Student_Ex$employ<-as.factor(OSU_Pre_Student_Ex$employ)
OSU_Pre_Student_Ex$employ = fct_infreq(OSU_Pre_Student_Ex$employ)

OSU_Pre_Student_Ex %>%
filter(!is.na(employ)) %>%

ggplot(aes(x =employ, fill= employ, drop.na = TRUE)) + 
geom_bar(show.legend = FALSE)+
xlab("Student type") + 
ylab("Percent") +
coord_cartesian(ylim = c(0, 100))+
scale_y_continuous(labels = scales::percent_format(accuracy=1, scale = 1))+

scale_fill_manual(values = BarGraphPalette5, drop = TRUE) + 

theme_minimal(base_family="Calibri")+

theme(panel.grid.major = element_line(size = 0.2), panel.grid.minor = element_line(size= 0.1))+theme(panel.grid.major.x = element_blank(), axis.text.x 
=element_text(size=12),axis.text.y = element_text(size = 12))+
scale_x_discrete(limits = levels(OSU_Pre_Student_Ex$employ))

末端的输出(在视觉上看起来不错,但数字却不好): 在此处输入图像描述

I was having trouble converting my bar chart to percentages (y-axis) and so have tried using the scales package. It worked, but the percentages that it is representing in the chart are not correct. I do not know what denominator it is using to determine the percentages. Does anyone know how/where to find this and check it or change it? Here is the code I'm using:

OSU_Pre_Student_Ex$employ <- factor(OSU_Pre_Student_Ex$employ, levels = 
c("Undergraduate student", "Pre-licensure student", "Master's student", "Doctoral student"))

OSU_Pre_Student_Ex$employ<-as.factor(OSU_Pre_Student_Ex$employ)
OSU_Pre_Student_Ex$employ = fct_infreq(OSU_Pre_Student_Ex$employ)

OSU_Pre_Student_Ex %>%
filter(!is.na(employ)) %>%

ggplot(aes(x =employ, fill= employ, drop.na = TRUE)) + 
geom_bar(show.legend = FALSE)+
xlab("Student type") + 
ylab("Percent") +
coord_cartesian(ylim = c(0, 100))+
scale_y_continuous(labels = scales::percent_format(accuracy=1, scale = 1))+

scale_fill_manual(values = BarGraphPalette5, drop = TRUE) + 

theme_minimal(base_family="Calibri")+

theme(panel.grid.major = element_line(size = 0.2), panel.grid.minor = element_line(size= 0.1))+theme(panel.grid.major.x = element_blank(), axis.text.x 
=element_text(size=12),axis.text.y = element_text(size = 12))+
scale_x_discrete(limits = levels(OSU_Pre_Student_Ex$employ))

The output on my end (which visually looks good but the numbers are not):
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文