Home > front end >  ggplot 2 cutting off end of x axis when legend is at the top
ggplot 2 cutting off end of x axis when legend is at the top

Time:10-20

I'm trying to create a graph using ggplot, and have added in the legend.position="top" function into the theme section.

However this causes the end of my x-axis to be cut off (labels)

Just wondering if anyone knows if there is an easy fix for this?

Thanks

EDIT Dataframe (I just used a set of 100 randomly generated numbers)

enter image description here

  geom_histogram(breaks=c(seq(0, 25, by=5)),col="white") 
  theme_light() #removes background colour
  labs(x="Length in Cm", y="Frequency", ) 
  scale_x_continuous(limits=c(0,25), breaks=c(seq(0, 25, by=5)), labels=c(seq(0,25, by=5)), expand = c(0,0)) 
  scale_y_continuous(expand = c(0,0), limits = c(0,25), breaks = c(seq(0,25,by=5))) 
  scale_fill_manual(values = c("#41ae76", "#006d2c"), name="") 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),legend.title = element_text(size = 20),
        legend.text = element_text(size = 16),panel.background = element_blank(), axis.line = element_line(colour = "black"),
        axis.text = element_text(size = 16),legend.position = "top", axis.title = element_text(size = 20),legend.key=element_rect(fill="white"))```


CodePudding user response:

I suggest simplifying your plotting routine (there are quite a few redundant & unnecessary statements).

df %>%
    ggplot(aes(Length, fill = Group))  
    geom_histogram(breaks = seq(0, 25, by = 5), col = "white")  
    theme_light()  
    labs(x = "Length in Cm", y = "Frequency")  
    scale_x_continuous(limits = c(0, 25))  
    scale_y_continuous(limits = c(0, 25))  
    scale_fill_manual(values = c("#41ae76", "#006d2c"), name = "")  
    theme(
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.title = element_text(size = 20),
        legend.text = element_text(size = 16),
        panel.background = element_blank(), 
        axis.line = element_line(colour = "black"),
        axis.text = element_text(size = 16),
        legend.position = "top", 
        axis.title = element_text(size = 20),
        legend.key=element_rect(fill = "white"))

This produces

enter image description here

Some selective & further comments:

  1. Remove the expand arguments of scale_*_continous. They are likely to be responsible for cutting off text.

  2. No need for duplicating breaks inside geom_histogram and scale_x_continuous.

  3. The breaks argument to scale_y_continous is unnecessary.

  4. There are lots of theme alterations to theme_light(). Perhaps better start with a more minimalistic theme_minimal() which will require less theme alterations.

  5. I would strongly recommend using a consistent code indentation & formatting style to help with readability.


Sample data


df <- structure(list(
    Length = c(19, 23, 19, 21, 21, 21, 2, 17, 1, 20, 
               14, 12, 5, 17, 13, 14, 14, 6, 18, 11, 8, 20, 2, 5, 17, 23, 8, 
               1, 11, 11, 15, 11, 7, 23, 21, 3, 11, 9, 6, 8, 5, 21, 10, 17, 
               24, 12, 11, 3, 16, 5, 10, 12, 12, 23, 19, 25, 17, 7, 10, 25, 
               5, 21, 12, 7, 10, 17, 1, 25, 11, 15, 25, 14, 13, 2, 1, 6, 1, 
               24, 13, 17, 7, 25, 19, 17, 2, 5, 5, 23, 3, 17, 21, 8, 2, 8, 13, 
               1, 4, 2, 20, 20), 
    Group = c("a", "b", "a", "b", "a", "b", "a", 
              "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", 
              "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", 
              "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", 
              "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", 
              "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", 
              "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", 
              "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", "a", "b", 
              "a", "b")), 
    row.names = c(NA, -100L), 
    class = c("tbl_df", "tbl", 
              "data.frame"))
  • Related