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 |
| ... | ... | ... | ... | ... | ... |
dput(head(MASTERDATA)).vari = variby{{ vari }} := varisolves your problem.variinside theaes()should also be curly-curlied.Type=Typeandvari=variinsummarise? What are they supposed to do?group_byor to keep same number of rows usemutateinstead ofsummarise.