在脚本和rmarkdown文件中运行时,ggplot图显示不同

时间:2016-09-29 08:54:30

标签: r ggplot2 knitr r-markdown

这是我的代码

Outlier_DF%>%
ggplot(. )+
        geom_bar(aes(x=(outlier_count), fill=as.character(outlier_count)))+
        facet_grid(Year~CVI_Quintile2)+
          ggtitle("Number of Outliers observed per Census Tract")+
          xlab("Risk Level")+ylab("Geography_Year observations")+
            scale_fill_discrete(name="Number of Outliers\n Per Census Tract")

这是来自Rmarkdown的错误生成的图表。 This is the incorrectly produced graph from the Rmarkdown.

这是Rscript正确生成的图形。 This is the correctly produced graph from the Rscript.

这是一瞥(Outlier_DF):

Observations: 474
Variables: 16
$ CVI_Quintile2         <fctr> Moderate Risk, High Risk, Very High Risk, Ve...
$ CVI                   <dbl> 32.0, 40.8, 48.0, 58.4, 67.3, 60.8, 31.0, 32....
$ Geography_Year        <fctr> Census Tract 6001_2012, Census Tract 6001_20...
$ Geography             <fctr> Census Tract 6001, Census Tract 6001, Census...
$ Year                  <int> 2012, 2013, 2014, 2012, 2013, 2014, 2012, 201...
$ PercEducHSorHigher    <dbl> 92.0, 90.6, 87.6, 68.4, 69.7, 76.7, 91.2, 90....
$ PercUnemployment      <dbl> 8.2, 8.2, 7.5, 10.1, 11.4, 5.6, 4.4, 5.8, 5.2...
$ PercSSinc             <dbl> 1.9, 3.0, 5.0, 8.8, 8.5, 8.6, 6.4, 7.4, 9.4, ...
$ PercNoHealthCov       <dbl> 10.9, 12.3, 12.8, 17.4, 20.0, 19.5, 8.7, 7.6,...
$ PercDisability        <dbl> 9.0, 9.2, 11.1, 9.5, 8.7, 6.9, 6.9, 6.7, 6.7,...
$ PercRentHigher        <dbl> 45.8, 58.4, 51.3, 59.2, 64.9, 60.9, 46.2, 35....
$ PercBelowPoverty      <dbl> 3.2, 4.7, 4.8, 17.3, 20.9, 17.5, 5.5, 2.6, 7....
$ outlier_count         <dbl> 0, 0, 0, 2, 3, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, ...
$ Clustered_Educ        <chr> NA, NA, NA, "Clustered", "Clustered", NA, NA,...
$ Clustered_Poverty     <chr> NA, NA, NA, "Clustered", "Clustered", "Cluste...
$ Clustered_NoHealthCov <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N...

据我所知,没有理由知道这会导致Rmarkdown中的不同/不正确的绘图与运行相同代码块的Rscript相比。我已经多次检查数据和代码以查看在运行Rmarkdown时它是否可能是一个排序问题,但这似乎不是问题,因为如果逐个运行降价,则生成的图形是正确的。我很高兴听到其他人遇到这个问题,或者对可能导致错误的原因提出建议,因为我无法理解这一点。

0 个答案:

没有答案