Home > database >  How to modify size of border lines in ggplot geom?
How to modify size of border lines in ggplot geom?

Time:10-22

I'm trying to modify a histogram like this:

library(tidyverse)

iris %>% 
  ggplot(aes(x=Sepal.Length)) 
  geom_histogram(bins=10,
                 fill="white",
                 color="black",
                 size=1) 
  labs(x="Sepal Length",
       y="Count",
       title = "Sepal Length Histogram") 
  theme_classic() 
  theme(plot.title = element_text(face="bold")) 
  scale_x_continuous(expand = c(0.01,0.01)) 
  scale_y_continuous(expand = c(0.00,0.01))

Which looks like the following plot:

enter image description here

I would like to know if there is a way to weight the size of the x and y axis lines like so to make them thicker:

enter image description here

I tried this:

iris %>% 
  ggplot(aes(x=Sepal.Length)) 
  geom_histogram(bins=10,
                 fill="white",
                 color="black",
                 size=1) 
  labs(x="Sepal Length",
       y="Count",
       title = "Sepal Length Histogram") 
  theme_classic() 
  theme(plot.title = element_text(face="bold"),
        panel.border = element_rect(linetype = "solid",
                                    colour = "black", size=5)) 
  scale_x_continuous(expand = c(0.01,0.01)) 
  scale_y_continuous(expand = c(0.00,0.01))

But that basically erased my plot:

enter image description here

I've also tried manually drawing them with geom_vline and geom_hline but this is time-consuming and causes other issues with the aesthetics. Any ideas would be appreciated.

CodePudding user response:

One option would be to increase the size of the axis lines plus setting the lineend to "square":

library(ggplot2)

ggplot(iris, aes(x=Sepal.Length)) 
  geom_histogram(bins=10,
                 fill="white",
                 color="black",
                 size=1) 
  labs(x="Sepal Length",
       y="Count",
       title = "Sepal Length Histogram") 
  theme_classic() 
  theme(plot.title = element_text(face="bold")) 
  scale_x_continuous(expand = c(0.01,0.01)) 
  scale_y_continuous(expand = c(0.00,0.01))  
  theme(axis.line = element_line(size = 2, lineend = "square"))

CodePudding user response:

Just add this line to your code: theme(axis.line = element_line(colour = 'black', size = 2))

library(tidyverse)

iris %>% 
  ggplot(aes(x=Sepal.Length)) 
  geom_histogram(bins=10,
                 fill="white",
                 color="black",
                 size=1) 
  labs(x="Sepal Length",
       y="Count",
       title = "Sepal Length Histogram") 
  theme_classic() 
  theme(plot.title = element_text(face="bold")) 
  scale_x_continuous(expand = c(0.01,0.01)) 
  scale_y_continuous(expand = c(0.00,0.01)) 
  theme(axis.line = element_line(colour = 'black', size = 2))

enter image description here

  • Related