Search code examples
rggplot2

Plotting only half-length axis lines


For example code:

library(Rtsne)
library(ggplot2)
data(iris)

tsne.res <- as.data.frame(Rtsne(as.matrix(iris[,1:4]), check_duplicates = FALSE)$Y)
colnames(tsne.res) <- c("x","y")

ggplot(tsne.res,
       mapping = aes(x = x, y = y)) +
  geom_point() +
  labs(x = "TSNE1", y = "TSNE2") +
  theme_bw() +
  theme(aspect.ratio = 1,
        panel.border = element_blank(),
        panel.grid = element_blank(),
        axis.line.x = element_line(arrow = arrow(type = "closed", length = unit(0.25, "cm"))),
        axis.line.y = element_line(arrow = arrow(type = "closed", length = unit(0.25, "cm"))),
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        axis.title.x = element_text(hjust = 0.05),
        axis.title.y = element_text(hjust = 0.05))

Current plot:

Example TSNE

I want to reduce the length of the x/y axis lines such that they appear as this mockup -

Required plot:

Example TSNE2

This thread already exists but it requires geom_segment() which requires you to specify coordinates for start and end of the segment

I would much prefer a solution that would be more generic/tied to formatting not plot content.

Update

Based on replies, I was able to use the following code and attach it onto the end of a Seurat plot:

dim1.gg <- DimPlot(sub.seurat, 
        reduction = "UMAP_PCA", 
        group.by = "dataset_fix", 
        label = FALSE, 
        raster = FALSE) + 
  scale_colour_manual(values = group2.cols) + 
  labs(title = "10x RNA", x = "UMAP1", y = "UMAP2")

dim1.gg + 
  scale_x_continuous(breaks = quantile(dim1.gg[[1]]$data$UMAPpca_1, prob = 0.25), guide = guide_axis(cap = 'upper')) +
  scale_y_continuous(breaks = quantile(dim1.gg[[1]]$data$UMAPpca_2, prob = 0.25), guide = guide_axis(cap = 'upper')) +
  
  theme(aspect.ratio = 1,
        panel.border = element_blank(),
        panel.grid = element_blank(),
        axis.line = element_line(arrow = arrow(type = "closed", length = unit(0.25, "cm"))),
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        axis.title = element_text(hjust = 0.15),
        legend.position = "none")

final plot


Solution

  • We can use the cap argument to the scale guide to cap the axis at the lower or upper (or both) breaks, like so:

    library(ggplot2)
    
    df <- data.frame(x = runif(100), y = runif(100))
    
    ggplot(df, aes(x, y)) +
      geom_point() +
      scale_x_continuous(breaks = max(df$x) / 4, guide = guide_axis(cap = 'upper')) +
      scale_y_continuous(breaks = max(df$y) / 4, guide = guide_axis(cap = 'upper')) +
      coord_fixed() +
      theme_classic() +
      theme(
        axis.text = element_blank(), 
        axis.ticks = element_blank(),
        axis.title = element_text(hjust = 0.125),
        axis.line = element_line(arrow = arrow(length = unit(0.4, "cm"), type="closed"))
      )
    

    You still need to set the breaks in data coordinates though, but this is relatively easy.

    enter image description here