Home > Enterprise >  ggplot2: no gap between axis end and last tick
ggplot2: no gap between axis end and last tick

Time:07-30

I am currently searching for a solution to have axis lines end at end of the last tick mark, not halfway through the last tick mark, if set to end at the last tick (vice versa for beginning and first tick mark). The overall line length therefore should be something like lengthaxis line widthtick mark*0.5.

library(ggplot2)
#V1 - base: Dotplot with axis ending at last tick mark
#--> axis ends AT tick mark, but only reaching to half of the tick width
ggplot(mpg, aes(cty, hwy)) 
  geom_point() 
  scale_y_continuous(breaks = c(10,30,50), limits = c(10,50), expand = c(0,0)) 
  scale_x_continuous(breaks = c(8,35), limits = c(8,35), expand = c(0,0)) 
  theme_classic() 
  theme(axis.ticks = element_line(color = "black"))

#V2: Expand axis length via expand to stretch over full width of last axis tick 
#--> requires lots of tinkering and is size dependent (zoom), not feasible for many different plots
ggplot(mpg, aes(cty, hwy)) 
  geom_point() 
  scale_y_continuous(breaks = c(10,30,50), limits = c(10,50), expand = c(0,0.05)) 
  scale_x_continuous(breaks = c(8,35), limits = c(8,35), expand = c(0,0.05)) 
  theme_classic() 
  theme(axis.ticks = element_line(color = "black"))

#V3: Decrease tick mark width
#--> ticks too thin 
ggplot(mpg, aes(cty, hwy)) 
  geom_point() 
  scale_y_continuous(breaks = c(10,30,50), limits = c(10,50), expand = c(0,0)) 
  scale_x_continuous(breaks = c(8,35), limits = c(8,35), expand = c(0,0)) 
  theme_classic() 
  theme(axis.ticks = element_line(color = "black", size = 0.5))

#V4: Draw axis via geom_segment
#--> same as V2
ggplot(mpg, aes(cty, hwy)) 
  geom_point() 
  scale_y_continuous(breaks = c(10,30,50), limits = c(9.5,50.5), expand = c(0,0)) 
  scale_x_continuous(breaks = c(8,17.5,35), limits = c(7.5,35.5), expand = c(0,0)) 
  theme_classic() 
  theme(axis.ticks = element_line(color = "black"),
        axis.line = element_blank()) 
  geom_segment(aes(x=7.9, xend = 35.1, y = 9.5,yend = 9.5), size = 1, color = "black") 
  geom_segment(aes(x=7.5, xend = 7.5, y = 9.9,yend = 50.1), size = 1, color = "black")

#V5: Truncate axis with ggh4x package
#--> closest to final form, but also not covering full width of tick marks
library(ggh4x)
ggplot(mpg, aes(cty, hwy)) 
  geom_point() 
  scale_y_continuous(breaks = c(10,30,50), limits = c(10,50)) 
  scale_x_continuous(breaks = c(8,35), limits = c(8,35)) 
  guides(x = "axis_truncated", y = "axis_truncated") 
  theme_classic() 
  theme(axis.ticks = element_line(color = "black"))

The last solution including the truncated axis by the ggh4x package is closest to the plot I ultimately like to have. For every other approach I listed why the outcome is flawed or hardly doable for multiple different plots.

I therefore would like to find a solution which achieves axis lines covering the full width of the respective first and last axis tick marks for the axis. The solution should be independent of plot size or other features, and not involve (too much) manual trial and error axis length/size settings.

Is there any solution or package out there which could achieve what I am failing to do? Thanks for any help in advance!

CodePudding user response:

I am quoting @teunbrand here who commented the correct solution.

"In the ggh4x option, assuming the linewidth of the ticks and linewidth of the axis line are the same, in theory you should be able to add axis.line = element_line(lineend = "square") to your theme to get the desired result. In practice, it also matters which graphical device is rendering the plot."

This works perfectly, as illustrated below.

ggplot(mpg, aes(cty, hwy)) 
  geom_point() 
  scale_y_continuous(breaks = c(10,30,50), limits = c(10,50), expand = c(0,0)) 
  scale_x_continuous(breaks = c(8,35), limits = c(8,35), expand = c(0,0)) 
  theme_classic() 
  theme(axis.ticks = element_line(color = "black"),
        axis.line = element_line(lineend = "square"))

If teunbrand posts his comment as an answer, I will delete this and accept his.

  • Related