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")
}