Reputation: 1644
How do I adjust the position of the x-axis labels in this bar plot? I would ideally make each name left justified of centered?
Here is my code:
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()
The column labelled 'Gene' is what I mean by x-axis label. Currently it is right justified but I would like it in the center. If there is a better way to write the label then suggestions are very welcome.
here is the dataframe:
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
here is an image of the output:
Upvotes: 1
Views: 1670
Reputation: 263301
Try this (comments inline):
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()
Tested with png(f, 1000,1000)
and seems to deliver acceptable results to my eye. This is somewhat like a pyramid-plot although those are arranged vertically. They are typically used to display compare proportions of populations by age-groups of men and women in demographic literature. I know there is an implementation of pyramid plots in the plotrix package. If you are using base-graphics, checking out the added capacities in pkg:plotrix would be a good idea.
Upvotes: 2