ggplot2 缩小的/一般长度的、带箭头的坐标轴 | R语言

24 篇文章 0 订阅

1. 效果图

在这里插入图片描述
左侧为DimPlot2()效果图。
右侧为DimPlot()效果图,原图。

2. 代码

  • axis.line = element_line(arrow = arrow(type = "open", length = unit(0.3, "cm"))), 其中:
    • type="open"表示是开放箭头,type=“closed” 表示是封闭箭头。
    • length= 设置箭头的长度。
#' DimPlot with 缩小的坐标轴
#'
#' @param scObject 
#' @param reduction 
#' @param group.by 
#' @param label 
#' @param raster 
#' @param legend.position 
#' @param ... 
#'
#' @return
#' @export
#'
#' @examples
DimPlot2=function(scObject, reduction="umap", group.by = "seurat_clusters", 
                  label=F, raster=F, legend.position="right", #"none"
                  ...){
  # https://stackoverflow.com/questions/78667978/plotting-only-half-length-axis-lines
  #1) init plot
  p1 <- DimPlot(scObject, 
                reduction = reduction, 
                group.by = group.by, 
                label = label, 
                raster = raster)#+ 
  #scale_colour_manual(values = group2.cols) #+ 
  #labs(title = "10x RNA", x = "UMAP_1", y = "UMAP_2")
  #2) get range
  getRange=function(x){
    #min(x) + 0.25 * diff(x)
    min(x) + 0.25 * (max(x)-min(x))
  }
  #3) set range
  p1 + 
    scale_x_continuous(breaks = getRange(p1$data[,1]), guide = guide_axis(cap = 'upper')) +
    #scale_y_continuous(breaks = quantile(p1$data[,2], prob = 0.20), guide = guide_axis(cap = 'upper')) +
    scale_y_continuous(breaks = getRange(p1$data[,2]), 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.2, "cm"))),
      axis.text = element_blank(),
      axis.ticks = element_blank(),
      axis.title = element_text(hjust = 0.025),
      legend.position = legend.position
    )
}

if(0){
	DimPlot2(scObj, reduction = "umap", label=T)
	DimPlot2(scObj, reduction = "umap", label=T, legend.position = "none")
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值