2

I have this code as given below

ggplot(df1, aes(x = -Ord, y = Value, fill = Item)) +
  geom_bar(stat = "identity") +
  scale_y_continuous(labels = comma) +
  facet_wrap(~Area, scales = "free", drop = T, nrow = 2) +
  labs(title = "Major Crops of South Asia, Top 10 on avg of 2006-2015",
       caption = "Source: fao.org") +
  theme(legend.position = "none",
        axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        plot.caption = element_text(face = "italic")) +
  geom_text(aes(label=Item), angle = 65,
            vjust=.3, hjust = -.1, size=3)

The image it produces is given as below image as you can see the bar labels are getting cropped. The problem with free scales in facet_wrap is that I cannot use limits in scale_y_continuous otherwise some plot areas would be blank due to wide variation in value range for each country. So, my query is how to manage adding some extra space by increasing y scale for each plot freely without compromising the scales.

I have tried scale_y_continuous(limits = c(0, max(df1$Value)), labels = comma) but then it kills scales = "free" in facet_wrap. And, also the max values are to be groupwise in order to be really effective. This part I am unable to sort out.

I have deliberately avoided dput data just in case if it is needed kindly let me know. I would supply it. Thanks.

ambrish dhaka
  • 689
  • 7
  • 27
  • can this help: https://stackoverflow.com/questions/59270544/change-the-y-axis-limits-in-a-ggplot-of-multiple-plots (set breaks rather than limits) – efz Oct 04 '20 at 16:54
  • 2
    You could try to increase the "expansion" of the scale, e.g. `scale_y_continuous(expand = expansion(mult = c(.05, .1))` will increase the default expansion (= 5 percent) to 10 percent at the upper end. – stefan Oct 04 '20 at 17:05
  • thanks! this was a wonderful suggestions. I just modified the `upper end` to `.2` and it yielded nicely. – ambrish dhaka Oct 04 '20 at 17:10

0 Answers0