R low quality graph in graphic device - r

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?

Related

Not seeing any plots showing up in RStudio

I suspect my inability to see any results from my attempts to plot (using ggplot in RStudio) are related to the fact that when I check dev.cur I get null device. Would love help figuring out how to get plotting working in RStudio on my M1 Macbook Pro running Monterey.
I had been assigning my plots to variables, in which case they are not rendered at assignment time (and don't show up.) E.g.
plt <- ggplot(mpg, aes(x = displ, y = hwy)) + geom_point()
In which case rendering the plot simply required evaluating the variable
plt
Problem solved. Thanks to the commenters for getting me on the right path.

Save plot exactly as previewed in the "Plots" panel

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!

Why can´t I reproduce a simple grouped violin plot from a tutorial

I´d like to reproduce the simple grouped violin plot from the tutorial here:
http://www.sthda.com/english/wiki/ggplot2-violin-plot-quick-start-guide-r-software-and-data-visualization
It uses the built in ToothGrowth dataset so it should be easily reproducible.
I use this code (in a blank R script after restarting RScript Software)
# Change violin plot colors by groups
ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
geom_violin()
And I get this result, totally different from the tutorial example.
My Result:
Tutorial Example = lower picture
Everything else up to this point in the Tutorial was easily reproducible and a great help in learning :)
So what is my mistake? I want it to look like the example in the tutorial.
Interestingly I can easily reproduce the same kind of violin plot using the code provided as an example in this question
Split violin plot with ggplot2
My Specs:
Win7 64bit (German Version, Software all english) -
RStudio newest Version 1.0.143 -
R newest Version Version 3.40.7034.0 -
The tutorial example seems to have been produced with
ggplot(ToothGrowth, aes(x=factor(dose), y=len, fill=supp)) + geom_violin()

R - ggplot2 poor quality of charts generated on Windows

Why lines in a chart generated from ggplot2 on Mac looks so smooth and round, but on Windows they look so sharp and edgy. Is there any option to fix that on Windows instead of going to the Apple Store?
For example, chart generated on Mac:
and a chart generated on Windows:
Edit:
Due to #thestatnoob answer, this is a plot generated from iris dataset on R 3.0.2, RStudio 0.99.486, chart resolution: 640x480.
data(iris)
library(ggplot2)
ggplot(data = iris) +
geom_density(aes(x = Sepal.Length, y = ..scaled.., fill = Species), alpha = 0.5)
Anti-aliasing options from the Cairo package in R solved the issue
This might be due to a rendering error rather than anything else. R graphics do look more low res and "edgy". Try plotting the exact same dataset in both OS's with the same settings, e.g. PNG/PS/PDF with identical resolution. If it turns out that the Windows plot doesn't look great, an alternative solution would be to:
Use RStudio, which is a GUI for R and produces "smooth" graphics, even on Windows
Use R in a MS PowerShell/command prompt

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

Resources