How to save ggplot size with long shortcuts

I have a graph that is a simple barcode of the number of each type of event. I need plot labels to be under the plot, as some of the events have very long names and overwhelmed the plot on the side. I tried to move the shortcuts to the plot, but now it is crushed up when there are many types of events. Is there a way to have the size of a static graph (i.e. for a histogram) so that long legends do not tablet the plot?

My code is:

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)

Result:

enter image description here

I think this is due to the fact that the image size must be static so that the plot is sacrificed for the axis. The same thing happens when I put a legend under the plot.

+4
source share
1

. , , . , , , .

, OP , :

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"

x , :

library(ggplot2)  # version 2.2.0+
p <- ggplot(df, aes(V1, V2)) + geom_col() + xlab(NULL) +
  ylab(yaxis_label) 
p

, geom_col() geom_bar(stat="identity").

enter image description here

OP:

X 90 ° , :

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

enter image description here

( y) , , ( , ):

p + coord_flip()

enter image description here

,

, , . , width stringr::str_wrap.

q <- p + aes(stringr::str_wrap(V1, 15), V2) + xlab(NULL) +
  ylab(yaxis_label)
q

enter image description here

: , , . , width stringr::str_wrap , .

q + coord_flip()

enter image description here

: scale_x_discrete()

, ggplot2 . .

p + scale_x_discrete(labels = abbreviate)

enter image description here

+10

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


All Articles