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
Related
Saving the output of one of R's built-in examples for the filled.contour function to PDF:
pdf('test.pdf')
require("grDevices")
filled.contour(volcano, asp = 1)
dev.off()
produces a discretised result (see below). Is there any way to fix this? System info:
> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.1 LTS
Edit after the accepted answer:
The problem is actually reported in the documentation of the pdf function:
If you see problems with PDF output, do remember that the
problem is much more likely to be in your viewer than in R ...
Symptoms for which the viewer has been at fault are apparent
grids on image plots ...
Unfortunately the default viewers on most Linux and macOS
systems have these problems, and no obvious way to turn off
graphics anti-aliasing.
It's an odd place to document the problem because the subdivision into rectangular segments is clearly caused by filled.contour and not by pdf. Otherwise ggplot2's output would also suffer from the same issue.
That is likely a result of antialiasing: when you display the image, it draws squares one at a time. As they are being drawn, the edge is a mix of the square colours and the white background, so it is drawn lighter.
Unfortunately, this isn't something that's really under your control. It's the PDF previewer that is introducing the artifacts. See this page https://codedocean.wordpress.com/2014/02/03/anti-aliasing-and-image-plots/ for a discussion.
The recommendation there worked for me: use the png() device with type = "cairo". This gives bitmapped output rather than the vector output of a pdf().
png('test.png',type="cairo")
filled.contour(volcano, asp = 1)
dev.off()
Edited to add:
I don't think you can do better with filled.contour, but if you are willing to switch to ggplot2 graphics you can. When it draws filled contours it appears to do it using polygons, not the image style plot that filled.contour uses. This still shows the same antialiasing bugs in the previewer, but now the lines appear along the borders between colours, which is much less irritating. For example:
df <- data.frame(x = as.numeric(row(volcano)-1)/(nrow(volcano)-1),
y = as.numeric(col(volcano)-1)/(ncol(volcano)-1),
z = as.numeric(volcano))
pdf('test.pdf')
library(ggplot2)
ggplot(df, aes(x=x, y=y, z=z)) +
geom_contour_filled()
dev.off()
I don't know how to get the same palette as filled.contour uses, i.e. function(n) hcl.colors(n, "YlOrRd", rev = TRUE). Maybe someone else can show us in a comment.
I'm trying to learn R on my own and a doing my best to follow along the 'R for Data Science' book from O'Reilly.
I've made it to an exercise where I want to create a binned boxplot plot of a subset of data from the 'Diamonds' data set in the 'Tidyverse' package. When I run the code in Bootcamp (Windows 10) I get the result I expect with vertical boxplots in the set bin width. When I run it in Mac OS the boxplot appears to be horizontal and I can't for the life of me figure out why! See my result here:
I'm sure there are folks who will say just work in Windows, but my stubborn 2016 Macbook Pro in Windows 10 mode doesn't like connecting to my external, wired monitor. The monitor works perfectly in Mac mode though, so I don't really care to sink any more time into figuring out why the monitor connection is so unreliable in Windows 10 mode.
Personal problems aside, below is the identical code I've been using in RStudio for Mac and Windows modes. Attached/linked above is the output plot I'm getting when I run RStudio in Mac. The boxplots should be oriented vertically. Hoping someone can help me understand how to fix this weird occurrence when running RStudio and R in Mac mode!
Sorry if referring to mac/windows as different 'modes of operation' is incorrect. That's the only way I know to convey the difference!
Here's my code:
library(tidyverse)
smaller <- diamonds %>% filter(carat<3) # just want to see carats less than 3
ggplot(data=smaller,mapping=aes(x=carat,y=price))+
geom_boxplot(mapping=aes(group=cut_width(carat,0.1)))
In R-devel under linux I get the same result.
It works, though, if you do:
library(tidyverse)
smaller <- diamonds %>% filter(carat<3) # just want to see carats less than 3
ggplot(smaller, aes(x=carat,y=price))+
geom_boxplot(aes(x=cut_width(carat, .1)))
Alternatively, you could just plot it like this:
library(tidyverse)
diamonds %>% filter(carat<3) %>%
mutate(carat = cut_width(carat,0.1)) %>%
ggplot(., aes(x=carat, y=price))+ geom_boxplot()+
theme(axis.text.x = element_text(angle = 90, hjust = 1))
Created on 2020-04-09 by the reprex package (v0.3.0)
When I view this bar plot in R Studio, it appears as I intended (this is from a screenshot):
However, when I use the ggsave("filename.png") function, it appears with light-colored lines overlaid (may have to look closely to see):
I'm using R version 3.2.3, ggplot2 version 2.00, and R Studio version 0.99.486, on OS X 10.11.3.
Why might this be happening?
You should check out the Cairo library. I use it for crisp graphics in presentations and reports.
Cairo initializes a new graphics device that uses the cairo graphics
library for rendering. The current implementation produces
high-quality PNG, JPEG, TIFF bitmap files, high resolution PDF files
with embedded fonts, SVG graphics and PostScript files. It also
provides X11 and Windows interactive graphics devices. Unlike other
devices it supports all graphics features including alpha blending,
anti-aliasing etc.
I can't reproduce your example, but here's a similar one.
library("ggplot2")
pl <- ggplot(aggregate(mpg ~ cyl, mtcars, FUN=mean),
aes(x = cyl, y = mpg)) +
geom_bar(stat="identity", fill="red3") +
theme_bw()
library("Cairo")
CairoPNG("CairoCarPlot.png")
pl
dev.off()
Uploading the PNG, it looks like:
Throwing this out there in case it helps anyone else. I had this same issue when the stacked bar chart was made up of multiple values, I fixed it by grouping and then summarizing. I think for your data it would be:
df <- dataframe %>% group_by(Weekday) %>% summarize(percent=sum(percentage of tweets))
I'm running R version 3.0.1 and ggplot2 version 0.9.3.1 on a Windows machine and getting aberrant behavior from color bar legends -- the legend labels appear but the color bar mysteriously does not.
For example, if I run the following code:
d <- data.frame(x=rnorm(100), y=rnorm(100), z=rnorm(100))
ggplot(d, aes(x, y, color=z)) + geom_point()
I get this plot:
whereas on my other machine (a Mac running R version 2.15.2 and ggplot 0.9.3.1) the same code gives me this:
The behavior seems to apply only to color bars for continuous numerical variables -- legends for discrete factors appear as expected. I've tried reinstalling ggplot2. Anybody have thoughts on what's going on here? Thanks!
I found the problem, and a working solution, here. It's an issue with color rendering when accessing a server via Remote Desktop, and can be fixed in host settings.
I'm running R version 3.0.1 and ggplot2 version 0.9.3.1 on a Windows machine and getting aberrant behavior from color bar legends -- the legend labels appear but the color bar mysteriously does not.
For example, if I run the following code:
d <- data.frame(x=rnorm(100), y=rnorm(100), z=rnorm(100))
ggplot(d, aes(x, y, color=z)) + geom_point()
I get this plot:
whereas on my other machine (a Mac running R version 2.15.2 and ggplot 0.9.3.1) the same code gives me this:
The behavior seems to apply only to color bars for continuous numerical variables -- legends for discrete factors appear as expected. I've tried reinstalling ggplot2. Anybody have thoughts on what's going on here? Thanks!
I found the problem, and a working solution, here. It's an issue with color rendering when accessing a server via Remote Desktop, and can be fixed in host settings.