user3631369
user3631369

Reputation: 331

ggplot2 theme axis.line. axes do not connect at origin

When trying to add custom axes using axis.line within theme() in ggplot2, the axes do not "perfectly" connect at origin. I use size=3 to see this effect better. Is there a way to fix this?

library(ggplot2)
ggplot(mpg, aes(displ, hwy, colour = class)) + 
geom_point()+
theme(axis.line = element_line(color = "black", size=3))

enter image description here

Upvotes: 0

Views: 359

Answers (1)

chemdork123
chemdork123

Reputation: 13863

The documentation of the element_line function lists some parameters. Of particular interest is the parameter lineend=. Default value is "butt". If you set this to "square" it fixes the issue:

ggplot(mpg, aes(displ, hwy, colour = class)) + 
    geom_point()+
    theme(axis.line = element_line(color = "black", size=3, lineend = 'square'))

enter image description here

Upvotes: 3

Related Questions