GGPLOT:打印堆积条形图&行到文件

时间:2016-10-15 23:40:24

标签: r ggplot2

我知道从这个问题看起来可能不是这样,但我实际上已经编程了20多年,但我是R的新手。我试图离开Excel并自动创建我目前在Excel中手动执行的约100个图表。我之前提出过两个问题:herehere。这些解决方案适用于那些玩具示例,但是当我在我自己的完整程序中尝试完全相同的代码时,它们的表现非常不同,我完全迷惑为什么。当我运行下面的程序时,testplot.png文件只是该行的图,没有堆积条形图。

所以这是我的(完整)代码,因为我可以做到这一点。如果有人想批评我的编程,请继续。我知道评论很轻松,但是为了这篇文章,我试图缩短它。此外,这确实下载了USDA PSD数据库,该数据库压缩了大约20MB,未压缩为170MB ...对不起,但我会某人的帮助!

编辑,这里是' full'的str()输出。数据和玩具'数据。玩具数据有效,完整的数据没有。

> str(melteddata)
Classes ‘data.table’ and 'data.frame':  18 obs. of  3 variables:
 $ Year    : int  1 2 3 4 5 6 1 2 3 4 ...
 $ variable: Factor w/ 3 levels "stocks","exports",..: 1 1 1 1 1 1 2 2 2 2 ...
 $ Qty     : num  2 4 3 2 4 3 4 8 6 4 ...
 - attr(*, ".internal.selfref")=<externalptr> 
> str(SoySUHist)
Classes ‘data.table’ and 'data.frame':  159 obs. of  3 variables:
 $ Year    : int  1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 ...
 $ variable: Factor w/ 3 levels "Stocks","DomCons",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ Qty     : num  0.0297 0.0356 0.0901 0.1663 0.3268 ...
 - attr(*, ".internal.selfref")=<externalptr> 
> str(linedata)
Classes ‘data.table’ and 'data.frame':  6 obs. of  2 variables:
 $ Year: int  1 2 3 4 5 6
 $ Qty : num  15 16 15 16 15 16
 - attr(*, ".internal.selfref")=<externalptr> 
> str(SoyProd)
Classes ‘data.table’ and 'data.frame':  53 obs. of  2 variables:
 $ Year: int  1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 ...
 $ Qty : num  701 846 928 976 1107 ...
 - attr(*, ".internal.selfref")=<externalptr> 
> 





library(data.table)
library(ggplot2)
library(ggthemes)
library(plyr)

toyplot <- function(plotdata,linedata){
  plotCExp <- ggplot(plotdata) +
    geom_bar(aes(x=Year,y=Qty,factor=variable,fill=variable), stat="identity") +
    geom_line(data=linedata, aes(x=Year,y=Qty)) # <---- comment out this line & the stack plot works

  ggsave(plotCExp,filename = "ggsavetest.png", width=7, height=5, units="in")

}

convertto <- function(value,crop,unit='BU'){
  if (unit=='BU' & ( crop=='WHEAT' | crop=='SOYBEANS')){
    value = value * 36.7437
  } 
  return(value)
}

# =====================================
# Download Data (Warning...large download!) 
# =====================================
system("curl https://apps.fas.usda.gov/psdonline/download/psd_alldata_csv.zip | funzip > DATA/psd.csv")
tmp <- fread("DATA/psd.csv")
PSD = data.table(tmp)
rm(tmp)
setkey(PSD,Country_Code,Commodity_Code,Attribute_ID)
tmp=unique(PSD[,.(Commodity_Description,Attribute_Description,Commodity_Code,Attribute_ID)])
tmp[order(Commodity_Description)]
names(PSD)[names(PSD) == "Market_Year"] = "Year"
names(PSD)[names(PSD) == "Value"] = "Qty"

PSDCmdtyAtt = unique(PSD[,.(Commodity_Code,Attribute_ID)])

# Soybean Production, Consumpion, Stocks/Use
SoyStocks = PSD[list("US",2222000,176),.(Year,Qty)]  # Ending Stocks
SoyExp    = PSD[list("US",2222000,88),.(Year,Qty)]  # Exports
SoyProd   = PSD[list("US",2222000,28),.(Year,Qty)]   # Total Production
SoyDmCons = PSD[list("US",2222000,125),.(Year,Qty)]  # Total Dom Consumption

SoyStocks$Qty = convertto(SoyStocks$Qty,"SOYBEANS","BU")/1000
SoyExp$Qty    = convertto(SoyExp$Qty,"SOYBEANS","BU")/1000
SoyProd$Qty   = convertto(SoyProd$Qty,"SOYBEANS","BU")/1000
SoyDmCons$Qty = convertto(SoyDmCons$Qty,"SOYBEANS","BU")/1000

# Stocks/Use
SoySUPlot <- SoyExp
names(SoySUPlot)[names(SoySUPlot) == "Qty"] = "Exports"
SoySUPlot$DomCons    = SoyDmCons$Qty
SoySUPlot$Stocks     = SoyStocks$Qty
SoySUHist <- melt(SoySUPlot,id.vars="Year")
SoySUHist$Qty = SoySUHist$value/1000
SoySUHist$value <- NULL
SoySUPlot$StocksUse = 100*SoySUPlot$Stocks/(SoySUPlot$DomCons+SoySUPlot$Exports)

SoySUPlot$Production = SoyProd$Qty/1000
SoySUHist$variable <- factor(SoySUHist$variable, levels = rev(levels(SoySUHist$variable)))
SoySUHist = arrange(SoySUHist,variable)

toyplot(SoySUHist,SoyProd)

1 个答案:

答案 0 :(得分:2)

好吧,我感觉很慷慨。您的示例代码包含大量的绒毛,不应该在一个可重复性最小的示例中,并且您的system调用不可移植,但无论如何我看了一下。

好消息:您的代码按预期工作。

我们只绘制条形图:

ggplot(SoySUHist) +
  geom_bar(aes(x=Year,y=Qty,factor=variable,fill=variable), stat="identity")

plot 1

现在只有行:

ggplot(SoySUHist) +
  geom_line(data=SoyProd, aes(x=Year,y=Qty))

plot 2

现在比较y轴的比例。如果您将两者一起绘制,则会绘制条形图,但它们非常小,您无法看到它们。你需要重新缩放:

ggplot(SoySUHist) +
  geom_bar(aes(x=Year,y=Qty,factor=variable,fill=variable), stat="identity") +
  geom_line(data=SoyProd, aes(x=Year,y=Qty/1000)) 

plot 3