Save plot exactly as previewed in the "Plots" panel - r

I know that a very similar question already exists here, but the provided answer did not work for me.
This is my usual workflow: I generate a plot and adjust the size of the plot in the "Plots" Panel of RStudio until I am satisfied. I then call dev.size() to get the exact size.
Afterwards, I save the plot with ggsave(...,dpi=300) and specify the previously determined width and height. The problem is, that after saving, the plot looks completely different, especially the text sizes.
If I use the "Export" option from RStudio the plot looks exactly as it does in the preview, but the quality is quite bad and doing this manually is tedious.
Here is the picture, that hopefully illustrates what I mean:
The code I use to save the plot looks like this:
library(ggplot2)
ggplot(mtcars, aes(x = mpg, y = wt)) +
geom_point() +
facet_grid(vs + am ~ gear, margins = "vs") +
theme_Publication()
ggsave("plot.png", width=4, height=3.2, dpi=300)
I would love to know, if there is an option to "programmatically" save a plot which exactly recreates the "Plots" preview in high quality.
Thanks a lot in advance!

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):

R low quality graph in graphic device

I'm trying to figure out what is going on with my plot render.
Yesterday I produced a geom_smooth with ggplot2 but the output was very low.
for the example when I render this code:
library(ggplot2)
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE)
I got this graph, and the line is not "sharp" at what it always does:
Crude render
Moreover, when I use the command ggsave () to produce the plot, the line renders look great as ever.
the render is the same if I use R GUI or Rstudio
I try to reinstall R and Rstudio, nothing do
I could not reproduce this "bug" on another computer and my Linux server
Did someone have any clue?

How can I prevent ggplot2 from overflowing window?

I'm running R 3.1.1 with ggplot2_1.0.0. I'm having trouble with the default layout. I would expect ggplot to do a better job avoiding the overflow you see here. It feels like ggplot thinks that my device is much larger than it actually is. I'm running this on Ubuntu 14.04, FWIW.
For something more reproducible, I run this:
ggplot(mtcars, aes(x=cyl,y=hp,color=as.factor(mpg))) + geom_point()
And get this, where the legends have really huge boxes.
Here is one solution. I used the cut argument to specify ranges over mpg. Using cut will automatically coerce the variable to a factor as well.
library(ggplot2)
ggplot(mtcars, aes(x=cyl,y=hp,color=cut(mpg,quantile(mpg,seq(0,1,by=.25))))) +
geom_point() + scale_color_discrete(name="Legend Title (mpg breaks)")

Oddly shaped markers in ggplot2

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)

Resources