ggplot错误:类似的数据图表,为什么不再存在?

时间:2011-11-23 08:08:37

标签: r ggplot2

我用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默认为范围/ 30。使用'binwidth = x'进行调整   这个。 if(!all(data $ ymin == 0))警告出错(“填写不好   在ymin!= 0“时定义:缺少需要TRUE / FALSE的值

任何人都知道这里发生了什么?谢谢!

1 个答案:

答案 0 :(得分:7)

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

原因是stat_bingeom_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