2013-01-14 67 views
6

他們的任何過程可以改善geom_line()在ggplot2中將多個點合併爲平滑的可顯示線條所產生的鋸齒線?如何改善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() 

使用這些代碼創建的線是非常像素化的曲折線。他們的任何出路,以繪製更平滑"solid"線(不曲折看)?

> 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  
+0

你的意思抗鋸齒線這樣?:https://en.wikipedia.org/wiki/Xiaolin_Wu %27s_line_algorithm –

+0

我同意@eznme,你可能指的是可能與你打印圖形的設備有關的反鋸齒問題,而不是ggplot2本身。解決這個問題將取決於你的平臺和系統配置,這就是爲什麼我要求'sessionInfo()'的輸出。 – joran

+0

@joran我已經添加了sessionInfo()。請幫助我。我正在使用RStudio(0.97.248) – Barun

回答

4

我猜你試圖平滑一條線。這是在你之後的球場嗎?

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

enter image description here

+1

感謝您的幫助。但我已經嘗試過了。你甚至可以在這裏注意到創建的線條是鋸齒狀的。我正在尋找的是高質量的線圖,如MS Excel中創建的那些(不是像素化)。 – Barun

4

輸出的圖表存儲在一個矢量格式的文件,諸如PDF或PostScript,然後使用ImageMagick或相似矢量圖像渲染爲位圖(PNG,JPEG等以高分辨率(150,300,甚至600 DPI)):

$ convert myGraphAsVector.pdf -density 300 myGraphAs300DpiBitmap.png 

的文件大小將是大的(並獲得越來越大的爲您提高最終產品的分辨率),但該位圖的鋸齒。似乎會以更高的像素消失密度。

4

在你對我評論的回答中,你確定你想要anti-alias你的線條(不改變你的情節的曲線)。

我認爲做R中的唯一方法是使用開羅,這裏是如何做到這一點與圓,我希望你能適應,要與線做一個教程:

http://www.r-bloggers.com/weekend-art-in-r-part-3/

3

正如其他人所說,像PDF或SVG這樣的基於矢量的圖像格式可能是最好的選擇。如果你想使用像PNG這樣的位圖,那麼你可以用選項png()來提高圖像的分辨率。

png("myplot.png", width = 1800, height = 1800, res = 600)

這將在600 dpi的產生由3英寸的圖像3英寸(在現實世界的尺寸)。根據我的經驗,對於相對簡單的圖形,文件大小仍然合理。

看到這個博客帖子,關於如何提高你的圖像更多的想法:

http://blog.revolutionanalytics.com/2009/01/10-tips-for-making-your-r-graphics-look-their-best.html