Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to maintain size of ggplot with long labels

Tags:

r

ggplot2

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:

enter image description here

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.

like image 947
Sebastian Zeki Avatar asked Jan 10 '17 12:01

Sebastian Zeki


People also ask

How do I make my axis labels bigger in ggplot2?

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.

How do I stop Ggplot from scientific notation?

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.

How do I add labels in Ggplot?

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.


Video Answer


1 Answers

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.

Dummy data

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" 

"Standard" bar chart

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.

enter image description here

OP's approach: rotate labels

Labels on x-axis are rotated by 90° degrees, squeezing the plot area:

p + theme(axis.text.x = element_text(angle = 90)) 

enter image description here

Horizontal bar chart

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

enter image description here

Vertical bar chart with labels wrapped

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 

enter image description here

Horizontal bar chart with labels wrapped

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

enter image description here

Addendum: Abbreviate labels using 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) 

enter image description here

like image 118
Uwe Avatar answered Oct 20 '22 18:10

Uwe