Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Problem with R: Labels too big, doesn't show up.
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Multimedia
View previous topic :: View next topic  
Author Message
coltson
n00b
n00b


Joined: 15 Oct 2005
Posts: 46

PostPosted: Fri Jun 22, 2012 1:23 am    Post subject: Problem with R: Labels too big, doesn't show up. Reply with quote

I am using R to generate graphics using a modified version of code previously posted here in another topic mine. It is this code:
Code:

> categories <- c ("Serviço","Comercio","Indústria","Autônomo","Profissional Liberal","Serviço Público","T.I","Saúde","Educação","Empresa","Entretenimento","Outros","Não Sabe")
> quantities <- c (15,3,10,0,3,10,4,10,0,0,10,35,0)
> barplot (quantities, names.arg=categories, col=rainbow(length(categories)), main="Escola: Ascendino Reis",xlab="Categorias", ylab="%")


These functions generates graphics that might be enough for what I need, but with a fatal drawback. Some of the labels doesn't show up! That is because I have too many categories (thirteen to be exact) and the name of a few of them are long . And that is when I maximize the window. I had the idea to search if was possible to R generate a window bigger than my screen resolution, so that all the names could be accommodated with the extra space. Is there a way to do that? Or other possible solution? Many thanks.
Back to top
View user's profile Send private message
rainer
Apprentice
Apprentice


Joined: 17 Feb 2005
Posts: 251
Location: Bonn, Germany

PostPosted: Fri Jun 22, 2012 5:39 am    Post subject: Reply with quote

Code:
x <- par()                        # get the present outer margin
par( oma = c( 4, 1, 1 , 1 ) )     # set outer margins to accommodate long labels
# las=2 sets vertical x axis labels:
barplot (quantities, las=2, names.arg=categories, col=rainbow(length(categories)), main="Escola: Ascendino Reis", ylab="%")
par( oma = x$oma )                # restore original outer margins   
Back to top
View user's profile Send private message
coltson
n00b
n00b


Joined: 15 Oct 2005
Posts: 46

PostPosted: Fri Jun 22, 2012 10:44 am    Post subject: Reply with quote

Now all the names of the categories are being showed up, but the most long ones are being cut by the window.

Another problem that I am facing is when the quantities value is broke, like
Code:
 quantities <- c (15,3,10,0,3,10,4,10,0,0,10,39,0)

The 39 value goes beyond the scale, with variates from 30-35 depending on the values passed to quantities.
So, how I control it, so that the value showed in the ylabel goes up to 40 ? (or any other value that I may need)
Back to top
View user's profile Send private message
rainer
Apprentice
Apprentice


Joined: 17 Feb 2005
Posts: 251
Location: Bonn, Germany

PostPosted: Fri Jun 22, 2012 3:05 pm    Post subject: Reply with quote

Which version of R are you using?

Which device are you printing to? Find out with
Code:
dev.cur()
I am using X11cairo and I have no problems with longer labels, but PDF is different. It would be helpful to work towards what you need in the end.

You can get the right y scale by adding the yaxs parameter, i.e.

Code:

barplot (quantities, las=2, yaxs="r", names.arg=categories, col=rainbow(length(categories)), main="Escola: Ascendino Reis", ylab="%")
Back to top
View user's profile Send private message
coltson
n00b
n00b


Joined: 15 Oct 2005
Posts: 46

PostPosted: Sat Jun 23, 2012 12:50 am    Post subject: Reply with quote

rainer wrote:
Which version of R are you using?

2.15

Quote:
Which device are you printing to? Find out with
Code:
dev.cur()
I am using X11cairo and I have no problems with longer labels, but PDF is different. It would be helpful to work towards what you need in the end.


Code:
dev.curl()
returns:
Quote:
null device
1


Quote:
You can get the right y scale by adding the yaxs parameter, i.e.

Code:

barplot (quantities, las=2, yaxs="r", names.arg=categories, col=rainbow(length(categories)), main="Escola: Ascendino Reis", ylab="%")


Well, that is one problem less
Back to top
View user's profile Send private message
rainer
Apprentice
Apprentice


Joined: 17 Feb 2005
Posts: 251
Location: Bonn, Germany

PostPosted: Sat Jun 23, 2012 5:55 pm    Post subject: Reply with quote

This works perfect here and should work as well on your Gentoo machine with R 2.15:

Code:
##### Data
categories <- c ("Serviço","Comercio","Indústria","Autônomo","Profissional Liberal","Serviço Público","T.I","Saúde","Educação","Empresa","Entretenimento","Outros","Não Sabe")                 
quantities <- c (15,3,10,0,3,10,4,10,0,0,10,35,0)
x <- par()                        # get the present outer margin

##### This should bring the desired result on your screen
par( oma = c( 4, 1, 1 , 1 ) )     # set outer margins to accommodate long labels
# las=2 sets vertical x axis labels:
barplot (quantities, las=2, names.arg=categories, col=rainbow(length(categories)), main="Escola: Ascendino Reis", ylab="%")


##### This should produce the same picture as pdf
cairo_pdf( "out.pdf" )
par( oma = c( 4, 1, 1 , 1 ) )     # set outer margins to accommodate long labels
barplot (quantities, las=2, names.arg=categories, col=rainbow(length(categories)), main="Escola: Ascendino Reis", ylab="%")
dev.off()


##### Same with larger numbers
quantities <- c (15,3,10,0,3,10,4,10,0,0,10,39,0)
cairo_pdf( "out1.pdf" )
par( oma = c( 4, 1, 1 , 1 ) )     # set outer margins to accommodate long labels
barplot (quantities, las=2, yaxs="r", names.arg=categories, col=rainbow(length(categories)), main="Escola: Ascendino Reis", ylab="%")
dev.off()

par( oma = x$oma )                # restore original outer margins


If not, more details about what actually is going wrong would be needed!

Good luck,
Rainer
Back to top
View user's profile Send private message
coltson
n00b
n00b


Joined: 15 Oct 2005
Posts: 46

PostPosted: Sat Jun 23, 2012 10:34 pm    Post subject: Reply with quote

It worked just fine.

However, when I pass a high value to "quantities", like 95, the scale isn't able to follow the value, ending at 80, with or without
Code:
yaxs="r"
Back to top
View user's profile Send private message
rainer
Apprentice
Apprentice


Joined: 17 Feb 2005
Posts: 251
Location: Bonn, Germany

PostPosted: Mon Jun 25, 2012 10:12 am    Post subject: Reply with quote

You can set the y axis limits manually with ylim, or calculate them based upon your data. This
Code:
barplot (quantities, las=2, ylim = c( 0, 100), names.arg=categories, col=rainbow(length(categories)), main="Escola: Ascendino Reis", ylab="%")
# or based upon your largest value in quantities
barplot (quantities, las=2, ylim = c( 0, max( quantities) * 1.2), names.arg=categories, col=rainbow(length(categories)), main="Escola: Ascendino Reis", ylab="%")

should get you done but it's sort of a "quick fix". If you want to do the real job, you have to dive into things like
Code:
?par
?axis

which is not trivial.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Multimedia All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum