Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot2&x2014;有没有办法在同一散点图上自动绘制不同的变换y?_R_Ggplot2_Linear Regression_Lm - Fatal编程技术网

R ggplot2&x2014;有没有办法在同一散点图上自动绘制不同的变换y?

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

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

  • ggplot2是否具有这样的自动控制功能,可以将这些图形也打印到同一个图形中?

  • 如果不是,最简单的推荐方法是什么?然后是手动计算
    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
    ,所以这很好。顺致敬意,