【问题标题】:Improving graph quality while exporting in r在 r 中导出时提高图形质量
【发布时间】:2021-03-29 19:06:43
【问题描述】:

我编写了以下代码,用于在一段时间内比较不同变量。代码工作正常,但唯一的问题是当我将文件输出为“jpeg”时,线条不平滑,我的箭头也不像我喜欢的那样平滑,换句话说,图形感觉质量非常低。但是当我将它输出为“pdf”时,我会得到平滑的线条,并且图形的质量更高。但是 pdf 文件的文件大小很大,我需要将这些图表插入到 word 文件中。我发现将 jpeg 附加到 word 文件中相对容易。那么jpeg格式是否可以提高图像质量。我尝试在 jpeg() 中使用 res 参数,但它不会输出图形,因为它显示在 rstudio 中。

我将不胜感激。谢谢!

代码:

library(shape)
library(Hmisc)
### samples ######
xaxs = seq(1,30,length=30)
precip = sample(200:800, 30)
ero = sample(0:10, 30, replace = T)
#########

svpth = getwd()
nm = "try.jpeg"
jpeg(paste0(svpth,"/",nm), width=950 , height =760, quality = 200, pointsize =15)
par(mar= c(5,4,2,4), oma=c(1,1,1,1))
plot(xaxs,precip, type = "p", pch=15, col="green", ylim = c(200,1000),
     xlab = "Year" , ylab = "", cex.main=1.5, cex.axis=1.5, cex.lab=1.5)
lines(xaxs, precip,lty =1, col="green")
# xtick<-seq(0,30, by=1)
# axis(side = 1, at=xtick, labels = FALSE )
minor.tick(nx=5, ny=2, tick.ratio=0.5, x.args = list(), y.args = list())
mtext("Depth (mm)", side = 2, line = 2.7, cex = 1.5)
par(new=T)
plot(xaxs, (ero * 10), ylim = c(0,max(pretty(range((ero * 10))))+20), type = "p", pch=20, cex=1.5, col="red", axes = F, xlab = "", ylab = "")
lines(xaxs, (ero * 10),lty =2, col="red")
axis(side = 4, at=pretty(range((ero * 10))), cex.axis = 1.5)
# mtext("Erosion (t/ha/yr)", side = 4, line = 2.2, cex = 1.5)
mtext(expression(paste("Erosion (t ", ha^-1, yr^-1, ")")), side = 4, line = 2.7, cex = 1.5)
legend("topleft", legend = c("Precipitation","Erosion"), lty = c(1,2), pch = c(15,20), col = c("green","red"), cex = 1.6, bty = "n")
####arrow
Arrows(7, 85, 11, 90,lwd= 1.1)
Arrows(26, 85, 21, 90, lwd= 1.1)
txt = "High erosion rates in \nwheat-planting years"
xt = 16
yt = 85
text(xt, yt, labels = txt, family="serif", cex = 1.23)
sw = strwidth(txt)+1.4
sh = strheight(txt) +6
frsz = 0.38
rect(xt - sw/2 - frsz, yt - sh/2 - frsz, xt + sw/2 + frsz, yt + sh/2 + frsz-1)
# legend(15,80, legend = c("High erosion rates in \nwheat-planting years\n"),
       # xjust = 0.5, yjust = 0.5)
dev.off()

【问题讨论】:

    标签: r


    【解决方案1】:

    它没有使用base R,但这会生成一个svg,它比jpeg小,并且会创建一些漂亮的图像。 MS Word 对 svg 也没有任何问题。 svg——18 kb; jpeg-- 592 kb 用于相同的图像。 如果它有效,则使用,如果无效,那么,也许其他人可以使用它?这不会显示在 RStudio 的绘图窗格中,它将显示在查看器窗格中。 在代码之后,我有一张将绘图保存在 RStudio 的查看器窗格中的图像。

    library(plotly)
    
    df = data.frame("Year" = xaxs, "Depth" = precip, "Erosion" = ero *10)
    
    p = plot_ly(df) %>% 
      add_trace(x = ~Year, y = ~Depth, 
                type = 'scatter', mode = 'lines', # to have both the points and lines use 'lines+markers'
                name = "Depth",
                line = list(shape = "spline",     # smooth the curves in the lines (not that effective with lines+markers)
                            color = "green")) %>% 
      add_trace(x = ~Year, y = ~Erosion, 
                mode = 'lines', 
                name = "Erosion",
                yaxis = "y2",                     # second y axis
                line = list(dash = 'dash',        # dash the lines
                            shape = "spline",     # smooth the curves in the lines
                            color = "red")) %>%   # without "lines+markers" spline will smooth out the points of the line
      add_annotations(inherit = F,        # add the arrows at the top of the plot
                      x = list(12, 18),   # this is plot coordinates
                      y = list(800, 800),
                      ax = list(-60, 60), # this is pixels
                      ay = list(10, 10),
                      showarrow = T,
                      text = "") %>% 
      add_annotations(inherit = F,        # add the textbox at the top of the plot
                      x = 15, y = 800,
                      ax = 0, ay = 0,
                      showarrow = F,
                      bordercolor = 'black',
                      text = "High erosion rates in\nwheat-planting years") %>% 
      layout(yaxis2 = list(overlaying = "y", side = "right",  # add labels
                           title = paste0("Erosion (t ", 
                                          "ha<sup>-1</sup>", 
                                          "yr<sup>-1</sup>", 
                                          ")")),
             yaxis = list(title = "Depth (mm)"),
             legend = list(x = .1, y = 1000),
             margin = list(r = 80))         # right margin space for label 
    

    要保存它,请添加功能。直到您将鼠标悬停在它们上方,最后图像中绘图顶部的图标才会显示。我想你可能会发现,如果你使用它,你所拥有的高度/宽度规格不再是最合适的了。

    (p <- p %>% config(            # save the plot; add a save function to the plot
      toImageButtonOptions = list(
        format = "svg",
        filename = "try",
        width = 950,
        height = 760)) # end config
      ) # end () for print simo object assignment
    

    情节。此图片的宽度和高度为 950 x 550。

    【讨论】:

    • 您好,感谢您的分享。虽然我将来肯定会使用它。在这种情况下我不能使用它,因为我的手稿中有其他情节是我通过基础 R 制作的。如果我把这张地图放进去,感觉它会脱颖而出,因为前景非常不同,这会使事情变得不一致。
    • 您始终可以将它们全部导出为 svg - 相同的质量水平,更好的质量水平。只是一个想法。在grDevices中,函数名是svg
    • 谢谢!我会试试这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 2015-03-21
    • 2021-05-30
    • 2013-02-16
    • 2012-06-10
    • 2021-10-31
    相关资源
    最近更新 更多