Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

barplot design issues

Tags:

r

I am doing a barplot of 14 columns to represent some data, I set the names.arg option to show as vertical lables, unfortunately, this caused the new vertical lables to overlap with the "sub" and "xlab" options I have. How do I prevent this from happening?

Here's my command:

par(mar=c(6, 5, 4,7.5 ))
barplot(x, main=paste("title1 \n","subtitle"),
names.arg=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14),las=2, sub=("overlapping text1"),
xlab="overlapping text2", col = c("red2","darkorange3"))

Another question on my mind is, I am using a 2-line title in "main" as you can see from the command. Is it possible to make the second line font smaller, whilst keeping the first line in the same format?

Thanks,

like image 987
Error404 Avatar asked Dec 27 '22 09:12

Error404


2 Answers

One solution to change font size for one of titles, is to use two calls of functions mtext() in different lines with different cex= values and remove main= from barplot(). To overcome problem with overlapping text, mtext() also can be used instead of xlab= and sub=. You just have to find the right line= and increase space around plot with par(mar=..).

x<-sample(letters[1:14],300,replace=TRUE)
par(mar=c(9,3,5,2))    
barplot(table(x),
        names.arg=paste0("very_long_",1:14),las=2, 
        col = c("red2","darkorange3"))
mtext(side=3,"Title1",line=2,cex=2)
mtext(side=3,"subtitle",line=1,cex=1.5)

mtext(side=1,"overlapping text1",line=6)
mtext(side=1,"overlapping text2",line=7)

enter image description here

like image 54
Didzis Elferts Avatar answered Jan 10 '23 14:01

Didzis Elferts


Another option to look at is the staxlab function in the plotrix package.

Also look at the mgp argument to the par function for a way to set the default placement of the axis title.

like image 29
Greg Snow Avatar answered Jan 10 '23 13:01

Greg Snow