ggplot2 文件输出问题

ggplot2 file output issues

晚上。今天我开始关注 ggplot2 并设法创建了一堆图。 但是 我遇到了两个障碍。第一个是自动创建绘图时输出的外观。 谁能帮我弄清楚?

当我 运行 手动使用绘图时...

ggplot(l.Exploration$Data,aes_string(x="domain",y="WP0", color="domain")) +
    geom_point(position=position_jitter(width=0.3), alpha=0.4) +
    geom_boxplot(size=1,alpha=0.9, outlier.size=1, outlier.shape=21, width=0.75, notch=TRUE) +
    facet_wrap(~Exchange, ncol=2) +
    ggtitle(plotTitle) +
    theme(plot.title=element_text(size=rel(1.5), lineheight=.9, face="bold", colour="black")) +
    xlab("Exchange") + 
    theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
    ylab("Weighted Price ($USD)") 

因此我得到了一个整洁的情节......

但是当我将情节创建为循环的一部分并让它们自动保存时,它们看起来很糟糕(是的,我知道 aes_string(...,Y="WP0") 应该使用 i但我也想不通)。

第二个问题是如何为Y正确指定aes_string。(我会把这个移到另一个问题)

 l_ply(-3:3, function(i){
  print(i)
  path     <- "~/Documents/1. Dev/r/data/plot"
  filename <- paste(path,"/Story_Price",i,".png",sep="")
  yCol  <- paste("l.Exploration$Data$WP",i,sep="")

  if(i < 0)         { plotTitle <- paste("Story Publication Against Price\n[Lead = ",i,"]",sep="") 
  } else if (i==0)  { plotTitle <- paste("Story Publication Against Price",sep="") 
  } else if (i>0)   { plotTitle <- paste("Story Publication Against Price\n[Lag = ",i,"]",sep="") 
  }

  ggplot(l.Exploration$Data,aes_string(x="domain",y="WP0", color="domain")) +
    geom_point(position=position_jitter(width=0.3), alpha=0.4) +
    geom_boxplot(size=1,alpha=0.9, outlier.size=1, outlier.shape=21, width=0.75, notch=TRUE) +
    facet_wrap(~Exchange, ncol=2) +
    ggtitle(plotTitle) +
    theme(plot.title=element_text(size=rel(1.5), lineheight=.9, face="bold", colour="black")) +
    xlab("Exchange") + 
    theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
    ylab("Weighted Price ($USD)") 

    dev.print(png, filename,res=600, height=1600, width=2500, units="px")
})

让我们来解决尺寸问题,它解决了您提出的两个不同问题之一。

问题: dev.print 的参数为您提供了一个大约 4 x 3 的图形。图形无法容纳在 space 中,因此它们被截断了。

ggplot2 有自己的保存命令,ggsave,这让您更容易控制尺寸。

解法: 尝试将 dev.print 替换为:

ggsave(file = "filename.png", dpi = 600, width = 8, height = 6, units = "in")

dpi越大,显示的图形就越大。将此与 ggsave 中的其他论点一起作为 ggplot2 情节的一部分进行测试,您会发现一个合适的保存方式。