Q: Given a typical x,y scatter plot using ggplot(), can ggplot2 automagically plot the transformed y’s as well?
• In this ex, using stat_smooth(..)
feature in ggplot2 to have multiple lm-fits:
ggplot(df, aes(x=myX, y=myY)) +
geom_point(color=‘darkgray’) +
stat_smooth(method=‘lm’, se=F, aes(color=‘black’), formula=“y ~ x”) +
stat_smooth(method=‘lm’, se=F, aes(color=‘blue’), formula=“log(y) ~ x”) +
stat_smooth(method=‘lm’, se=F, aes(color=‘green’), formula=“sqrt(y) ~ x”) +
# log-scale it so transforms show up:
scale_y_continuous(trans=‘log10’)
• But I want to plot the scatters for the transformed y’s as well: sqrt(y)
andlog(y)
Does ggplot2 have such an automagic feature to also plot these into the same plot?
If not, what is the simplest recommended approach? Is it to compute manually then unstack
(base-R) or melt
(reshaper2) them into a long-format?
I would suggest next approach. You can create the transformations in new variables. Then, reshape data to long and then plot using one visualization for all variables or facets. Here the facet approach:
library(tidyverse)
#Code1
iris %>% mutate(x=Petal.Length,y=Sepal.Length,logy=log(Sepal.Length),sqrty=sqrt(Sepal.Length)) %>%
select(c(x,y,logy,sqrty)) %>%
pivot_longer(-x) %>%
ggplot(aes(x=x,y=value,color=name,group=name))+
geom_point()+
geom_smooth(method = lm,se=F)+
facet_wrap(.~name,scales = 'free_y')
Output:
Or the individual plot approach:
#Code2
iris %>% mutate(x=Petal.Length,y=Sepal.Length,logy=log(Sepal.Length),sqrty=sqrt(Sepal.Length)) %>%
select(c(x,y,logy,sqrty)) %>%
pivot_longer(-x) %>%
ggplot(aes(x=x,y=value,color=name,group=name))+
geom_point()+
geom_smooth(method = lm,se=F)
Output:
I have used iris
dataset and tidyverse
functions.