0

I am working on a script to generate a statistical report for each school who has participated in a test/survey.

For that purpose i want to be able to automatically adjust the limits of x- and y axis in graphs to the specific data of each school.

For example, i would like the percentage range to be displayed 5% below and 5% above the specific data available.

As an example i have the following graph:

library(ggplot2)
library(scales)

example1 <- data.frame(stringsAsFactors=FALSE, 
                  ID = c("Skole", "Land", "Skole", "Land", "Skole", "Land"),
                  value = c(0.654590909090909, 0.528446335193598, 0.631238336316461,
                             0.550262048394226, 0.669981060606061, 0.502430105282051),
                  variable = as.factor(c("Measurement and Geometry(Applying)",
                                          "Measurement and Geometry(Applying)",
                                          "Measurement and Geometry(Knowing)",
                                          "Measurement and Geometry(Knowing)",
                                          "Measurement and Geometry(Reasoning)",
                                          "Measurement and Geometry(Reasoning)")))
item <- ggplot(example1, aes(x=variable, y=value, fill=ID)) + 
  geom_bar(stat="identity", position = position_dodge()) +
  labs (y = "Procentdel rigtige besvarelser", title = "Matematik", x="Fagområde") +
  scale_fill_manual(values=c("grey","blue")) +
  coord_flip() +
  guides(fill = guide_legend(reverse = TRUE)) +
  theme(legend.title = element_blank()) +
  scale_y_continuous(labels = percent)
item

Below is an example image:

enter image description here

Thank you!

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Anders
  • 75
  • 1
  • 8
  • Could you add a picture showing a sample plot? – NelsonGon Jan 07 '19 at 09:40
  • I have added a link to the picture showing the example corresponding to the code :). – Anders Jan 07 '19 at 09:47
  • You can try passing a custom function that changes the range throught the `breaks` argument of the `scale_y_continuous` function. Or try the `expand` argument. – MRau Jan 07 '19 at 10:27
  • If your issue is that the bars disappear when you specify limits then take a look at [this question](https://stackoverflow.com/questions/10365167/geom-bar-bars-not-displaying-when-specifying-ylim) – wici Jan 07 '19 at 10:37
  • Would setting `expand = c(0, 0.05)` in `scale_y_continuous` address your requirement? – Z.Lin Jan 08 '19 at 00:45

1 Answers1

0

Here is a clunky variant, by calculating the min and max values, then control the ylim within the coord_flip.

library(tidyverse)
data.frame(stringsAsFactors=FALSE, 
       ID = c("Skole", "Land", "Skole", "Land", "Skole", "Land"),
       value = c(0.654590909090909, 0.528446335193598, 0.631238336316461,
                 0.550262048394226, 0.69981060606061, 0.502430105282051),
       variable = as.factor(c("Measurement and Geometry(Applying)",
                              "Measurement and Geometry(Applying)",
                              "Measurement and Geometry(Knowing)",
                              "Measurement and Geometry(Knowing)",
                              "Measurement and Geometry(Reasoning)",
                              "Measurement and Geometry(Reasoning)"))) %>% 
  mutate(low = min(value) - min(value) * 0.05,
     high = max(value) + max(value) * 0.05) -> example1 

example1 %>% 
  ggplot(aes(variable, value, fill=ID)) + 
  geom_bar(stat="identity", position = position_dodge()) +
  labs (y = "Procentdel rigtige besvarelser", title = "Matematik", x="Fagområde") +
  scale_fill_manual(values=c("grey","blue")) +
  guides(fill = guide_legend(reverse = TRUE)) +
  theme(legend.title=element_blank()) +
  coord_flip(ylim=c(mean(test$low), mean(test$high)))
B Williams
  • 1,992
  • 12
  • 19