Ggplot error: similar data diagrams, why not?

I draw some data with ggplot. However, I do not understand the error that I get with slightly different data than the data that I can graph successfully. For example, this data completes successfully:

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") 

But this is not so:

 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") 

With the latest code, I get this error:

stat_bin: binwidth The default is range / 30. Use "binwidth = x" to configure this. Error in if (! All (data $ ymin == 0)) warning ("Filling is not sufficiently determined when ymin! = 0"): there is no value that requires TRUE / FALSE

Does anyone know what is going on here? Thanks!

+4
source share
1 answer

The error occurs because your variable x has numerical values, if in fact you want them to be discrete, i.e. use x=factor(School) .

The reason for this is that stat_bin , the default stat for geom_bar , will try to summarize for each unique value of x . When your x-variable is numeric, it tries to sum each integer in the range. This is clearly not what you need.

 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

+7
source

Source: https://habr.com/ru/post/1382655/


All Articles