0

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
... ... ... ... ... ...
7
  • Please share a small example of your data using dput(head(MASTERDATA)). Commented Aug 23, 2021 at 11:14
  • 2
    Not sure, what you are trying to do. Perhaps replacing vari = vari by {{ vari }} := vari solves your problem. vari inside the aes() should also be curly-curlied. Commented Aug 23, 2021 at 11:25
  • 2
    Why do you have Type=Type and vari=vari in summarise ? What are they supposed to do? Commented Aug 23, 2021 at 12:23
  • @RonakShah I use them there, cause I found that otherwise I don't have these variables anymore to work with in later lines. For example plot or filter. Commented Aug 23, 2021 at 12:30
  • 1
    If those variables are the same throughout maybe you need to include them in group_by or to keep same number of rows use mutate instead of summarise. Commented Aug 23, 2021 at 12:32

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.