Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot axis change total length

Tags:

r

ggplot2

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!

like image 217
nhaus Avatar asked Sep 10 '25 03:09

nhaus


2 Answers

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

like image 128
TarJae Avatar answered Sep 12 '25 19:09

TarJae


For the sake of completeness - it's not difficult to change the length of your axis.

Add limits to coord_..., turn clipping off, and add a margin to your plot.

library(ggplot2)

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

ggplot(df, aes(x,y))+
  geom_point()+
  ## limit the coordinates and turn clipping off
  coord_cartesian(xlim = c(0, .25), ylim = c(0, .25), clip = "off") +
  ## add a margin to your plot
  theme(
    plot.margin = margin(t = 2.6, r = 3.7, unit = "inch"),
    panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        axis.ticks = element_blank(),
        axis.text = element_blank(),
        panel.background = element_rect(fill = "white"),
        axis.line = element_line(colour="black", arrow = grid::arrow(length = unit(0.3, "cm"))))

Created on 2023-04-24 with reprex v2.0.2

like image 30
tjebo Avatar answered Sep 12 '25 17:09

tjebo