 |
 |
 |
 |
| Programming & Packaging A place to discuss programming and packaging. |

22nd June 2012, 02:22 AM
|
|
Registered User
|
|
Join Date: Mar 2005
Posts: 34

|
|
|
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 (as can be seen in the attached screenshot)! 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.
|

22nd June 2012, 08:02 AM
|
 |
Registered User
|
|
Join Date: Nov 2006
Location: Detroit
Posts: 4,621

|
|
|
Re: Problem with R: Labels too big, doesn't show up.
You could try rotating the labels by 45 degrees:
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)
mp <- barplot(quantities,col=rainbow(length(categories)),main="Escola: Ascendino Reis",xlab="Categorias",ylab="%")
text(mp,par("usr")[3],labels=categories,srt=45,offset=1,adj=1,xpd=TRUE)
It will look like this (I couldn't get the nonEnglish characters in the labels):
__________________
OS: Fedora 18 x86_64 | CPU: AMD64 3700+ 2.2GHz | RAM: 2GB PC3200 DDR | Disk: 160GB PATA | Video: ATI Radeon 7500 AGP 64MB | Sound: Turtle Beach Santa Cruz CS4630 | Ethernet: Realtek 8110SC
|

22nd June 2012, 12:19 PM
|
|
Registered User
|
|
Join Date: Mar 2005
Posts: 34

|
|
|
Re: Problem with R: Labels too big, doesn't show up.
It worked, but the labels got kind blurry,
|

22nd June 2012, 01:27 PM
|
 |
Registered User
|
|
Join Date: Mar 2009
Location: Lancaster, UK
Posts: 883

|
|
|
Re: Problem with R: Labels too big, doesn't show up.
The reason it goes blurry is the due to the rendering program that R uses on specific machines. The help says values of rotation in multiples of 90 degrees are the only ones guaranteed to work in all systems.
Alternatively, you could shrink the text using the option cex.lab=0.7 (values less than 1 shrink, values greater than 1 magnify).
Another alternative is to print the graphic to a file where you can rescale the size to be "landscape", e.g. pdf(file='mygraph.pdf',width=12,height=5)
This elongates the x axis so that there is more space between bars. Downside is that it is in a separate file which you have to open rather than the session.
|

22nd June 2012, 02:58 PM
|
|
Registered User
|
|
Join Date: Mar 2005
Posts: 34

|
|
|
Re: Problem with R: Labels too big, doesn't show up.
How this pdf option works?
I try to insert the function that you wrote
Code:
pdf(file='mygraph.pdf',width=12,height=5)
and R says:
Quote:
Error in pdf(file = "mygraph.pdf", width = 12, height = 5) :
cannot open file 'mygraph.pdf'
|
I imagine that this should dave to a pdf file? There are other formats?
Anyway I am having three more problems. As can be seen in the screenshot, when the labels get very long, they don't fit in the image and so are cut, Plus they get over the xlab "categories". Can the pdf thing fix it? And last, when the values of the quantities are slightly smaller than multiples of ten (in this case), the scale doesn't follow it. In another words, in the "nao sabe" label, it's bar goes out of scale (it's value it is 39 and the graph only goes to 30). So, how can I control it so that it goes to 40 (or any other value that I may wish?)
|

22nd June 2012, 08:55 PM
|
 |
Registered User
|
|
Join Date: Nov 2006
Location: Detroit
Posts: 4,621

|
|
|
Re: Problem with R: Labels too big, doesn't show up.
First, never use JPG for graphics that contain anti-aliased text; it will look terrible. Ideally you should use a vector graphics format like EPS or PDF, less ideally you'd use PNG (which will at least handle the text better than JPG).
Try this R code, which rotates the x-axis labels by 90 degrees:
Code:
png("mybarplot.png",width=640,height=640)
categories <- c("Servico","Comercio","Industria","Autonomo","Profissional Liberal","Servico Publico","T.I","Saude","Educacao","Empresa","Entretenimento","Outros","Nao Sabe")
quantities <- c(15,3,10,0,3,10,4,10,0,0,10,35,0)
par(mar=c(12,4,4,2) + 0.1)
mp <- barplot(quantities,col=rainbow(length(categories)),main="Escola: Ascendino Reis",ylab="%",ylim=c(0,40),axes=FALSE)
axis(2,at=seq(0,40,5))
text(mp,par("usr")[3]-.8,labels=categories,srt=90,offset=1,adj=1,xpd=TRUE)
mtext("Categorias",line=10,side=1)
q("yes")
In the png(...) command you can set the name, width, and height of the PNG image.
The "par(mar=c(12,4,4,2) + 0.1)" command sets the bottom margin to be 12 lines in order to make space for the labels (the default is 5, and the other defaults are kept). You could change that 12 to whatever you want if you have longer labels.
The "ylim=c(0,40)" option in the barplot(...) command sets the y range. The "axis(2,at=seq(0,40,5))" command sets the tick marks on the y-axis to go from 0 to 40 in steps of 5.
The -.8 part in par("usr")[3]-.8 in the text(...) command moves the x-axis labels down a bit away from the axis.
The "line=10" option in the mtext(...) command puts the x-axis name "Categorias" 10 lines down from the x-axis. You could adjust this to what you want depending on your par command above.
The result is a PNG file (mybarplot.png) which looks like this:
__________________
OS: Fedora 18 x86_64 | CPU: AMD64 3700+ 2.2GHz | RAM: 2GB PC3200 DDR | Disk: 160GB PATA | Video: ATI Radeon 7500 AGP 64MB | Sound: Turtle Beach Santa Cruz CS4630 | Ethernet: Realtek 8110SC
|

23rd June 2012, 02:44 AM
|
|
Registered User
|
|
Join Date: Mar 2005
Posts: 34

|
|
|
Re: Problem with R: Labels too big, doesn't show up.
Many thanks. Overall, it worked, and apparently it will solve any problem that might arise.
However the png command doesn't work, when I enter with
Code:
png("mybarplot.png",width=640,height=640)
, R says
Quote:
Error in X11(paste("png::", filename, sep = ""), g$width, g$height, pointsize, :
unable to start device PNG
In addition: Warning message:
In png("mybarplot.png", width = 640, height = 640) :
no png support in this version of R
|
Now some doubts:
Why + 0.1 in par?
What "axes=FALSE" does in barplot?
Why is now necessary to put -.8 in text?
What "side=1" does in mtext?
Many thanks.
|

23rd June 2012, 09:07 AM
|
 |
Registered User
|
|
Join Date: Nov 2006
Location: Detroit
Posts: 4,621

|
|
|
Re: Problem with R: Labels too big, doesn't show up.
It's possible that your version of R may not have been compiled with png support. You can check with the capabilities() command. My F17 system with R 2.15.0 shows this:
Code:
> capabilities()
jpeg png tiff tcltk X11 aqua http/ftp sockets
TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE
libxml fifo cledit iconv NLS profmem cairo
TRUE TRUE TRUE TRUE TRUE FALSE TRUE
I already mentioned what the -.8 in the text(...) command is for, and your other questions are answered in the help for those commands (for example, help(barplot)).
__________________
OS: Fedora 18 x86_64 | CPU: AMD64 3700+ 2.2GHz | RAM: 2GB PC3200 DDR | Disk: 160GB PATA | Video: ATI Radeon 7500 AGP 64MB | Sound: Turtle Beach Santa Cruz CS4630 | Ethernet: Realtek 8110SC
|

23rd June 2012, 06:05 PM
|
|
Registered User
|
|
Join Date: Mar 2005
Posts: 34

|
|
|
Re: Problem with R: Labels too big, doesn't show up.
Quote:
Originally Posted by RupertPupkin
I already mentioned what the -.8 in the text(...) command is for
|
What I mean is why he is now necessary and before wasn't
|

23rd June 2012, 07:12 PM
|
 |
Registered User
|
|
Join Date: Nov 2006
Location: Detroit
Posts: 4,621

|
|
|
Re: Problem with R: Labels too big, doesn't show up.
It probably should have been done before to move the labels down a bit.
---------- Post added at 02:12 PM ---------- Previous post was at 01:09 PM ----------
By the way, what version of R are you running? The version of R for F17 available in the Fedora repos (yum install R) has png support compiled in. So either you're using a different version of R or you're not using Fedora.
__________________
OS: Fedora 18 x86_64 | CPU: AMD64 3700+ 2.2GHz | RAM: 2GB PC3200 DDR | Disk: 160GB PATA | Video: ATI Radeon 7500 AGP 64MB | Sound: Turtle Beach Santa Cruz CS4630 | Ethernet: Realtek 8110SC
|

23rd June 2012, 11:16 PM
|
|
Registered User
|
|
Join Date: Mar 2005
Posts: 34

|
|
|
Re: Problem with R: Labels too big, doesn't show up.
2.15, I installed it from the source though.
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Current GMT-time: 07:00 (Saturday, 25-05-2013)
|
|
 |
 |
 |
 |
|
|