R ggplot2尝试制作重复叠加面积图;获取不一致的错误
作为一个更大脚本的一部分,我必须制作70个堆叠面积图,每年1个,然后将它们写入pdf。我只是偶尔有问题。我把它设置成一个循环,有时在前四年中断,有时完成一半,有时几乎结束 当我跨过循环,不管它在哪一年中断(通过取消注释ThisYear和window行),我都可以让图形在窗口中显示得很好。有时候。有时我会两次绘制同一年的图表,第一次有效,第二次无效。有时反之亦然 发生错误时,可能是“错误:结果必须全部是原子的,或所有数据帧”,或“错误:参数意味着不同的行数:14641458”,或“错误:不能在CHARSXP上有属性”,但我无法可靠地使相同的错误再次发生。它们发生在打印(p)时,但我不知道“p”是什么让它崩溃的 我使用的数据(下面冒充为“BlueData”、“GreenData”、“YellowData”和“SalmonData”)都是双倍的,我已经检查并重新检查过了。没有NAs。事实上,当我伪造数据在这里发布这篇文章时,我以为我有什么东西,因为它没有崩溃。然后我运行了几次,它也开始以同样不一致的方式抛出同样的错误 编辑:我可能缺少一些“require”语句。。。我很快就会修好的R ggplot2尝试制作重复叠加面积图;获取不一致的错误,r,ggplot2,R,Ggplot2,作为一个更大脚本的一部分,我必须制作70个堆叠面积图,每年1个,然后将它们写入pdf。我只是偶尔有问题。我把它设置成一个循环,有时在前四年中断,有时完成一半,有时几乎结束 当我跨过循环,不管它在哪一年中断(通过取消注释ThisYear和window行),我都可以让图形在窗口中显示得很好。有时候。有时我会两次绘制同一年的图表,第一次有效,第二次无效。有时反之亦然 发生错误时,可能是“错误:结果必须全部是原子的,或所有数据帧”,或“错误:参数意味着不同的行数:14641458”,或“错误:不能在CH
require(lubridate)
TestYear = 1929
BlueLimit = 110
### creating fake data to mimic what my data is like
BlueData <- rnorm(70*365+14, mean = 100, sd = 25)
BlueData <- ifelse(BlueData > BlueLimit, BlueLimit, BlueData)
YellowData <- rnorm(70*365+14, mean = 15, sd = 2)
SalmonData <- rnorm(70*365+14, mean = 20, sd = 3)
GreenData <- rnorm(70*365+14, mean = 5, sd = 1)
BigData.df <- as.data.frame(cbind(BlueData,YellowData,SalmonData,GreenData))
BigData.df$Date <- seq(from = ISOdate(TestYear - 1, 10, 1), length.out = nrow(BigData.df), by = "day")
BigData.df$MainData <- BigData.df$BlueData + BigData.df$GreenData + BigData.df$YellowData + BigData.df$SalmonData
BigData.df$LimitData <- BlueLimit
### long complicated reasons, but no February 29ths allowed in the dataset...
BigData.df <- subset(BigData.df, (month(BigData.df$Date) == 2)*(day(BigData.df$Date) == 29) == 0)
### okay, that should do it for mocking up data...
pdfFilename <- "C:\\TestFolder\\TestPDF.pdf"
plot_colors_aggregate = c("gold","forestgreen","lightsalmon","dodgerblue")
### establish appropriate scale for top of graph and gridlines
max_y <- max(BigData.df$MainData)
max_y <- ceiling(max_y/50)*50
if (max_y <= 200) {
gridline_multiplier = 20
} else {
gridline_multiplier = 50
}
pdf(file = pdfFilename, height=8, width = 11, onefile = TRUE)
for (ThisYear in 1929:1998){
#GraphYear = TestYear
#windows(height = 8, width = 12)
cat("\t\tNow working on",ThisYear)
page_title = paste("This Graph is for",ThisYear)
##### CREATE SUBSET OF BIGDATA.DF WITH JUST THIS GRAPH YEAR
GraphData.df <- subset(BigData.df, year(BigData.df$Date) == ThisYear)
##### CREATE SMALLER DF WITH JUST DATA TO BE GRAPHED
df4plot <- data.frame(
AllColors = c(GraphData.df$BlueData,
GraphData.df$SalmonData,
GraphData.df$GreenData,
GraphData.df$YellowData),
date = rep(GraphData.df$Date, 4),
DataType = rep(c("Blue Stuff", "Salmon Stuff", "Green Stuff", "Yellow Stuff"), each = 365)
)
df4LimitLine <- data.frame(
BlueLimit = GraphData.df$LimitData,
date = GraphData.df$Date
)
df4plot$DataType <- factor(df4plot$DataType,
levels = c("Yellow Stuff", "Green Stuff","Salmon Stuff","Blue Stuff"))
#PeriodBreaks <- GraphData.df$Date[FirstOfTheMonth.v]
scale_color_manual(values=plot_colors_aggregate)
p <- ggplot(data = df4plot, aes(date, AllColors)) +
scale_fill_manual(values = plot_colors_aggregate) +
scale_color_manual(values = plot_colors_aggregate)+
scale_y_continuous(limits=c(0,max_y), breaks=seq(0,max_y,gridline_multiplier*2))
p <- p +
geom_area(aes(colour = DataType, fill = DataType), position="stack") +
theme(axis.text.x = element_text(angle = -45, hjust = 0, vjust=1)) +
theme(legend.position=c(.7, .7), legend.title=element_blank(), legend.justification = "left", legend.background = element_rect(fill="white")) +
theme(panel.background = element_rect(), panel.grid.major = element_line(colour = "darkgrey", linetype = "dotted"), panel.grid.minor = element_line(colour="lightgrey", linetype = "dotted")) +
theme(plot.margin = unit(c(1,1,5,1),"line"))
p <- p + geom_line(data=df4LimitLine, aes(x=date,y=BlueLimit, linetype = "Limit of Blue Data") ,colour="darkblue") +
theme(legend.key = element_rect(fill="white", linetype = 0)) +
scale_linetype_manual(name = "Linetype", values = "dashed") +
annotate("text", x=df4LimitLine$date[floor(nrow(df4LimitLine)/2)], y=max_y, label=page_title)
print(p)
grid.text("Blah blah blah", x=unit(.95,"npc"), y=unit(.1,"npc"), hjust=1, gp=gpar(cex = .8))
grid.text("Yadda yadda yadda", x=unit(.95,"npc"),y=unit(.08,"npc"), hjust = 1, gp=gpar(cex = .8))
cat(ThisYear,"now finished.\n")
}
dev.off()
require(润滑油)
测试年=1929年
蓝极限=110
###创建虚假数据以模拟我的数据
BlueDataI解决了不一致的错误和错误:结果必须全部是原子的,或者通过恢复到r3.1.1,所有的数据帧。3.2.0似乎不稳定。关于不同行数的消息可能是由您创建df4plot
的方式造成的-您可能会在一年中没有365天数据的任何时候收到此错误。考虑使用代码从<代码> RESHAPE2或<代码> TyDyr < /代码>对数据集进行整形,以避免此问题。我无法复制任何其他错误消息。