gpt4 book ai didi

r - ggplot 错误 : similar data graphs, 为什么不再?

转载 作者:行者123 更新时间:2023-12-05 00:02:14 24 4
gpt4 key购买 nike

我正在用 ggplot 绘制一些数据。但是,我不明白我得到的数据与我可以成功绘制的数据略有不同的错误。例如,此数据图表成功:

to_graph <- structure(list(Teacher = c("BS", "BS", "FA"
), Level = structure(c(2L, 1L, 1L), .Label = c("BE", "AE", "ME",
"EE"), class = "factor"), Count = c(2L, 25L, 28L)), .Names = c("Teacher",
"Level", "Count"), row.names = c(NA, 3L), class = "data.frame")

ggplot(data=to_graph, aes(x=Teacher, y=Count, fill=Level), ordered=TRUE) +
geom_bar(aes(fill = Level), position = 'fill') +
scale_y_continuous("",formatter="percent") +
scale_fill_manual(values = c("#FF0000", "#FFFF00","#00CC00", "#0000FF")) +
opts(axis.text.x=theme_text(angle=45)) +
opts(title = "Score Distribution")

但这不会:
to_graph <- structure(list(School = c(84351L, 84384L, 84385L, 84386L, 84387L, 
84388L, 84389L, 84397L, 84398L, 84351L, 84384L, 84385L, 84386L,
84387L, 84388L, 84389L, 84397L, 84398L, 84351L, 84386L), Level = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 3L, 3L), .Label = c("BE", "AE", "ME", "EE"), class = "factor"),
Count = c(3L, 7L, 5L, 4L, 3L, 4L, 4L, 6L, 2L, 116L, 138L,
147L, 83L, 76L, 81L, 83L, 85L, 53L, 1L, 1L)), .Names = c("School",
"Level", "Count"), row.names = c(NA, 20L), class = "data.frame")

ggplot(data=to_graph, aes(x=School, y=Count, fill=Level), ordered=TRUE) +
geom_bar(aes(fill = Level), position = 'fill') +
scale_y_continuous("",formatter="percent") +
scale_fill_manual(values = c("#FF0000", "#FFFF00","#00CC00", "#0000FF")) +
opts(axis.text.x=theme_text(angle=90)) +
opts(title = "Score Distribution")

使用后一个代码,我收到此错误:

stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this. Error in if (!all(data$ymin == 0)) warning("Filling not well defined when ymin != 0") : missing value where TRUE/FALSE needed



有谁知道这里发生了什么?谢谢!

最佳答案

发生错误是因为您的 x 变量具有数值,而实际上您希望它们是离散的,即使用 x=factor(School) .

这样做的原因是stat_bin , geom_bar 的默认统计信息, 将尝试总结 x 的每个唯一值.当您的 x 变量是数字时,它会尝试对范围内的每个整数进行汇总。这显然不是您所需要的。

ggplot(data=to_graph, aes(x=factor(School), y=Count, fill=Level), ordered=TRUE) + 
geom_bar(aes(fill = Level), position='fill') +
opts(axis.text.x=theme_text(angle=90)) +
scale_y_continuous("",formatter="percent") +
opts(title = "Score Distribution") +
scale_fill_manual(values = c("#FF0000", "#FFFF00","#00CC00", "#0000FF"))

enter image description here

关于r - ggplot 错误 : similar data graphs, 为什么不再?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8238918/

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