我正在用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")
Run Code Online (Sandbox Code Playgroud)
但这不是:
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")
Run Code Online (Sandbox Code Playgroud)
使用后面的代码,我收到此错误:
stat_bin:binwidth默认为范围/ 30.使用'binwidth = x'来调整它.if(!all(data $ ymin == 0))警告错误("ymin!= 0时填充不明确"):缺少值需要TRUE/FALSE
谁知道这里发生了什么?谢谢!
发生错误是因为您的x变量具有数值,而实际上您希望它们是离散的,即使用x=factor(School)
.
这样做的原因是stat_bin
,默认的stat 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"))
Run Code Online (Sandbox Code Playgroud)