I am trying to modify my ggplot theme, so that it looks like in the image 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!
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)
)
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)
)
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)
)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With