【问题标题】:R legend outside of graph not showing in pdf图外的R图例未在pdf中显示
【发布时间】:2018-05-09 17:06:02
【问题描述】:

下面的代码应该将下面的图表(带有图表外的图例)导出为 pdf。但是图例没有出现在生成的 pdf 中。但是,如果我只运行没有 pdf 行的代码,则图例会显示在 Rstudio 的绘图查看器中。

pdf(paste("testgraph.pdf", sep=''), paper="a4r", width=10, height=10)

set.seed(1) # just to get the same random numbers

plot(1:30, rnorm(30), pch = 1, lty = 1, type = "o", ylim=c(-2,2), bty='L')

legend("topright", inset=c(-0.3,0),c("group A", "group B"), pch = c(1,2), lty = c(1,2))

dev.off()

【问题讨论】:

    标签: r pdf plot graph legend


    【解决方案1】:

    这段代码对你有帮助吗?

    pdf(paste("testgraph.pdf", sep=''), paper="a4r", width=10, height=10)
    set.seed(1) # just to get the same random numbers
    plot(1:30, rnorm(30), pch = 1, lty = 1, type = "o", ylim=c(-2,2), bty='L')
    legend("topright",c("group A", "group B"), pch = c(1,2),lty = c(1,2))
    dev.off()
    

    【讨论】:

    • 我希望将图例放在图表之外,因为我计划在图表/图例中添加更多项目,并且不希望它掩盖我的图表。删除插入语句使图例出现在图表内
    【解决方案2】:

    legend() 中设置xpd = TRUE 并使用par(mar = c(c(bottom, left, top, right) + 0.1)) 获得更宽的边距。使用 x 和 y 坐标定位图例。

    pdf("testgraph.pdf", paper="a4r", width=10, height=10)
    
    par(mar = c(c(5, 4, 4, 6) + 0.1))
    set.seed(1) # just to get the same random numbers
    plot(1:30, rnorm(30), pch = 1, lty = 1, type = "o", ylim=c(-2,2), bty='L')
    
    legend(x = 31, y = 1, legend = c("group A", "group B"),
           pch = c(1,2), lty = c(1,2), xpd = TRUE)
    
    dev.off()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-25
      • 2021-10-13
      • 1970-01-01
      • 1970-01-01
      • 2016-07-23
      • 2019-04-07
      • 2014-01-07
      • 1970-01-01
      相关资源
      最近更新 更多