R图: size and resolution

标签 r png plot

我已经陷入了这个问题:我需要使用 DPI=1200 和特定的打印尺寸来绘制图像。

默认情况下 png 看起来不错... enter image description here

png("test.png",width=3.25,height=3.25,units="in",res=1200)
par(mar=c(5,5,2,2),xaxs = "i",yaxs = "i",cex.axis=1.3,cex.lab=1.4)
plot(perf,avg="vertical",spread.estimate="stddev",col="black",lty=3, lwd=3)
dev.off()

但是当我应用此代码时,图像变得非常糟糕,它没有缩放(适合)到所需的尺寸。我错过了什么?如何使图像“适合”情节?

enter image description here ,

最佳答案

可重现的示例:

the_plot <- function()
{
  x <- seq(0, 1, length.out = 100)
  y <- pbeta(x, 1, 10)
  plot(
    x,
    y,
    xlab = "False Positive Rate",
    ylab = "Average true positive rate",
    type = "l"
  )
}

James 建议使用 pointsize 并结合各种 cex 参数,可以产生合理的结果。

png(
  "test.png",
  width     = 3.25,
  height    = 3.25,
  units     = "in",
  res       = 1200,
  pointsize = 4
)
par(
  mar      = c(5, 5, 2, 2),
  xaxs     = "i",
  yaxs     = "i",
  cex.axis = 2,
  cex.lab  = 2
)
the_plot()
dev.off()
<小时/>

当然,更好的解决方案是放弃这种对基本图形的摆弄,并使用可以为您处理分辨率缩放的系统。例如,

library(ggplot2)

ggplot_alternative <- function()
{
  the_data <- data.frame(
    x <- seq(0, 1, length.out = 100),
    y = pbeta(x, 1, 10)
  )

ggplot(the_data, aes(x, y)) +
    geom_line() +
    xlab("False Positive Rate") +
    ylab("Average true positive rate") +
    coord_cartesian(0:1, 0:1)
}

ggsave(
  "ggtest.png",
  ggplot_alternative(),
  width = 3.25,
  height = 3.25,
  dpi = 1200
)

关于R图: size and resolution,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8399100/

相关文章:

html - iPad/ios6 不渲染 png 背景图片

iphone - UITableView 背景图像 Alpha 问题

c++ - 如果没有 libRcpp.so 文件,我如何链接 Rcpp?

r - 取消列表 R

ImageMagick,用不透明白色替换半透明白色

r - R中水平图的颜色

r - 在图中显示 "for all"(∀) 符号

r - 在单个图中绘制多个表

R sp : unit of area of Polygon

java - 在终端中使用 R 时避免在 ctrl+c 上退出 rJava 相关系统