本文介绍了将exp / power趋势线添加到ggplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的情节添加一个指数(+ power)(趋势)线。我使用的是ggplot2软件包。



我有类似的东西(只是有更多的数据):



<$ p $


df< -read.table(test.csv,header = TRUE,sep =,)
df
元临时
1 1.283 6
2 0.642 6
3 1.962 6
4 8.989 25
5 8.721 25
6 12.175 25
7 11.676 32
8 12.131 32
9 11.576 32

ggplot(df,aes(temp,meta))+
ylab(Metabolism)+ xlab (Temperature)+
geom_point()+
theme_bw()+
scale_x_continuous(limits = c(0,35))+
scale_y_log10()

我知道这应该用一个指数函数来表示 - 所以我的问题是我如何才能做出最好的'指数'拟合?同样地,是否有可能进行动力匹配?



stat_smooth()函数是否有这个机会,或者在我应该使用的 ggplot2 包中有其他函数吗?

解决方案

您可以指定模型以适合 stat_smooth 通过传递两个参数:


  • 方法,例如 method =lm

  • 模型,例如 model = log(y)〜x



ggplot2 首先进行比例转换,然后适合模型,所以在你的例子中你只需添加

  + stat_smooth(method =lm)

到您的情节:

  library(ggplot2)
ggplot(df,aes(temp,meta))+
ylab(Metabolism)+ xlab (Temperature)+
geom_point()+
theme_bw()+
scale_x_continuous(limits = c(0,35))+
scale_y_log10()+
stat_smooth(method =lm)





同样,拟合和绘制一个功率曲线就像改变你的x-scale到log一样简单:

  ggplot(df,aes(temp,meta))+ 
ylab(Metabolism)+ xlab(Temperature)+
geom_point()+
theme_bw()+
scale_x_log 10()+
scale_y_log10()+
stat_smooth(method =lm)

I want to add a exponential (+ power) (trend) line to my plot. I am using ggplot2 package.

I have something like this (just with much more data):

require(ggplot2)

df <-read.table("test.csv", header = TRUE, sep = ",")
df
    meta temp
1  1.283    6
2  0.642    6
3  1.962    6
4  8.989   25
5  8.721   25
6 12.175   25
7 11.676   32
8 12.131   32
9 11.576   32

ggplot(df, aes(temp, meta)) + 
    ylab("Metabolism") + xlab("Temperature") +
    geom_point() + 
    theme_bw() + 
    scale_x_continuous(limits = c(0, 35)) + 
    scale_y_log10()

I know that this should be expressed with an exponential function - so my question is how I can ad the best 'exponential' fit? Likewise, is it possible to make a power-fit too?

Does the stat_smooth() function have this opportunity, or are there other functions in ggplot2 package I should use?

解决方案

You can specify the model to fit as an argument to stat_smooth by passing two arguments:

  • method, e.g. method="lm"
  • model, e.g. model = log(y) ~ x

ggplot2 first does the scale transformation and then fits the model, so in your example you simply have to add

+ stat_smooth(method="lm")

to your plot:

library(ggplot2)
ggplot(df, aes(temp, meta)) + 
    ylab("Metabolism") + xlab("Temperature") +
    geom_point() + 
    theme_bw() + 
    scale_x_continuous(limits = c(0, 35)) + 
    scale_y_log10() +
    stat_smooth(method="lm")

Similarly, fitting and plotting a power curve is as simple as changing your x-scale to log:

ggplot(df, aes(temp, meta)) + 
    ylab("Metabolism") + xlab("Temperature") +
    geom_point() + 
    theme_bw() + 
    scale_x_log10() + 
    scale_y_log10() +
    stat_smooth(method="lm")

这篇关于将exp / power趋势线添加到ggplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 04:15