Program Club

R 플롯 : 크기 및 해상도

proclub 2020. 12. 27. 11:31
반응형

R 플롯 : 크기 및 해상도


나는 질문에 쌓여있다 : DPI = 1200 및 특정 인쇄 크기로 이미지를 플로팅해야합니다.

기본적으로 png는 괜찮아 보입니다 ... 여기에 이미지 설명 입력

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

하지만이 코드를 적용했을 때 이미지가 정말 끔찍 해져서 필요한 크기로 확장 (맞추기)되지 않았습니다. 내가 놓친 게 무엇입니까? 이미지를 플롯에 "맞추는"방법은 무엇입니까?

여기에 이미지 설명 입력,


재현 가능한 예 :

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"
  )
}

pointsize다양한 cex매개 변수 와 함께 를 사용하는 James의 제안은 합리적인 결과를 생성 할 수 있습니다.

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
)

당신이 기본 그래픽을 사용하려는 경우, 당신은보고있을 수 있습니다 . 추출물 :

인치당 픽셀 수를 지정하는 png에 대한 res = 인수를 사용하여이를 수정할 수 있습니다. 이 숫자가 작을수록 플롯 영역 (인치)이 커지고 그래프 자체에 비해 텍스트가 작아집니다.

참조 URL : https://stackoverflow.com/questions/8399100/r-plot-size-and-resolution

반응형