将 ggplot 转化为函数

Turning ggplot into function

我正在尝试将一些重复的 ggplot 代码转换为函数。这样我就可以在需要时调用函数,或者是否可以在函数中编写整个 ggplot 代码

我把代码放在重复的函数里

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"))
}

并在 ggplot 中调用该函数,但出现错误(无法将 ggproto 对象添加在一起。您是否忘记将此对象添加到 ggplot 对象?)

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'))

这是实际的整个 ggplot 代码

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'))




                          

您只能使用 +ggplot 创建的对象添加内容。如果您不想在函数中调用 ggplot,则不能使用 +。相反,您应该让函数 return 变成 list,就像这个例子中的那样:

library(ggplot2)
foo <- function(text) list(geom_point(), 
                           labs(x = "", y = text))
ggplot(iris, aes( x = Species, y = Sepal.Length)) + foo("bar")