Reputation: 197
Is their any process to improve jagged lines produced by geom_line() joining multiple points into smooth presentable lines in ggplot2?
lai.se <- structure(list(DOS = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), .Label = c("D1", "D2", "D3"), class = "factor"),
DAS = c(31L, 84L, 113L, 132L, 160L, 35L, 82L, 108L, 126L,
146L, 37L, 83L, 94L, 113L, 134L), N = c(24L, 24L, 24L, 24L,
24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L), LAI = c(1.5879167,
4.3241667, 3.70375, 2.9704167, 0.1879167, 1.7679167, 3.7670833,
3.4104167, 2.7879167, 0.195, 1.3179167, 3.5233333, 3.1604167,
2.45875, 0.2758333), sd = c(0.4276323, 0.32478644, 0.34151596,
0.3338638, 0.09868611, 0.18551876, 0.38212767, 0.31431747,
0.35024189, 0.08836682, 0.16378616, 0.29256982, 0.28257326,
0.44131535, 0.09536733), se = c(0.08729008, 0.06629675, 0.06971165,
0.06814966, 0.02014422, 0.03786886, 0.07800148, 0.06415978,
0.07149283, 0.0180378, 0.03343271, 0.05972057, 0.05768002,
0.09008312, 0.01946677), ci = c(0.18057328, 0.13714529, 0.14420954,
0.14097832, 0.04167149, 0.0783377, 0.16135836, 0.13272463,
0.14789418, 0.03731404, 0.06916083, 0.1235414, 0.11932022,
0.18635113, 0.04027009)), .Names = c("DOS", "DAS", "N", "LAI",
"sd", "se", "ci"), class = "data.frame", row.names = c(NA, -15L
))
ggplot(lai.se, aes(x=DAS, y=LAI, colour=DOS)) +
geom_errorbar(aes(ymin=LAI-se, ymax=LAI+se), colour ="black", size =.5, width=1, position=position_dodge(.9)) +
geom_line() +
geom_point()+ theme_bw()
Lines created using these codes were very much pixelated zig-zag lines. Is their any way out to plot smoother "solid"
lines (not zig-zag looking)?
> sessionInfo()
R version 2.14.2 (2012-02-29)
Platform: i386-pc-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=English_India.1252 LC_CTYPE=English_India.1252 LC_MONETARY=English_India.1252 LC_NUMERIC=C
[5] LC_TIME=English_India.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggplot2_0.9.2.1
loaded via a namespace (and not attached):
[1] colorspace_1.1-1 dichromat_1.2-4 digest_0.5.2 grid_2.14.2 gtable_0.1.1 labeling_0.1
[7] MASS_7.3-17 memoise_0.1 munsell_0.4 plyr_1.7.1 proto_0.3-9.2 RColorBrewer_1.0-5
[13] reshape2_1.2.1 scales_0.2.2 stringr_0.6.1 tools_2.14.2
Upvotes: 13
Views: 34180
Reputation: 544
Old question but I was surprised no one had updated with this method: wrap the ggplot code in plotly::ggplotly() like so:
plotly::ggplotly(
ggplot(lai.se, aes(x=DAS, y=LAI, colour=DOS)) +
geom_errorbar(aes(ymin=LAI-se, ymax=LAI+se), colour ="black", size =.5,
width=1, position=position_dodge(.9)) +
geom_line() +
geom_point()+
theme_bw()
)
and then export as png:
This will still look a little jagged to meet SO's image file-size spec, but the original png is smooth as butter.
Upvotes: 7
Reputation: 2374
As others have mentioned, a vector-based image format like PDF or SVG might be the best way to go. If you want to use a bitmap, like PNG, then you can increase the resolution of your image with the res
option of png()
.
png("myplot.png", width = 1800, height = 1800, res = 600)
This will produce a 3 inch by 3 inch image (in real-world dimensions) at 600 dpi. In my experience, the file size was still reasonable for relatively simple graphs.
See this blog post for more ideas on how to improve your images:
http://blog.revolutionanalytics.com/2009/01/10-tips-for-making-your-r-graphics-look-their-best.html
Upvotes: 3
Reputation: 96967
Output your graph to a file stored in a vector format, such as PDF or PostScript, and then use ImageMagick or similar to render that vector image to a bitmap (PNG, JPEG, etc.) at high resolution (150, 300 or even 600 dpi):
$ convert myGraphAsVector.pdf -density 300 myGraphAs300DpiBitmap.png
The file size will be large (and get increasingly larger as you increase the resolution of the final product), but the jaggedness of the bitmap will appear to go away at higher pixel densities.
Upvotes: 5
Reputation: 23550
In your answer to my comment you confirm that you want to anti-alias your lines (not change the curve of your plot).
I think the only way to do that in R is to use Cairo and here is a tutorial on how to do that with circles, i hope you can adapt that to do it with lines:
http://www.r-bloggers.com/weekend-art-in-r-part-3/
Upvotes: 4
Reputation: 70643
I'm guessing you're trying to smooth a line. Is this in the ball park of what you're after?
ggplot(lai.se, aes(x=DAS, y=LAI, colour=DOS)) +
geom_errorbar(aes(ymin=LAI-se, ymax=LAI+se), colour ="black", size =.5, width=1, position=position_dodge(.9)) +
geom_smooth() +
geom_point()+ theme_bw()
Upvotes: 6