Reputation: 23
I am designing a function with ggplot2 and gridExtra to plot a data.frame below a plot. I have a trouble when I want to display the data.frame with white background.
library(ggplot2)
library(dplyr)
library(zoo)
library(gridExtra)
ej_actual<-ts(c(111584870,97818038,72432935,33477615,16289241,9635199,6272041,5847004,7985875,16389777,55992917,88918110,83788425,83840471,83992346,47811949,20300292,9341412,6880458,6879812,10561044,26444598,81640751,106310938,124994452,92134791,77768240,41761054,21910005,9375447,8523399,12976029,16349157,22153214,89375420,128070708,125035587,129972393,118743416,66455293,31471336,12022772),
start=c(2015,1),
frequency=12)
ej_forecast<-ts(c(111590870,105638538.5,67248898.9,40910921.01,19411541.49,11803143.49,6900191.943,7699304.861,8717574.837,11294409.56,54827821.67,90462182.16,96485019.64,78259892.64,86166354.09,45345505.32,23816633.26,12043131.27,7105590.958,6780659.903,8641346.192,17331924.41,91083976.51,133440607.9,116656483.3,90541374.96,82041830.87,46318475.58,20818673.83,11884937.27,8212540.442,8762332.625,15948119.95,26247824.83,84778535.04,128973918.7,106840644.2,132625869.5,101188496.3,51225515.59,28582808.19,12776399.32),
start=start(ej_actual),
frequency=frequency(ej_actual))
Data <- cbind(ej_actual, ej_forecast)
Data<-cbind.data.frame(as.data.frame(Data), Fecha=index(Data))
colnames(Data) <- c("actual","forecast","Fecha")
Data %>%
mutate(MAPE = abs_error/abs(actual),
WMAPE = (abs_error/media_actual),
PWMAPE = (abs_error/max(media_actual,actual)),
SMAPE=(abs_error/((abs(forecast)+abs(actual))/2)))->df_MAPEs
#Calculamos los MAPES medios
metricas<-sapply(df_MAPEs[,4:7],FUN=function(x){round(mean(x)*100,2)})
df_metricas<-as.data.frame(t(metricas))
selected_MAPE <- (switch(selection, "MAPE" = 4, "WMAPE" = 5, "PWMAPE" = 6, "SMAPE" = 7))
Nombre <- as.character(colnames(df_MAPEs)[selected_MAPE])
#Ploteamos la comparación
color_names <- c("red","darkgreen","white")
names(color_names) <- c("Actual","Forecast",Nombre)
names(color_names)
#Diseñamos el theme para pintar la tabla
tt <- ttheme_default(colhead=list(fg_params = list(parse=TRUE)),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank())
if(isTRUE(plot)){
graph_MAPE<-ggplot(data=df_MAPEs)+
geom_bar(aes(x=Fecha,y=250000000*df_MAPEs[,selected_MAPE],fill=names(color_names)[3]),color="darkblue", stat="identity")+
geom_line(aes(x=Fecha,y=actual, color="Actual"),stat="identity", size=1)+
geom_line(aes(x=Fecha,y=forecast,color="Forecast"), size=1)+
scale_y_continuous(sec.axis = sec_axis(~./250000000, name= Nombre))+
labs(title=paste("Forecast y",Nombre), x="Fecha", y="Valor serie")+
scale_color_manual(name="Lines", values=color_names)+
scale_fill_manual(name="Bar",values=color_names)
tabl_r<-tableGrob(df_metricas, rows=NULL, theme=tt)
graph_t<-grid.arrange(graph_MAPE, tabl_r,
nrow = 2,heights = c(2, 0.5), as.table=TRUE)
plot(graph_t)
Even I put element_blank()
on theme specifications, I obtained the following plot:
Does anybody know how to turn table's background to white?
Upvotes: 1
Views: 602
Reputation: 78917
For ggplot add this line of code:
p + theme_void()
For plot() function in base R use this code:
par(bg = 'white')
Upvotes: 1