本文介绍了ggplot只将灰盒打印到文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 所以我们试图让我的图表起作用,但现在我无法打印它!最终目标是我需要自动更新这些图表,因此ggplot和print调用需要在函数中。 $ b toyfn< - 函数(plotdata){$ b $当我运行此代码时,每个文件只包含一个灰色方块。 b库(ggplot2) plotS1 plotS1 + geom_bar(aes(x = year,y = value,factor = variable,fill = variable, order = - as.numeric(variable)),stat =identity)+ geom_line(data = linedata,aes(x = year,y = production)) ggsave('testprint.png',plotS1) png(文件名='testprint2.png') print(plotS1) dev.off()} library( ggplot2) library(reshape) #首先让我们为我们的堆积图/线图例子制作一个玩具数据集。 年= c(1,2,3,4,5,6)股票= c(2,4,3,2,4,3)出口=股票* 2 国内=库存* 3 生产= c(15,16,15,16,15,16) #制作2个df's:alldata用于堆积条形图,linedata是在它上面绘制一条线。 alldata = data.frame(year,stocks,exports,domestic) linedata = data.frame(year,production) #使所有数据'长'为堆叠 melteddata = melt(alldata,id.vars =year) toyfn(melteddata) 解决方案您正在保存一个没有 geoms 的情节。使用geoms的情节会显示在屏幕上,但不会显示在文件中。 试试这个: geom_bar(绘图数据,aes(年,价值,因子=变量, (stat =identity,aes(order = -as.numeric(variable)))+ geom_line(data = linedata,aes(x = year,y = production)) ggsave('testprint.png',plot = plotS1)} So SO nailed getting my graph to work, but now i can't get it to print! The end goal is that i need to automate the updating of these plots, so the ggplot and print calls need to be in a function. When i run this code, each file just contains a gray square.toyfn <- function(plotdata){ library(ggplot2) plotS1 <- ggplot(plotdata) plotS1 + geom_bar(aes(x=year,y=value,factor=variable,fill=variable, order=-as.numeric(variable)), stat="identity") + geom_line(data=linedata, aes(x=year,y=production)) ggsave('testprint.png',plotS1) png(filename='testprint2.png') print(plotS1) dev.off()}library(ggplot2)library(reshape)# First let's make a toy dataset for our stacked plot/line plot example.year = c(1,2,3,4,5,6)stocks = c(2,4,3,2,4,3)exports = stocks*2domestic = stocks*3production = c(15,16,15,16,15,16)# Make 2 df's: alldata is for stacked bar chart, linedata is for plotting a line on top of it.alldata = data.frame(year,stocks,exports,domestic)linedata = data.frame(year,production)# Make alldata 'long' for the stackingmelteddata = melt(alldata,id.vars="year")toyfn(melteddata) 解决方案 You are saving a plot with no geoms. The plot with geoms will display on the screen, but not in the file.Try this:toyfn <- function(plotdata){ plotS1 <- ggplot(plotdata, aes(year, value, factor = variable, fill = variable)) + geom_bar(stat="identity", aes(order = -as.numeric(variable))) + geom_line(data=linedata, aes(x=year,y=production)) ggsave('testprint.png', plot = plotS1)} 这篇关于ggplot只将灰盒打印到文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-31 14:46