如何改善ggplot2中的锯齿线图?

Bar*_*run 8 r line ggplot2

他们的任何过程都是为了改善geom_line()生成的锯齿状线条,将多个点连接成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()
Run Code Online (Sandbox Code Playgroud)

使用这些代码创建的线条是非常像素化的锯齿形线条.他们是否有任何出路可以绘制更平滑的"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    
Run Code Online (Sandbox Code Playgroud)

小智 7

老问题,但令我惊讶的是没有人用这种方法更新:将 ggplot 代码包装在plotly::ggplotly() 中,如下所示:

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()
)
Run Code Online (Sandbox Code Playgroud)

然后导出为png:

平滑-b

为了满足 SO 的图像文件大小规范,这看起来仍然有点锯齿状,但原始 png 像黄油一样光滑。


Rom*_*rik 5

我猜你正在尝试平滑一条线。这是在你所追求的球场吗?

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()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

  • 谢谢你的帮助。但我试过这个。您甚至可以在这里注意到创建的线条是锯齿状的。我正在寻找的是高质量的折线图,就像在 MS Excel 中创建的一样(不是像素化的)。 (5认同)

Ale*_*lds 5

将图形输出到以矢量格式存储的文件(例如PDF或PostScript),然后使用ImageMagick或类似工具以高分辨率(150、300甚至600)将该矢量图像渲染为位图(PNG,JPEG等) dpi):

$ convert myGraphAsVector.pdf -density 300 myGraphAs300DpiBitmap.png
Run Code Online (Sandbox Code Playgroud)

文件大小将很大(随着您提高最终产品的分辨率,它会变得越来越大),但是在更高的像素密度下,位图的锯齿状将消失。