I am trying to convert some ggplot code which is repetitive into function .So that i can call the function whenever it needs or is it possible to write the whole ggplot code inside the function
i put the code inside the function which is repetitve
ggplot_common_function = function(text){
geom_bar(stat='identity',position=position_dodge(width=20),width=10)+theme_classic()+
theme(axis.line.y = element_blank(),axis.ticks = element_blank(),legend.position = "bottom",
axis.text.x = element_text(face = "bold", color = "black", size = 10, angle = 45, hjust = 1))+
labs(x="", y=text, fill="")+
theme(axis.title.y =element_text(size=8))+
scale_x_date(date_breaks ="1 month", date_labels ="%b-%Y")+
scale_y_continuous(labels = function(x) format(x, scientific = FALSE),expand = expansion(mult = c(0,.3)),breaks = integer_breaks())+
scale_fill_manual(values=c("#dfe022", "#288D55"))
}
And calling the function inside the ggplot but i am getting an error (Cannot add ggproto objects together. Did you forget to add this object to a ggplot object?)
ggplotly(ggplot(aggregate(revenue~date+type,data=revenue_data(),FUN=sum),aes(x=date,y=revenue,fill=type,text=paste(revenue)))+
ggplot_common_function("INR (In Lakhs)"),expand = expansion(mult = c(0,.3)),tooltip = c("text"))%>%layout(legend = list(orientation = "h", x = 0.25, y = -0.2,font=list( family='Arial', size=10, color='black')),xaxis = x_labels,yaxis = y_labels)%>%config(displaylogo = FALSE,modeBarButtonsToRemove = list('sendDataToCloud', 'autoScale2d', 'resetScale2d', 'toggleSpikelines','hoverClosestCartesian', 'hoverCompareCartesian','zoom2d','pan2d','select2d','lasso2d','zoomIn2d','zoomOut2d'))
This is the actual whole ggplot code
ggplotly(ggplot(aggregate(revenue~date+type,data=revenue_data(),FUN=sum),aes(x=date,y=revenue,fill=type,text=paste(revenue)))+
geom_bar(stat='identity',position=position_dodge(width=0.5),width=0.3)+theme_classic()+
theme(axis.line.y = element_blank(),axis.ticks = element_blank(),legend.position = "bottom",
axis.text.x = element_text(face = "bold", color = "black", size = 10, angle = 45, hjust = 1))+
labs(x="", y="INR (In Lakhs)", fill="")+
theme(axis.title.y =element_text(size=8))+
scale_y_continuous(labels = function(x) format(x, scientific = FALSE),expand = expansion(mult = c(0,.3)),breaks = integer_breaks())+
scale_fill_manual(values=c("#dfe022", "#288D55")),expand = expansion(mult = c(0,.3)),tooltip = c("text"))%>%layout(legend = list(orientation = "h", x = 0.25, y = -0.2,font=list( family='Arial', size=10, color='black')),xaxis = x_labels,yaxis = y_labels)%>%config(displaylogo = FALSE,modeBarButtonsToRemove = list('sendDataToCloud', 'autoScale2d', 'resetScale2d', 'toggleSpikelines','hoverClosestCartesian', 'hoverCompareCartesian','zoom2d','pan2d','select2d','lasso2d','zoomIn2d','zoomOut2d'))
>Solution :
You can only use + to add something to the object created by ggplot. If you don’t want to have the ggplot call within your function, you cannot use +. Instead you should let the function return a list like in this example:
library(ggplot2)
foo <- function(text) list(geom_point(),
labs(x = "", y = text))
ggplot(iris, aes( x = Species, y = Sepal.Length)) + foo("bar")