rgl plot: point size does not change when saving as postscript - r

I'm trying to generate a 3d scatterplot using rgl. It looks great on my screen, but whenever I export it as a PDF (or any other postscript format) it completely ignores any size specifications I use.
(I'm running RGui v.2.15.1 and rgl v.0.92.892 on a Macbook under Mountain Lion.)
For example:
library(rgl)
set.seed(1982)
points3d(runif(5),runif(5),runif(5), size=20)
# points look huge
rgl.postscript('testplot.pdf', fmt='pdf')
# points look tiny
Does anyone have an idea for a way to get this to work? The resolution of the images I get using rgl.snapshot don't look so good, and I would really like to get a vector image for this plot.
Also, I followed this thread and I got text to resize just fine, but not points. So I thought one way to work around this would be to plot my points as text using a circle as my character, but I couldn't get rgl to accept symbols or expressions either...

Confirmed on Windows, look like some paper size scaling problem. You might try
spheres3d(runif(5),runif(5),runif(5),radius=0.1)
as a workaround if you can live with real 3d.

Related

Blurry edges when using "corrplot" package in R

I'm using corrplot in R to plot the correlation matrrix. For some reason, the edges of the circles in the plot seems missing some pixels. The plot overall looks very rough. Please see the graph I plotted:
enter image description here
But the example I followed shows the plot is supposed to be like this:
enter image description here
My initial guess was dpi and scaling issue because I use a 4K display. I tried changing the resolution and size of the plots but it doesn't work.
Thank you so much for your help.

Wrong output size when plotting over an image in R

My goal is to read an image file in either the PNG or JPEG format and plot various data over said image and save it to disk.
I also want the image to take up all available space in the produced plot, no axes or labels or anything. I'm a bit concerned that this might be relevant to my problem.
Code example
Below is my current code that currently only tries to output the same image as you put in. Later I plan on plotting data points corresponding to coordinates over the image. I've used some sample code found here in order to remove the axes and be able to have the image in the background of the plot.
library(jpeg)
library(grid)
img <- readJPEG(system.file("img", "Rlogo.jpg", package="jpeg"),native=TRUE)
jpeg(filename = "Rlogo-2.jpg", width=100,height=76, quality = 100,res=299)
op<-par(mar=rep(0,4))
plot(0:100,type="n", axes="FALSE",ann="FALSE")
lim <- par()
rasterImage(img, lim$usr[1], lim$usr[3], lim$usr[2], lim$usr[4])
dev.off()
Example output
This is an example output of my above code in a comparison with the original image:
The image to the left is the original and the right one is the modified one. As you can see it seems as if the image I read and plot somehow is smaller than the original image and when saved to the original dimensions it appears blurred.
I've been pulling my hair over this one for hours and I don't seem to get anywhere. This is my first attempt to plot data over images and I'm aware of my lack of knowledge about how R represents images and I've mostly been using the basic graphics to do relatively simple plots before.
I'm currently considering doing this in Python instead but I'm afraid that'll come back and bite me when it comes to the actual plotting of the data.
I run R version 3.1.0 on x86_64 running Windows 7.
Just to summarize, since you already found the culprit, there are two issues present here:
Firstly, the blurring appears to be caused by the jpeg device on Windows. There is no such problem on Ubuntu Linux and it disappears if you use the Cairo-device instead, as you did already discover. Cairo-devices are great for pdf:s too since they embed all the fonts etc. making the figure look the same across platforms.
Secondly, R adds 4% extra margin to the x and y axes by default to prevent graphics from being chopped off near the edge of the plot area. It can be corrected by setting xaxs="i" and yaxs="i".
par(mfrow=1:2)
plot(1:5, 1:5) # Left
plot(1:5, 1:5, xaxs="i", yaxs="i") # Right
In your case the difference is subtle but still would cause everything to be slightly misaligned.

How to enlarge map in maps package within rstudio?

When I plot a map in Rstudio, using the maps package, the map is small and when I try to zoom in it stays small. I was wondering if there is a way to enlarge it.
I use e.d. this script from http://www.molecularecologist.com/2012/09/making-maps-with-r/
library(maps)
library(mapdata)
map("worldHires","Canada”, xlim=c(-141,-53), ylim=c(40,85), col="gray90”, fill=TRUE)
Does anyone know how to enlarge this image or is this just not possible due to lack of enough pixels?
Two things:
First, there are a few problems with your code, particularly the " (double quote) characters. Below is the correction:
map("worldHires","Canada", xlim=c(-141,-53), ylim=c(40,85), col="grey90", fill=T)
Second, in answer to your question when I exported the plot from with RStudio as a .pdf and opened the .pdf it is indeed a vector file which you can zoom in and out of as expected. You can specify the default canvas size on export, either from the GUI or using a function like pdf()

Quality differences of graphics produces by PC and Mac in R

Not sure if this is the appropriate place to ask this question, so please feel free to delete it...
I've been preparing a little introduction to course lately, and in the section about saving plots I noticed something odd. When I created a figure on my work PC, it looked the way it should. But when I ran the exact same code again on my Mac at home, the image looked somewhat blurred. I tried this for the png, tiff, and jpeg formats, the result is the same, the plot looks nicer when produced on a PC. I include two graphs below so you can see for yourself. (The first the the PC plot, the second the Mac plot). Although the PC produces the better quality, the figure produced by the Mac is larger (19 vs 8 KB). Below is another image, where both figures are magnified (Mac left, PC right). While the PC draws a crisp line, the Mac produces some kind of a shading around the lines. Can anyone explain why this is the case and how I can produce better quality plots on the Mac? I played around with the options, in particular the type argument, but couldn't neither find a solution, nor find help on the internet.
Here is the code to generate the plot:
set.seed(2)
BMI<-rnorm(n=1000, m=24.2, sd=2.2)
png(file="fig3_m2.png", width=700, height=700, res=120)
par(mgp=c(1.7,.6,0), mar=c(3,3,2,2), yaxs="i")
x <- hist(BMI, freq=F, main="Distribution of BMI",
xlab="Body Mass Index", col="lightgreen",
ylim=c(0,.2),
breaks=seq(min(BMI), max(BMI), length=18))
box()
dev.off()

R How to make smoother looking plots of oscillations

When plotting oscillations in R, e.g., using the package desolve,
df1 <-function(t,y,mu)( list(c(y[2],mu*y[1]^3-y[1]+0.005*cos(t))))
library (deSolve)
yini<-c(y1=0,y2=0)
df2 <-ode(y=yini,func=df1, times=0:520,parms=0.1667)
plot(df2,type="l",which="y1",ylab="Displacement",xlab="Time", main="")
I get raggedy plots such as:
instead of a smooth plot (not done in R) such as:
Does anyone know of a way to obtain a smoother plot in R instead of a raggedy one when displaying oscillations? Note that it is not just a matter of the difference in scale and I am not looking for a smoothing filter.
Thanks,
I generated your plot in R and exported it as PDF. I zoomed in on it and it's quite lovely. I can't see the problem you're talking about there. Therefore, there are some scaling issues or something with a raster format that are causing the issue. Perhaps you're pasting into Word and that's giving you a raster image that's bad. The plot that R is making, at a logical level, is great in spite of the one you posted. It's even better than the comparison plot you put up.
It's possible that you're generating the plot in a raster format and not setting a high enough resolution and size. Try tiff('filname', 1200, 1200, 300) for a good raster image of it. I did notice that when exporting to raster formats it was easy to make your plot into a fine mess with default png or jpg settings that would just smear things.
Maybe you really wanted to sample in your function at a higher resolution, something not done in the comparison plot. If that's the case then it's relatively easy. Change 0:520 to seq(0, 520, 0.1). That's an even nicer plot, as shown below (much better than shown as PDF, EPS, or SVG).

Resources