Saving filled contour plots to PDF in R gives pixelated results - r

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.

Related

Transparent Polygon not being drawn in R 4.1.2? (but is drawn in R 4.0.3)

I was wondering if anyone else has encountered this?
I was re-running an old script in R 4.1.2 that had worked correctly in R 4.0.3 and was baffled by some transparent filled polygons not being drawn in the plots. After closer investigation, if the fill colour of the polygons were changed to a non-transparent colour (I was using the "adjustcolor" function, so I just changed the "alpha" option to equal 1) the polygons would however be drawn.
The following code examples will hopefully illustrate the problem - I am including a specific set of x,y coordinates giving me the problem (as I wonder if the specific complexity or shape of the polygon is part of the issue?)
running this in R 4.0.3
time=seq(0,360,by=1)
lower=c(0,9.808413838,16.73164571,18.76349566,18.39009377,17.02697093,15.841783,14.40087799,13.44195299,12.33561621,10.81540015,9.483312805,7.772220814,26.52906075,28.47845246,27.2619361,25.99168124,24.41197181,21.92306555,19.37183496,16.98551416,14.89342689,12.78656155,10.35230535,8.379609529,19.23918021,17.56574333,16.96574759,15.48243068,13.96697171,12.15929731,10.49247456,8.739319275,7.019079443,5.62710167,4.504181154,3.659087275,15.55871575,14.38943941,13.67748891,12.79299843,11.49848585,10.09104001,8.779516045,7.59585163,6.39048855,5.12093291,4.098618087,3.277594375,14.69391639,13.73897617,12.79468771,12.07063769,10.65682969,9.286941718,8.015166137,6.916478565,5.765415956,4.577256813,3.615388525,2.85415718,14.23825007,13.45717759,12.61987982,11.88914909,10.38071466,9.064098314,7.822741548,6.75030935,5.673792771,4.538753087,3.585044609,2.829329539,14.07813214,13.21393248,12.27663099,11.3946419,10.0191356,8.643706628,7.400416609,6.322833357,5.216156272,4.085693947,3.186810227,2.482541615,13.79301681,13.02451854,12.14948757,11.26249433,9.85974012,8.509470463,7.285462798,6.239612509,5.15732224,4.057991935,3.165265425,2.465489432,13.67019144,12.80324751,11.82771572,10.80004119,9.539992631,8.130486693,6.906005578,5.809265297,4.662903175,3.656752502,2.816776712,2.166464812,13.29163314,12.64020931,11.72166635,10.68866272,9.405388881,8.018218732,6.810824409,5.748154362,4.621878498,3.635726524,2.800635347,2.154070051,13.14083827,12.43063765,11.41735337,10.34778747,9.090105356,7.665861878,6.465622063,5.336040273,4.176715721,3.268006713,2.494918602,1.903210523,13.05748144,12.28903912,11.32830719,10.21091475,8.976876914,7.570441091,6.385254864,5.295802489,4.145233249,3.243382305,2.482784428,1.888787068,12.91746197,12.08635429,11.03947335,9.95916774,8.655292565,7.241351112,6.064677001,4.840125361,3.746561123,2.898820495,2.214058495,1.710402991,12.75826178,11.96321316,10.96436563,9.839597279,8.557927824,7.159948195,5.996593755,4.806818289,3.720796451,2.878896528,2.204916035,1.699038473,12.62867702,11.77195167,10.68951368,9.601906021,8.254773767,6.857114381,5.697973305,4.394775862,3.363139658,2.572307266,1.967380334,
1.501234263,12.38578403,11.66420086,10.62612083,9.497114911,8.17080331,6.787435364,5.640152024,4.37043521,3.344520684,2.558078722,1.956504468,1.492699679,12.26939161,11.48022213,10.36411335,9.271817581,7.884156682,6.50324234,5.277289723,3.990264544,3.016518293,2.280548458,1.750173056,1.317093039,12.14751232,11.38731851,10.31028995,9.179781506,7.81159112,6.443459157,5.250539845,3.970013026,3.001049123,2.268874016,1.744983776,1.306614005,12.06035203,11.20981234,10.06010246,8.966946699,7.539857826,6.175632453,4.853333824,3.628141193,2.713359341,2.068709549,1.557875714,1.174585372,11.96878509,11.12938629,10.0143305,8.885907081,7.477038777,6.124251218,4.831890392,3.612065593,2.701238472,2.059110468,1.553932576,1.166263989,11.88469849,10.95757123,9.774793319,8.681525483,7.220171843,5.871305363,4.470292036,3.304181511,2.45540786,1.905969062,1.388797173,1.049200441,11.80387139,10.8854499,9.735699353,8.610313333,7.165568345,5.82706986,4.453361903,3.291643105,2.445262757,1.89821737,1.38568409,1.042485695,11.72243174,10.71861504,9.517479086,8.414867943,6.925553127,5.587745592,4.119464393,3.009868173,2.28547583,1.757736182,1.241279051,0.938078818,11.64044168,10.65434568,9.472502662,8.351797851,6.878099368,5.549607215,4.105653264,2.999737652,2.277101741,1.751881,1.23899594,0.932613544,11.57175714,10.49209428,9.287952554,8.164481211,6.649091841,5.257391429,3.799446568,2.775234675,2.12997825,1.590491902,1.111079106,0.839303237,11.46549158,10.4365872,9.223098009,8.108670731,6.607799624,5.242461177,3.788661351,2.766158165,2.123182548,1.588045554,1.109244026,0.834988343,11.40053157,10.27859688,9.072397615,7.928751173,6.388968935,4.905238095,3.505043788,2.611309117,1.98733038,1.441269475,0.995783544,0.75156961,11.30406599,10.22963857,9.010823429,7.879263189,6.352998085,4.893427179,3.496602946,2.603793181,1.981761313,1.439237889,0.994377054,0.748090146,0.567241261,0.394235453,0.274734085,0.191506461,0.136819214,0.102492417,0.076809002,0.05757791,0.043211435,0.03245718,0.024380428,0.0179901,0.013068467,0.009490345,0.006895657,0.005014055,0.003645662,0.002648651,0.001923437,0.001397503,0.001016078,0.00073862,0.000535927,0.00038477)
upper=c(0,69.5831179,68.95384572,65.19871687,63.07577135,58.42865679,57.12603158,52.54380606,49.80408744,47.1150641,44.57133065,42.50061429,40.12088379,103.424341,104.3845494,98.89831002,91.34843181,88.84275747,84.27690912,81.01326165,76.27023447,73.44058199,70.71161721,68.04732037,65.44032643,87.45554273,85.06801642,79.7651964,75.40844245,72.05345779,68.92556376,65.42942743,61.75284589,58.02698326,54.34382546,52.84792331,51.42992262,75.16729319,72.65102945,68.84323484,65.09182036,61.52029261,59.36494361,57.77386683,56.22515382,54.71824955,53.25206966,51.82558844,50.4378052,68.76602884,66.77372286,63.15917145,61.4114633,59.68395169,57.99753756,56.35678013,54.76218733,53.21302921,51.70803198,50.2269393,48.134254,64.36181471,64.20492356,61.68563857,59.98131512,58.29587399,56.65025739,55.04906445,53.49284177,51.83784643,49.67908545,47.6101092,45.62760452,62.85774639,62.79427979,60.38113463,58.62263405,56.88725923,55.19578325,53.55261705,51.39042376,49.13824363,47.16453174,46.17045897,45.19746231,61.27936266,61.4518984,58.87235611,57.15947146,55.46830646,53.81967871,51.79142852,49.5275121,47.9177436,46.90815091,45.91986835,44.95252799,60.07617068,60.07271883,57.5673766,55.80611757,54.07075266,52.26734316,49.88358797,48.32564824,47.25585014,46.2095179,44.89292001,43.51016856,58.51300191,58.28929641,56.10838671,54.39302113,52.70208394,50.77446337,48.76534571,47.68720386,46.54794077,45.14959263,43.76928012,42.27209698,57.36343872,57.05040503,54.86599819,53.10625386,51.37506434,49.28146501,47.90665842,46.60013805,45.13259089,43.69412909,42.26887464,40.49035876,55.89571938,55.58915546,53.50024116,51.78535869,50.09753748,48.17001319,46.93674103,45.45862915,44.02718542,42.60569483,41.21596336,39.4557192,54.8328106,54.56457113,52.34925508,50.59271573,48.86733765,47.21183505,45.66074309,44.15653969,42.69243559,41.23545996,39.73212956,37.89445351,53.47851817,53.29339,51.09255541,49.37913327,47.69537412,46.08972382,44.57149957,43.10326886,41.66674364,40.24475049,38.80509503,37.01027617,52.51175986,52.40094238,50.04065186,48.28749161,46.56808449,44.93851523,43.39302923,41.90069023,40.42780704,38.98735562,37.41389322,35.60777089,
51.30159646,51.2627496,48.89308183,47.18100591,45.50119383,43.90853978,42.3985256,40.94043231,39.49999382,38.09256367,36.60021508,34.83333874,50.7351443,50.4658534,47.93672679,46.18636323,44.51055451,42.85259118,41.31679903,39.83605288,38.36581933,36.94124287,35.3335624,33.55654161,49.7637448,49.43261039,46.8907618,45.17941649,43.56552618,41.91491291,40.41274647,38.96441547,37.52809435,36.13458273,34.60987625,32.86921435,49.24840226,48.71211684,46.0215309,44.27308819,42.65277469,40.9475555,39.42079796,37.95097972,36.48863922,35.07906893,33.44720674,31.69768183,48.35383707,47.76695908,45.06707824,43.35567734,41.76143678,40.09420351,38.59926686,37.16007812,35.73136168,34.35100458,32.79851973,31.08288885,47.89163697,47.11085668,44.27529294,42.52783649,40.90546502,39.20666414,37.68815526,36.22848067,34.77600144,33.38054462,31.72538683,30.00216038,47.06882018,46.24206451,43.40152396,41.68929446,40.07571614,38.49503705,36.93988943,35.50918527,34.08921341,32.72127616,31.14079892,29.44929015,46.64917673,45.64109217,42.67754328,40.93022418,39.28040633,37.67539487,36.10034031,34.65006428,33.20779036,31.82564245,30.14604325,28.44814687,45.88871596,44.8394584,41.87484116,40.16109102,38.51291531,37.03828062,35.41679941,33.99396952,32.58274311,31.22657312,29.61701944,27.94888534,45.50555999,44.2923426,41.21025376,39.46232203,37.7768702,36.27466594,34.64048329,33.19784048,31.76645627,30.39691112,28.69157303,27.01818763,44.80063562,43.75881127,40.47017228,38.75430117,37.08404461,35.68983062,34.01400367,32.59850933,31.19553653,29.85056985,28.21113878,26.56574192,44.44935087,43.32040092,39.85769954,38.10851161,36.40908135,34.97602751,33.29358419,31.85785362,30.43680458,29.07925528,27.34742269,25.6978364,43.79426015,42.81507034,39.33241158,37.4544847,35.78434927,34.43769277,32.71749864,31.30887793,29.91351121,28.5792679,26.90975287,25.28653622,23.76165468,22.32814478,20.95200122,19.64681864,18.42459558,17.27927326,16.22289482,15.248767,14.33347408,13.4734914,12.66548077,11.90629053,11.19292688,10.52260887,9.892751818,9.300912878,8.744781708,8.222186384,7.731091567,7.269588774,6.855227294,6.535482722,6.230707621,5.940198023)
plot(time, upper, col='white', ylab='random', log='y')
polygon( c(time, rev(time)),
c(lower, rev(upper)),
col=adjustcolor('dodgerblue', alpha=0.25), border=NA)
gives the following plot
4.0.3
running in R 4.1.2 gives the following (polygon apparently not drawn)
4.1.2_plot1
...but changing the polygon fill color to non transparent as follows,
polygon( c(time, rev(time)),
c(lower, rev(upper)),
col=adjustcolor('dodgerblue', alpha=1), border=NA)
does draw the polygon
4.1.2_plot2
If a simpler polygon is drawn in 4.1.2 (e.g. just a rectangle in the following, albeit with the same number of x,y coordinates as before)
polygon( c(time, rev(time)),
c(rep(10,length(time)), rep(25,length(time))),
col=adjustcolor('dodgerblue', alpha=0.25), border=NA)
then the transparent polygon is drawn in R 4.1.2 without issue.
4.1.2_plot3
It's no issue for me to go back to R 4.0.3 to get what I need done, however does anyone have any ideas what might be happening here?
Many thanks in advance :-)
If you are running R under Windows, then the behaviour that you are seeing is very likely related to this bug in R 4.1.x. The problem arises only when drawing polygons outside of the plot region, which explains why your attempt with the rectangle succeeded. The bug has been fixed in R-devel (r81114). You can download the development version of R for Windows here.

Lines overlaid on ggplot2 bar plot when saved with ggsave()

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

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

Why does this R ggplot2 code bring up a blank display device?

While SO is not usually used for help with bugs, this one shows particularly simple and particularly annoying behavior. If you are a ggplot2 user, you can reproduce it in 10 seconds or less.
As this GitHub issue: ggplot_gtable creates blank display says, the following code
library(ggplot2)
stat = qplot(Sepal.Length, Petal.Length, data = iris, color = Species)
ggplot_gtable(ggplot_build(stat))
will produce a blank device. Note that since ggplot2 is a graphics library, some commands can bring up a graphics device to show the relevant plot. Specifically, just running ggplot_build(stat) will bring up a plot. But that doesn't explain this behaviour.
I'm not sure how to debug this (print statements don't really seem appropriate or useful), and the ggplot2 development community seems to be on vacation or something, so if any experienced R user can offer suggestions on how to debug this effectively, I would appreciate it. This is a trivial but incredibly annoying bug. Every time I run code which looks like the snippet, it brings up a blank device which the display switches focus to, and so I have to click it away before I can continue.
It is possible that I'm doing something horribly wrong and am the only person who can reproduce this bug. It is also possible, for some reason I can't imagine, that this is normal behavior. If you think either of these things are true, please let me know.
I'm using ggplot2 0.9.3.1 (latest release) on Debian squeeze.
Some grid grobs have units that can only be resolved at drawing time, that is to say once a device window is open. This is the case of text grobs, for instance, as their size can depend (in the most general case) of the cex and fontsize arguments of the parent(s) viewports (which can be nested, etc.)
library(grid)
widthDetails(textGrob("hi"))
The current version of ggplot2 appears to use widthDetails in the code to build the legend grobs (guides_build function). It is conceivable that this could be replaced by grobWidth, unless the grob size is too convoluted.
I wonder if it is related to this thread from 3 years ago on R-Help with this workaround from #G.Grothendieck (copied material follows)
https://stat.ethz.ch/pipermail/r-help/2010-December/263754.html
library(lattice)
library(zoo)
df <- data.frame(y = matrix(rnorm(24), nrow = 6), x = 1:6)
xyplot(zoo(df[1:4], df$x), type = "p")
plot.object <- xyplot(zoo(df[1:4], df$x), type = "p")
# problem: a Quartz device is opened (on Mac OS X 10.6)
Grothendieck wrote in response:
This also opens up a window on Windows. It occurs within lattice
when lattice issues a trellis.par.get . A workaround would be to open
a device directed to null. On Windows this would work. I assume if
you use "/dev/null" it would work on your machine.
png("NUL")
plot.object <- ...
dev.off()
I am still having this issue with R 3.6.1 in 2019.
My lack of reputation doesn't allow me to comment. So I am writing yet another answer:
Things become a bit tricky if your plot uses non-standard fonts, say
library (ggplot2)
stat = qplot(Sepal.Length, Petal.Length, data = iris, color = Species) +
theme(text = element_text (family="DejaVu Sans"))
As was explained in the answer by baptiste, the output device is needed in order to determine some text dimensions. With the non-standard font, however, you need the appropriate output device. If you just use a pdf(file=NULL) as baptiste said, you get the dimensions wrong (and a warning that 'DejaVu Sans' was substituted by another font which is responsible for the false dimensions). In order to resolve this, I had to open an output device that is able to render the non-standard font (with a temporary file, say):
cairo_pdf (tempfile (fileext=".pdf"))
grob = ggplot_gtable (ggplot_build (stat))
dummy = dev.off ()
Hope this information is useful.
Problem seems to be caused by "color = Species".
If replaced by "group = Species", then no more blank display device.

Cannot change text size in rgl plot

I have a problem changing the text size of a 3d plot I generated with the package rgl. Everything works fine, but I can't effectively change the cex properties of an 3d object. I run R 2.14.1 (2011-12-22) under 64bit Ubuntu 10.04 LTS (2.6.32-37-generic). As an example see the following code (I found on Stack Overflow):
library(rgl)
set.seed(1001)
n <- 20
text3d(runif(n),runif(n),runif(n),LETTERS[1:n],cex=seq(0.5,5,length=n))
The letters have all the same size when I plot them.
Any help is highly appreciated! Best Regards.
Frieder Schillinger
Weird. It works for me on Ubuntu 10.04 (rgl 0.92.829) [this is my code, I think]. Can you tell us your rgl version? By "plot" do you mean rendering the picture on the screen, or producing PostScript output? This was fixed in the most recent (0.92.829) version:
> news(grepl("text",Text),package="rgl")
Changes in version 0.92.829:
o rgl.postscript() now adjusts the size of text following the cex setting.
The font and family settings are still ignored.
o Transparency in material textures was not always rendered properly.
?text3d
There is no cex argument. You should be using "scale".

Resources