Search code examples
rggplot2

ggplot axis change total length


I am trying to modify my ggplot theme, so that it looks like in the image here: enter image description here

I managed to get everything, except the part that the axis are much shorter. I was unable to find any argument in the element_line which can control the length of it, but because I think this is not an uncommon thing to do, I must have missed something...

This is the theme so far:

library(ggplot2)

x<-runif(100)
y<-runif(100)
df <- data.frame(x=x,y=y)

ggplot(df, aes(x,y))+
  geom_point()+
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        axis.title = element_text(face = "bold",size = 10),
        plot.title = element_text(size=13),
        axis.ticks = element_blank(),
        axis.text = element_blank(),
        panel.background = element_rect(colour = "white"),
        plot.background = element_rect(colour = "white"),
        axis.line = element_line(colour="black", arrow = grid::arrow(length = unit(0.3, "cm"))))

Any help is much appreciated!


Solution

  • We could do it with geom_segment():

    ggplot(df, aes(x, y)) +
      geom_point() +
      geom_segment(aes(x=0, y=0, xend=0.2, yend=0), arrow = arrow(length=unit(.5, 'cm')))+
      geom_segment(aes(x=0, y=0, xend=0, yend=0.2), arrow = arrow(length=unit(.5, 'cm')))+
      labs(x = "PHATE1", y = "PHATE2")+
      theme_void()+
      theme(axis.title.x = element_text(hjust = 0.1, vjust = 4),
            axis.title.y = element_text(hjust = 0.07, vjust = -4, angle=90)
      )
    

    enter image description here

    In case we want closed arrows:

    ggplot(df, aes(x, y)) +
      geom_point() +
      geom_segment(aes(x=0, y=0, xend=0.2, yend=0), arrow = arrow(length=unit(.5, 'cm'), type = "closed"))+
      geom_segment(aes(x=0, y=0, xend=0, yend=0.2), arrow = arrow(length=unit(.5, 'cm'), type = "closed"))+
      labs(x = "PHATE1", y = "PHATE2")+
      theme_void()+
      theme(axis.title.x = element_text(hjust = 0.1, vjust = 4),
            axis.title.y = element_text(hjust = 0.07, vjust = -4, angle=90)
      )
    

    enter image description here

    OR using ggarchery package:

    library(ggarchery)
    library(ggplot2)
    
    ggplot(df, aes(x, y)) +
      geom_point() +
      geom_arrowsegment(aes(x=0, y=0, xend=0.2, yend=0), arrows = arrow(type = "closed"))+
      geom_arrowsegment(aes(x=0, y=0, xend=0, yend=0.2), arrows = arrow(type = "closed"))+
      labs(x = "PHATE1", y = "PHATE2")+
      theme_void()+
      theme(axis.title.x = element_text(hjust = 0.1, vjust = 4),
            axis.title.y = element_text(hjust = 0.07, vjust = -4, angle=90)
      )
    

    enter image description here