在 R 基本绘图中调整条形图中的 x 轴标签名称

Adjusting x-axis label names in barchart in R base plotting

如何调整此条形图中 x 轴标签的位置?我最好让每个名字左对齐或居中?

这是我的代码:

data <- read.csv('CNV_bar_DF.csv')

f<-"CNV_data.png"
png(f,width=1000, height=1000)

par(fig=c(0.0,1.0,0.5,1.0)) #   FINE POSITIONING, x1,x2,y1,y2
barplot(data$Amplification, names=data$Gene, cex.names=1.0, las=2, ylim=c(0,60), col=colors()[c(0,81,0)]) 

par(fig=c(0.0,1.0,0.05,0.55), new=TRUE) #   FINE POSITIONING, x1,x2,y1,y2
barplot(data$Deletion, ylim=rev(c(0,60)), col=colors()[c(0,137,0)])

dev.off()

标记为 'Gene' 的列就是我所说的 x 轴标签。目前它是右对齐的,但我希望它在中心。如果有更好的标签写法欢迎提出建议

这是数据框:

    Gene    Deletion    Amplification   Total
KIAA0100    2   43  45
DNAH9   44  0   44
BPTF    0   38  38
PDCD11  32  1   33
PREX2   3   30  33
RAD51C  0   31  31
CSMD3   0   30  30
ENPP2   0   29  29
STAT3   0   28  28
CPXM2   24  1   25
SOCS7   0   25  25
STAT5B  0   24  24
ACADSB  22  1   23
CSMD1   8   14  22
FAM208B 1   21  22
GOLGA7B 22  0   22
STAT5A  0   22  22
AXIN2   0   21  21
UNC45B  0   21  21
GAD2    20  0   20
SLC6A4  0   20  20
ANK3    19  0   19
APBB1IP 19  0   19
C17orf47    0   19  19
PRKCQ   0   19  19
TSPYL5  0   19  19
CRHR1   1   17  18
CDH23   16  0   16
TET1    15  0   15
CDHR1   14  0   14
FAM110B 0   14  14
FAM83A  0   12  12
GPR158  10  2   12
KIAA1244    12  0   12
ELAVL2  11  0   11
LRRC6   1   10  11
PTPRK   9   2   11
SASH1   10  1   11
BRCA2   10  0   10
ENPEP   6   4   10
FAM171A2    0   10  10
RFX6    9   1   10

这是输出图像:

试试这个(内联评论):

png(f, 1000, 1000)

par(fig=c(0.0,1.0,0.5,1.0)) 
pos <-   # the result of barplot is the location of the midpoints for axis construction
   barplot(data$Amplification, xaxt="n", # suppresses x-axis printing
                ylim=c(0,60), col=colors()[c(0,81,0)]) 
axis(1,           # now make the x-axis
     las=2, hadj=0.5, # this "centers" the text
     at=pos,          # this uses the return result of `barplot`
     lab=data$Gene, 
     line=2,   # the 'line' argument needed to adjust the vertical "centerline"
     col="transparent")   # needed to suppress drawing an "axis-line"
par(fig=c(0.0,1.0,0.05,0.55), new=TRUE) 
barplot(data$Deletion, ylim=rev(c(0,60)), col=colors()[c(0,137,0)])

dev.off()

使用 png(f, 1000,1000) 进行了测试,在我看来似乎可以提供可接受的结果。这有点像 pyramid-plot 尽管它们是垂直排列的。它们通常用于显示比较人口统计文献中 age-groups 男性和女性的人口比例。我知道 plotrix 包中有金字塔图的实现。如果您使用的是 base-graphics,请查看 pkg:plotrix 中的新增容量是个好主意。