I have a plot that is a simple barplot of number of each type of an event. I need the labels of the plot to be under the plot as some of the events have very long names and were squashing the plot sideways. I tried to move the labels underneath the plot but it now gets squashed upwards when there are lots of event types. Is there a way of having a static plot size (i.e. for the bar graph) so that long legends don't squash the plot?
My code:
ggplot(counts_df, aes(x = Var2, y = value, fill - Var1)+ geom_bar(stat = "identity") + theme(legend.position = "bottom") + theme(legen.direction = "vertical") + theme(axis.text.x = element_text(angle = -90)
The result:
I think this is because the image size must be static so the plot gets sacrificed for the axis. The same thing happens when I put a legend beneath the plot.
To increase the X-axis labels font size using ggplot2, we can use axis. text. x argument of theme function where we can define the text size for axis element.
Example 1: Disable Scientific Notation of ggplot2 Axis We did that by applying the scale_x_continuous function in combination with the comma function of the scales package.
To add labels at specified points use annotate() with annotate(geom = "text", ...) or annotate(geom = "label", ...) . To automatically position non-overlapping text labels see the ggrepel package.
There a several ways to avoid overplotting of labels or squeezing the plot area or to improve readability in general. Which of the proposed solutions is most suitable will depend on the lengths of the labels and the number of bars, and a number of other factors. So, you will probably have to play around.
Unfortunately, the OP hasn't included a reproducible example, so we we have to make up our own data:
V1 <- c("Long label", "Longer label", "An even longer label", "A very, very long label", "An extremely long label", "Long, longer, longest label of all possible labels", "Another label", "Short", "Not so short label") df <- data.frame(V1, V2 = nchar(V1)) yaxis_label <- "A rather long axis label of character counts"
Labels on the x-axis are printed upright, overplotting each other:
library(ggplot2) # version 2.2.0+ p <- ggplot(df, aes(V1, V2)) + geom_col() + xlab(NULL) + ylab(yaxis_label) p
Note that the recently added geom_col()
instead of geom_bar(stat="identity")
is being used.
Labels on x-axis are rotated by 90° degrees, squeezing the plot area:
p + theme(axis.text.x = element_text(angle = 90))
All labels (including the y-axis label) are printed upright, improving readability but still squeezing the plot area (but to a lesser extent as the chart is in landscape format):
p + coord_flip()
Labels are printed upright, avoiding overplotting, squeezing of plot area is reduced. You may have to play around with the width
parameter to stringr::str_wrap
.
q <- p + aes(stringr::str_wrap(V1, 15), V2) + xlab(NULL) + ylab(yaxis_label) q
My favorite approach: All labels are printed upright, improving readability, squeezing of plot area are is reduced. Again, you may have to play around with the width
parameter to stringr::str_wrap
to control the number of lines the labels are split into.
q + coord_flip()
scale_x_discrete()
For the sake of completeness, it should be mentioned that ggplot2
is able to abbreviate labels. In this case, I find the result disappointing.
p + scale_x_discrete(labels = abbreviate)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With