Sometimes when you are making a figure in R, the automatic axis tick marks won't look like you want. Let's generate some sample data and a histogram and see its automatic labeling.

decimals<-c(0.12,0.15,0.22,0.37,0.25,0.4,0.5,0.2)

hist(decimals,

breaks=seq(from=0.05, to=0.55, by=0.1),

col="gray",

main="",

xlab="Proportion of things")

Now try it while suppressing the x-axis so we can add our own labels.

hist(decimals,

breaks=seq(from=0.05, to=0.55, by=0.1),

col="gray",

main="",

xlab="Percent of things",

xaxt="n") #suppress the x-axis.

axis(side=1, #from help: "1=below, 2=left, 3=above and 4=right."

las=1,

at=c(0.1,0.3,0.5), #desired intervals if you want to change that too

labels=c("10%", "30%", "50%"))#re-add the x-axis with desired labels at whatever intervals (here every 0.2).

#You can make the labels any arbitrary value you wish,

# so you could label 1-12 as January-December too.

#If you want to automate labeling, use seq().

hist(decimals,

breaks=seq(from=0.05, to=0.55, by=0.1),

col="gray",

main="",

xlab="Percent of things",

xaxt="n") #suppress the x-axis.

axis(side=1,

las=1,

at=c(seq(from=0.1, to=0.5, by=0.1)),

#Use seq in both at and labels (the intervals must match)

#or you get an error: "'at' and 'labels' lengths differ"

labels=c(100*seq(from=0.1, to=0.5, by=0.1)))

#Multiply the sequence you created in "at" to get percentages.

## No comments:

## Post a Comment

Comments and suggestions welcome.