R: ggplot2 used in a function that does not reflect changes in variable font size

I often need several different sizes of the same output of the ggplot2 diagram for png files. The size of each png file is simple enough to generate using variables for the height and width of the output in pixels. For the ggplot2 part, I use variables for font size and some other elements and set up a simple loop that changes these variables in each pass. This all works as expected and is a tribute to the flexibility of R and ggplot2.

In most cases, I create one of a small number of chart types, most of which do not change. Accordingly, I thought it would make sense to create a simple function that monitors the code template and returns graphs from ggplot2 in the list. All I have to do is pass the data frame to the function, the name of the column that I want to use in the chart, and a couple of other variables. The loop creates a name for the chart, calls ggplot, and assigns the result to the item in the list. On the second pass, it changes the font size variables, but otherwise behaves the same.

However, font sizes do not seem to be collected by ggplot. In particular, I use variables to control the size of geom_text (), the text size of the x and y axis and the title of the graph. When I print the contents of a list after it returns from a function, the size of geom_text () changes, as expected, but the other two elements do not change when they should be different. (Note that in the code below I use two β€œmedium” and β€œlarge” PNG files with the same pixel size, but usually they will be twice as many as the others β€” this is for illustrative purposes only.)

Image 1

And the second image, which should have different header and axis sizes for the first, but not:

Image 2

Since this approach works great when using in-line as part of a normal piece of code, I can only assume that there is some direct problem with the way I call, or possibly update the function that causes the problem. Any help is greatly appreciated.

I have not used named functions in R before, and I'm more likely a programmer than a professional, so I apologize in advance for the ingenious code below.

# create test data set.seed(12345) mydf <- data.frame(passdate=seq(as.Date("1995/1/1"), by="month", length.out=204),passval=runif(204, min=25, max=100),ignoreval=runif(204, min=-21, max=-2)) myplots <- list() myplots <- chart_high_mom(mydf,'passdate','passval','1995-02-01','2011-12-31',"My title here") # first chart mywidth = 700 myheight = 600 png(filename = "chart1.png", width = 700, height = 600, units = "px", res = NA) print(myplots[[1]]) dev.off() # second chart - this intended to be twice as large when bugs fixed png(filename = "chart2.png", width = 700, height = 600, units = "px", res = NA) print(myplots[[2]]) dev.off() # end of calling code chart_high_mom <- function( x = NULL, # a data frame containing the data to plot datecol, # name of column holding yyyy-mm-dd date series valcol, # name of column holding value to use for calculation fstartdate, # date in yyyy-mm-dd format specifying first date to plot fenddate, # date in yyyy-mm-dd format specifying last date to plot ftitle # title to add to plot ) { require(gdata) require(xts) require(ggplot2) require(lubridate) # strip the data frame down x <- subset(x,select = c(datecol,valcol)) colnames(x) <- c('mydate','myval') # create year and month columns x$year <- as.numeric(format(as.Date(x$mydate), format="%Y")) x$month <- as.numeric(format(as.Date(x$mydate), format="%m")) # create month-on-month change column mydata.xts <- xts(x$myval,order.by=x$mydate) x$myvalmom <- diff(mydata.xts,lag=1)/lag(mydata.xts,1) plotlist <- list() for (i in 1:2) { # loop to create plot with two different sets of font sizes plotname <- paste("myplot", i, sep = "") if (i == 1) { fontsize <- 8 titlesize <- 16 geomtextsize <- 4 ftitle <- "medium" } if (i == 2) { fontsize <- 24 titlesize <- 28 geomtextsize <- 5 ftitle <- "large" } print(paste("i = ",i," and fontsize = ",fontsize," and plot = ",plotname,sept="")) plotlist[[plotname]] <- ggplot(x[x$mydate>=fstartdate & x$mydate<=fenddate,], aes(x=month(mydate,label=TRUE),y=year(mydate), fill = myvalmom, label = sprintf("%1.1f%%", 100*myvalmom))) + scale_y_date(major="years", format="%Y") + geom_tile() + geom_text(data=x[x$mydate>=fstartdate & x$mydate<=fenddate,],size = geomtextsize,colour = "black") + scale_fill_gradient2(low = "blue", high = "red",midpoint=0) + scale_x_discrete(expand=c(0,0)) + scale_y_reverse(breaks=1980:2012, labels=1980:2012, expand=c(0,0) ) + opts( axis.text.y = theme_text(size = fontsize, colour = "grey50"), axis.text.x = theme_text(size = fontsize, colour = "grey50"), plot.title = theme_text(size = titlesize), title = ftitle, panel.grid.minor = theme_blank(), axis.ticks = theme_blank(), panel.grid.major = theme_blank(), axis.title.y = theme_blank(), axis.title.x = theme_blank(), panel.background = theme_rect(fill = "transparent",colour = NA), legend.position = "none" ) } return(plotlist) } 
+6
source share
1 answer

First, define your chart functions (so that they can accept the parameters that you will change:

 chart_high_mom <- function(fontsize, titlesize, geomtextsize, ftitle, x = NULL, # a data frame containing the data to plot datecol, # name of column holding yyyy-mm-dd date series valcol, # name of column holding value to use for calculation fstartdate, # date in yyyy-mm-dd format specifying first date to plot fenddate # date in yyyy-mm-dd format specifying last date to plot ) { # strip the data frame down x <- subset(x,select = c(datecol,valcol)) colnames(x) <- c('mydate','myval') # create year and month columns x$year <- as.numeric(format(as.Date(x$mydate), format="%Y")) x$month <- as.numeric(format(as.Date(x$mydate), format="%m")) # create month-on-month change column mydata.xts <- xts(x$myval,order.by=x$mydate) x$myvalmom <- diff(mydata.xts,lag=1)/lag(mydata.xts,1) plotlist <- list() print(paste("i = ",i," and fontsize = ",fontsize," and titlesize = ",titlesize,sep="")) thisplot <- ggplot(x[x$mydate>=fstartdate & x$mydate<=fenddate,], aes(x=month(mydate,label=TRUE),y=year(mydate), fill = myvalmom, label = sprintf("%1.1f%%", 100*myvalmom))) + scale_y_date(major="years", format="%Y") + geom_tile() + geom_text(data=x[x$mydate>=fstartdate & x$mydate<=fenddate,],size = geomtextsize, colour = "black") + scale_fill_gradient2(low = "blue", high = "red",midpoint=0) + scale_x_discrete(expand=c(0,0)) + scale_y_reverse(breaks=1980:2012, labels=1980:2012, expand=c(0,0) ) + force(opts(axis.text.y = theme_text(size = force(fontsize), colour = "grey50"), axis.text.x = theme_text(size = force(fontsize), colour = "grey50"), plot.title = theme_text(size = titlesize), title = ftitle, panel.grid.minor = theme_blank(), axis.ticks = theme_blank(), panel.grid.major = theme_blank(), axis.title.y = theme_blank(), axis.title.x = theme_blank(), panel.background = theme_rect(fill = "transparent",colour = NA), legend.position = "none" )) return(thisplot) } 

.... then call them. And call them with the parameter values ​​you want:

 set.seed(12345) mydf <- data.frame(passdate=seq(as.Date("1995/1/1"), by="month", length.out=204),passval=runif(204, min=25, max=100),ignoreval=runif(204, min=-21, max=-2)) myplots <- list() for (i in 1:2) { # loop to create plot with two different sets of font sizes if (i == 1) { print(fontsize <- 8) print(titlesize <- 32) print(geomtextsize <- 4) print(ftitle <- "medium"); myplots[[1]] <-chart_high_mom(fontsize= fontsize , titlesize= titlesize , geomtextsize= geomtextsize , ftitle= ftitle , x=mydf, 'passdate', 'passval', '1995-02-01', '2011-12-31') png(filename = "chart1.png", width = 700, height = 600, units = "px", res = NA) print(myplots[[1]]) dev.off() } if (i == 2) { fontsize <- 24 titlesize <- 12 geomtextsize <- 5 ftitle <- "large"; myplots[[2]] <-chart_high_mom(fontsize= fontsize , titlesize= titlesize , geomtextsize= geomtextsize , ftitle= ftitle , x=mydf, 'passdate', 'passval', '1995-02-01', '2011-12-31') png(filename = "chart2.png", width = 700, height = 600, units = "px", res = NA) print(myplots[[2]]) dev.off() } } # end of calling code 
+4
source

Source: https://habr.com/ru/post/907807/


All Articles