简体   繁体   中英

Variable name as argument in function - ggplot R

I want to make a plot that shows what is sold relative to a variable that tells me more about the company. I want a different plot for every kind of product( for example all sorts of fruits: apples, bananas). Next I want to make it easy to make these plots for other company variables as well.

I have working code for one variable but not for the function. It does not accept the argument like it is supposed to. I know it has something to do with non-standard evaluation, but I don't manage to fix the issue.

MASTERDATA%>%
  group_by(COMPANY)%>%
  summarize(AMOUNT_COMP = (sum(AMOUNT, na.rm=TRUE)),
            Type=Type,
            COMP_VAR1=COMP_VAR1)%>%
  filter(!is.na(Type)) %>%
  ggplot(aes(COMP_VAR1,AMOUNT_COMP ), na.rm = TRUE)+
  geom_point()+
  facet_wrap(~Type,nrow=4)

Following code for function

plot_var_aantal<-function(vari){
eval(substitute(vari), MASTERDATA)
MASTERDATA%>%
  group_by(COMPANY)%>%
  summarize(AMOUNT_COMP = (sum(AMOUNT, na.rm=TRUE)),
            Type=Type,
            vari=vari)%>%
  filter(!is.na(Type)) %>%
  ggplot(aes(vari,AMOUNT_COMP ), na.rm = TRUE)+
  geom_point()+
  facet_wrap(~Type,nrow=4)

}

If I For example put plot_var_aantal("people") I get just the word people on x-axis with all the points in a straght line above it.

DATA example

Productnr Type Amount COMPANY COMP_VAR1 COMP_VAR2
1 Apple 29 Company1 2 45
1 Pear 271 Company2 2 45
3 Apple 565 Company2 5 78
2 Banana 354 Company2 12 36
2 Pear 984 Company3 12 36
1 Banana 247 Company3 2 45
... ... ... ... ... ...

If you call plot_var_aantal with a string like "people" you need to evalute vari as a symbol on the righthand side with !!sym(vari) and on the lefthand side you need to put it into a glue specification "{vari}" . The following code should work:

plot_var_aantal<-function(vari){
  eval(substitute(vari), MASTERDATA)
  MASTERDATA%>%
    group_by(COMPANY)%>%
    summarize(AMOUNT_COMP = (sum(AMOUNT, na.rm=TRUE)),
              Type=Type,
              "{vari}" := !!sym(vari)) %>% # changed this line 
    filter(!is.na(Type)) %>%
    ggplot(aes(!! sym(vari),AMOUNT_COMP ), na.rm = TRUE) + # changed this line
    geom_point()+
    facet_wrap(~Type,nrow=4)
  
}

However, without seeing your data it is hard to figure out what COMP_VAR1 = COMP_VAR1 is doing in your dplyr::summarise call. You are not using an aggregating function (like mean or paste(..., collapse = ",") ) so the whole summarise is probably not summarising but returning data in the original length. Similarly the line "{vari}":= !!sym(vari) doesn't seem to make sense (although the non-standard evaluation when vari is a string, is correct).

When passing column names as string you can use .data pronoun.

library(dplyr)
library(ggplot2)

plot_var_aantal<-function(vari){
  MASTERDATA%>%
    group_by(COMPANY)%>%
    summarize(AMOUNT_COMP = (sum(AMOUNT, na.rm=TRUE)),
              Type=Type,
              !!vari := .data[[vari]])%>%
    filter(!is.na(Type)) %>%
    ggplot(aes(.data[[vari]],AMOUNT_COMP), na.rm = TRUE)+
    geom_point()+
    facet_wrap(~Type,nrow=4)
}

plot_var_aantal("people")

You can either use aes_string(vari, 'AMOUNT_COMP') to refer to the column names as string, or assign COMP_VAR1 = vari in the summarize statement and use your original plot code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM