R ggplot2&x2014;有没有办法在同一散点图上自动绘制不同的变换y?
Q:给定一个使用ggplot()的典型x,y散点图,ggplot2是否也能自动绘制转换后的y? •在本例中,使用ggplot2中的R ggplot2&x2014;有没有办法在同一散点图上自动绘制不同的变换y?,r,ggplot2,linear-regression,lm,R,Ggplot2,Linear Regression,Lm,Q:给定一个使用ggplot()的典型x,y散点图,ggplot2是否也能自动绘制转换后的y? •在本例中,使用ggplot2中的stat_smooth(..)功能进行多个lm拟合: 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(metho
stat_smooth(..)
功能进行多个lm拟合:
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’)
•但我也想绘制变换后y的散射:sqrt(y)
和log(y)
unstack
(base-R)还是将它们融化成长格式
我建议采取下一个办法。您可以在新变量中创建转换。然后,将数据重塑为long,然后对所有变量或面使用一种可视化绘制。这里是分面方法:
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')
#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)
输出:
或单独绘图法:
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')
#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)
输出:
我已经使用了
iris
dataset和tidyverse
函数。感谢您非常有用的回复。实际上,我也一直想学习facet
,所以这很好。顺致敬意,