| View previous topic :: View next topic |
| Author |
Message |
coltson n00b

Joined: 15 Oct 2005 Posts: 46
|
Posted: Fri Jun 22, 2012 1:23 am Post subject: Problem with R: Labels too big, doesn't show up. |
|
|
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 |
|
 |
rainer Apprentice


Joined: 17 Feb 2005 Posts: 251 Location: Bonn, Germany
|
Posted: Fri Jun 22, 2012 5:39 am Post subject: |
|
|
| 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 |
|
 |
coltson n00b

Joined: 15 Oct 2005 Posts: 46
|
Posted: Fri Jun 22, 2012 10:44 am Post subject: |
|
|
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 |
|
 |
rainer Apprentice


Joined: 17 Feb 2005 Posts: 251 Location: Bonn, Germany
|
Posted: Fri Jun 22, 2012 3:05 pm Post subject: |
|
|
Which version of R are you using?
Which device are you printing to? Find out with 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 |
|
 |
coltson n00b

Joined: 15 Oct 2005 Posts: 46
|
Posted: Sat Jun 23, 2012 12:50 am Post subject: |
|
|
| rainer wrote: | | Which version of R are you using? |
2.15
| Quote: | | Which device are you printing to? Find out with 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. |
returns:
| 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 |
|
 |
rainer Apprentice


Joined: 17 Feb 2005 Posts: 251 Location: Bonn, Germany
|
Posted: Sat Jun 23, 2012 5:55 pm Post subject: |
|
|
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 |
|
 |
coltson n00b

Joined: 15 Oct 2005 Posts: 46
|
Posted: Sat Jun 23, 2012 10:34 pm Post subject: |
|
|
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 |
|
| Back to top |
|
 |
rainer Apprentice


Joined: 17 Feb 2005 Posts: 251 Location: Bonn, Germany
|
Posted: Mon Jun 25, 2012 10:12 am Post subject: |
|
|
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
which is not trivial. |
|
| Back to top |
|
 |
|
|
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
|
|