簡體   English   中英

如何在 function 中將列名作為 arguments 傳遞

[英]How to pass column name as an arguments in the function

我正在嘗試將重復的 ggplot 代碼轉換為 function 我已經完成了幾乎但唯一的問題是我需要將應用程序作為變量傳遞

這是 plot 代碼,我在匯總和變異中使用了名為 Application 的列。 挑戰是同一列已用於 y 軸

    ggplot_common_function <- function(data,x,y,z){
  Removestring(ggplotly(
   data %>%
      group_by(m_year,status) %>%
      summarise(Applications = sum(Applications)) %>%
      mutate(total_sum = sum(Applications))%>% ggplot(mapping = aes({{x}},{{y}},text = paste(total_sum))) +
      geom_col(aes(fill = {{z}}))+theme_classic()+
      theme(axis.line.y = element_blank(),axis.ticks = element_blank(),legend.position = "bottom")+
      labs(x="", y="Agreements Values (In Lakhs)", fill="")+
      theme(axis.title.y =element_text(size=8))+
      scale_fill_manual(values=c("#1F7A3F","#70B821"))+
      scale_y_continuous(labels = function(x) format(x, scientific = FALSE),expand = expansion(mult = c(0,.3)),breaks = integer_breaks()),tooltip = c("text"))%>% 
      layout(legend = list(orientation = "h", x = 0.1, 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_common_function(數據,m_year,應用程序,狀態)

要運行上面的代碼,有一些 pre function

integer_breaks <- function(n = 5, ...) {
  fxn <- function(x) {
    breaks <- floor(pretty(x, n, ...))
    names(breaks) <- attr(breaks, "labels")
    breaks
  }
  return(fxn)
}

Removestring = function(d){
  for (i in 1:length(d$x$data)){
    if (!is.null(d$x$data[[i]]$name)){
      d$x$data[[i]]$name =  gsub("\\(","",str_split(d$x$data[[i]]$name,",")[[1]][1])
    }
  }
  return(d)
}

Plan <- "#288D55"
multicolor = c("#135391","#0098DB","#828388","#231F20","#C41330","#698714","#162FBF","#F36717","#BD00FF")
x_labels = list(tickangle = -45,tickfont = list(family = "Arial",size = 10,color = "black",face="bold"))
y_labels = list(tickfont = list(family = "Arial",size = 10,color = "black",face="bold"))




structure(list(Month = structure(c(5L, 5L, 5L, 5L, 7L, 7L, 6L, 
    6L, 6L, 6L), .Label = c("Apr", "May", "Jun", "Jul", "Aug", "Sep", 
    "Oct", "Nov", "Dec", "Jan", "Feb", "Mar"), class = "factor"), 
        m_year = structure(c(1L, 1L, 1L, 1L, 3L, 3L, 2L, 2L, 2L, 
        2L), .Label = c("Aug-2021", "Sep-2021", "Oct-2021"), class = "factor"), 
        Applications = c(182, 121, 115, 187, 0, 0, 18, 15, 15, 28
        ), status = c("Open", "Open", "Open", "Open", "Open", "Open", 
        "Open", "Open", "Open", "Open")), row.names = c(NA, -10L), class = "data.frame")

任何建議,將不勝感激

這基本上是與dplyr中的編程有關的問題。 為了達到您想要的結果並擺脫對 function 中的列名進行硬編碼並使用 x、y、z 代替,您可以像在 ggplot 代碼中一樣使用{{ curly-curly 運算符和特殊賦值運算符:= . 此外,您無需將所有代碼包裝在 ggplotly 中,而是按步驟進行。 進行數據整理,制作您的 ggplot 並最終將其傳遞給 ggplotly:

library(plotly)
library(dplyr)
library(stringr)

ggplot_common_function <- function(data, x, y, z) {
  data <- data %>%
    group_by({{ x }}, {{ z }}) %>%
    summarise({{ y }} := sum({{ y }})) %>%
    mutate(total_sum = sum({{ y }}))

  p <- ggplot(data, mapping = aes({{ x }}, {{ y }}, text = paste(total_sum))) +
    geom_col(aes(fill = {{ z }})) +
    theme_classic() +
    theme(axis.line.y = element_blank(), axis.ticks = element_blank(), legend.position = "bottom") +
    labs(x = "", y = "Agreements Values (In Lakhs)", fill = "") +
    theme(axis.title.y = element_text(size = 8)) +
    scale_fill_manual(values = c("#1F7A3F", "#70B821")) +
    scale_y_continuous(labels = function(x) format(x, scientific = FALSE), expand = expansion(mult = c(0, .3)), breaks = integer_breaks())

  ggp <- ggplotly(p, tooltip = c("text")) %>%
    layout(legend = list(orientation = "h", x = 0.1, 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"))

  Removestring(ggp)
}

ggplot_common_function(data, m_year, Applications, status)
#> `summarise()` has grouped output by 'm_year'. You can override using the
#> `.groups` argument.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM