Oddly shaped markers in ggplot2 - r

I recently installed ggplot2 and tried a qplot. The plot comes out like this,
If you notice you can see that the markers seem to look deformed rather than circles. Is there a way to correct this?
Here is my code:
require(ggplot2)
set.seed(1410)
qplot(carat, price, data = diamonds)
EDIT: The plot looks fine when exported to a pdf.
I am using R3.0.2 ggplot2_0.9.3.1 on elementary OS.Thanks.

I usually don't worry about the look of the picture in the "window view" / "r view" -- it is just a graphical view of what the picture will look given the current setting for the resolution.
If possible, I save a picture as an eps or pdf file. They are vector based picture and scale well regardless of size.
If I don't use an eps or pdf file, I use png files to save my pictures:
g1 <- ggplot(data, aes(x=X1, y=Y1)+
geom_point(x)
png("high_res_png.png", width = 10000, height = 7000, res = 1300)
print(g1)
dev.off()
Using a lot of pixels on a png will prevent the "saved" picture from looking "fuzzy" or "oddly" shaped (the size of it will be large, but the png looks good even when you zoom in). Hope that helps.

The goofy looking circles is an artifact of your window system; on my Mac OS X (Quartz) setup, default qplot gives great-looking circles. However, I recall that the points do look a little like yours on my Linux box at home...
Shape is customizable, though. The following gives squares rotated 45 degrees, for instance:
qplot(carat, price, data = diamonds) + scale_shape_identity() + geom_point(shape = 23)

Related

ggplot with the same width and height as ggsave(width=x, height=y)

Within R-Studio, I am generating plots with ggplot, then I save them with ggsave() for further use (I know, not ideal, but non-optional right now).
My problem is that when I generate the plot before saving it, R shows it to me in a particular size. With ggsave, I set width and height, so the elements displayed shift etc. I only see this after saving the plot.
I want R to show it to me before. I thus assume that I need to set the size of the plot within ggplot() somewhere , not in ggsave().
How can I do this in the least complicated fashion?
library(ggplot2)
ggplot(mtcars, aes(mpg,disp)) + geom_point() +
labs(title="Rocket science title that will get cut by ggsave")
ggsave("rocketScience.png", width=10, height=7, unit="cm")
You can use the set_panel_size() function from the egg package.
With this function you can fix the panel size of the plot. This can be very useful when creating multiple plots that should have the exact same plotting area but use varying axis labels or something similar that would usually slightly change the panel dimensions. Especially useful for presentations with seamless animations or publications. It also ensures the same dimensions in the preview and the saved plot.
p <- ggplot(mtcars, aes(mpg,disp)) +
geom_point() +
labs(title="Rocket science title that will get cut by ggsave")
#to view the plot
gridExtra::grid.arrange(egg::set_panel_size(p=p, width=unit(5, "cm"), height=unit(7, "cm")))
#to save the plot
ggsave(filename = "myplot.pdf", plot = egg::set_panel_size(p=p, width=unit(5, "cm"), height=unit(7, "cm")))
Within ggsave you can still manipulate the size of the whole "page" saved, but this will only influence the amount of white space around the plot. The actual panel size will stay fixed.
The example plot from above with 5cm or 15cm as width:
I don't believe this is achievable via ggplot settings; you might get around it if using RMarkdown, as you can set with and height of an output of a markdown chunk via fig.width and fig.height params.

Line graph is blurry, not clear & axes positioning difficulties in R

I am trying to plot (with ggplot2) a simple time-velocity graph in R, but my data looks messy. I am using Markdown Notebook.
I am using this base code for this:
ggplot(data, aes(x = time, y = velocity)) + geom_line() +
scale_x_continuous(name="Time (s)",
limits=c(min(data$time),max(data$time)),breaks=seq(0,3000,500)) +
scale_y_continuous(name="", limits=c(-1,max(data$velocity))) +
theme(axis.text = element_text(color="black"),axis.title = element_text(
color="black"))
This is how it looks like with the default settings:
After that, I tried to extend the figure horizontally, but then the labels became really small, and the data is still kind of blurry:
For this, I added the following (at the beginning of my Chunk):
{r fig1, fig.width=95, fig.asp = 0.15}
If I make the font sizes bigger the labels look okay, but the velocity graph stays the same (naturally). Does someone know a way to fix that? I thought that maybe this is because of my monitor, it has 4K resolution, but I'm not sure. I also wonder if anyone knows how to move the y-axis so it would start at the zero point of the x-axes (now there's a space, and it looks weird).
I am also open to suggestions on how to improve the visualization. :D Thank you in advance!
with the changed DPI (comment) it looks better in my opinion, but the figure is really small (I included the output window for reference):

Turn pixel image into scalable vector graphic in ggplot

Please note that im not interested in any kind of interpolation algorithms where you expand the amount of pixels and interpolate the new values.
I want to leave the world of pixel based images and am looking for some scalable vector image solution.
Is there a way to turn a pixel image in a ggplot into a color meshed smooth vector graphic?
The following pictures demonstrate what im aiming for.
and then smooth it out.
The images are taken from the following wikipedia article HERE
Please note that the original images from the article are SVG files. You can zoom in as much as you want and you always have smooth color transitions and no edges.
Some additional images and infos: HERE2
Here is some example Data of something that meets the first image "nearest"
library(ggplot2)
n = 5
pixelImg <- expand.grid(x=1:n,y=1:n)
pixelImg$value <- sample(1:n^2,n^2,replace = T)
ggplot(pixelImg, aes(x, y)) +
geom_raster(aes(fill = value)) +
scale_fill_gradientn(colours=c("#FFCd94", "#FF69B4", "#FF0000","#4C0000","#000000"))
If not in ggplot is there a way to do it outside of ggplot?
Look into the ggsave() function. It supports .svg files for vector graphics.

panel.border in ggplot2 ploting thicker lines in bottom and right side of the plot in CairoPDF device

I am using Cairo setting as
CairoPDF(file = "test2.pdf", width = 8.3, height = 11.7)
and theme setting is like this:
mytheme<-theme_bw() +
theme(plot.title = element_text(lineheight=.8, face="bold"),
text=element_text(size=11, family="Times New Roman"))+
theme(panel.border = element_rect(linetype = "dashed",
colour = "black"))+
theme(plot.margin = unit(c(1,1.5,1,1.5), "inches"))
I guess, this is a problem with my theme setting for ggplot. please give advice to fix it.
Thanks.
Using only theme_bw() I can reproduce the feature.
df <- data.frame(lab = c('D1','D2','D3'),y = c(4,8,10),x= c(1,2,3))
library(Cairo)
#CairoPDF(file = "test2.pdf", width = 8.3, height = 11.7)
library(grid)
library(ggplot2)
ggplot(df, aes(xmin = x-0.2, xmax = x + 0.2, ymin = 0, ymax = y,fill=lab)) +
geom_rect()+xlim(labels = as.character(df$lab))+ theme_bw()
dev.off()
It seems that the combination of Cairo and ggplot2, when we use a theme, creates some rendering problems.
One workaround is to save in simple pdf. I hope that others give a better solution using this reproducible example.
pdf(file = "test2.pdf", width = 8.3, height = 11.7)
This may or may not fix your specific problem, but I often have this issue with 'grid' objects with respect to the pixel scaling. I can frequently fix the problem by slightly reducing or increasing the size of the image.
I would troubleshoot by taking the following steps:
Print the object to a screen plotting device, and see if the problem is still there. If not, the problem exists between your plotting device (in memory) and the exporter (CairoPDF). In that case, print to the screen first, and then save the file. Make sure the screen plotting device has the appropriate size.
Try adjusting the size of the 'width' and 'height' arguments. If you see the problem changing or others popping up, then you have a problem with scaling between the size of the plotting device and the export file. Make sure you get a pixel-exact match between the two, by using the format of: windows(width=8.3, height=11.7, xpinch=72, ypinch=72) before you print the plot to screen.
Test exporting directly to a different format. It may be that the problem is only in the specific exporter. If you can print directly to a PNG file without a problem, then the issue is probably with the way the CairoPDF exporter is working. You may find it easier to simply use the other file format or to manually convert the PNG (or other file) into a PDF using a different program, like LibreOffice or ImageMagick.
Good luck!
I have the same problem with bottom and right panel borders becoming thicker when sending the above ggplot example code to svg, pdf, cairo_pdf etc.
Here's a workaround that I use in Inkscape which might give some insight to someone with better understanding of R and pdf/svg rendering:
In Inkscape:
select the panel border
object > ungroup (do this twice)
or keyboard shortcut shift+ctrl+G (x2)
So it seems the panel border is grouped with the panel background. Surprisingly rather than the bottom and right becoming thinner, the top and left become thicker. So actually it looks like OP question should be rephrased to "plotting thinner lines in top and left side of plot".

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

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.

Resources