I want to pass column names in a custom function that uses ggplot so I can recreate the graph.
The error states:
Error: All columns in a tibble must be 1d or 2d objects: * Column `x`
How can I update my function so I can define columns I want?
Thanks.
#DATA AND GRAPH
data("USArrests")
USArrests$IsHigh <- ifelse(USArrests[1] >= 13, 1 ,0)
ggplot(USArrests, aes(x=Assault, fill=factor(IsHigh)))+geom_density(alpha=0.25)+
geom_vline(aes(xintercept=mean(Assault[IsHigh==0],na.rm=T)),color="red",linetype="dashed",lwd=1)+
geom_vline(aes(xintercept=mean(Assault[IsHigh==1],na.rm=T)),color="blue",linetype="dashed",lwd=1)+
scale_x_continuous()+
theme_classic()
##ATTEMPT AT FUNCITON
Test <- function(DATA, col1, col2){
ggplot(DATA, aes(x=col1, fill=factor(col2)))+
geom_density(alpha=0.25)+
geom_vline(aes(xintercept=mean(col1[col2==0],na.rm=T)),color="red",linetype="dashed",lwd=1)+
geom_vline(aes(xintercept=mean(col1[col2==1],na.rm=T)),color="blue",linetype="dashed",lwd=1)+
scale_x_continuous()+
theme_classic()
}
#ERROR
Test(USArrests, "Assault", "IsHigh")
aes_stringggplot2.tidyverse.org/reference/aes_.html