首页 \ 问答 \ Stat_function范围被截断(Stat_function range truncated)

Stat_function范围被截断(Stat_function range truncated)

我试图使用ggplot2绘制一些数据并拟合非线性曲线。 我想将stat_function与我已定义的nls对象一起使用,但结果会产生截断的曲线。 我查看了以下页面,但到目前为止我还没有找到解决方案:

R - ggplot2在线性区域外推回归线

使用stat_function()在R中的ggplot中绘制大量自定义函数

http://docs.ggplot2.org/0.9.3/stat_function.html

ggplot的等效曲线()

我对ggplot2不是很有经验,所以如果我遗漏了一些简单的东西,我很抱歉,我很感激任何帮助。 这是我的示例数据:

df=structure(list(Spp = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Dugentia", "Eugenia", 
"Faramea", "Licania", "Mouriri"), class = "factor"), Tx = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Control", 
"Drought"), class = "factor"), no. = structure(c(1L, 3L, 4L, 
5L, 6L, 1L, 3L, 4L, 5L, 6L, 1L, 3L, 4L, 5L, 6L, 1L, 3L, 4L, 5L, 
6L, 1L, 3L, 4L, 5L, 6L, 1L, 3L, 4L, 5L, 6L, 1L, 3L, 4L, 5L, 6L, 
5L, 6L, 7L, 8L, 5L, 6L, 7L, 8L, 5L, 6L, 7L, 8L, 5L, 6L, 7L, 8L, 
5L, 6L, 7L, 8L, 5L, 6L, 7L, 8L, 5L, 6L, 7L, 8L), .Label = c("1", 
"101", "2", "3", "4", "5", "6", "7", "7A", "9"), class = "factor"), 
Fv.Fm = c(0.74, 0.702, 0.797, 0.782, 0.769, 0.759, 0.701, 
0.805, 0.79, 0.775, 0.763, 0.725, 0.8, 0.786, 0.774, 0.759, 
0.664, 0.791, 0.776, 0.758, 0.729, 0.592, 0.757, 0.722, 0.681, 
0.66, 0.084, 0.652, 0.633, 0.63, 0.569, 0.259, 0.424, 0.376, 
0.432, 0.771, 0.696, 0.685, 0.761, 0.782, 0.772, 0.736, 0.775, 
0.784, 0.755, 0.707, 0.746, 0.777, 0.765, 0.705, 0.744, 0.706, 
0.55, 0.582, 0.635, 0.615, 0.384, 0.504, 0.513, 0.584, 0.378, 
0.328, 0.302), Temp. = c(27L, 27L, 27L, 27L, 27L, 30L, 30L, 
30L, 30L, 30L, 35L, 35L, 35L, 35L, 35L, 40L, 40L, 40L, 40L, 
40L, 45L, 45L, 45L, 45L, 45L, 48L, 48L, 48L, 48L, 48L, 50L, 
50L, 50L, 50L, 50L, 27L, 27L, 27L, 27L, 30L, 30L, 30L, 30L, 
35L, 35L, 35L, 35L, 40L, 40L, 40L, 40L, 45L, 45L, 45L, 45L, 
48L, 48L, 48L, 48L, 50L, 50L, 50L, 50L)), .Names = c("Spp", 
"Tx", "no.", "Fv.Fm", "Temp."), class = "data.frame", row.names = c(NA, 
63L))

这是我到目前为止用ggplot2绘制的情节:

library(ggplot2)
f1 = ggplot(data = df, aes(x = Temp., y = Fv.Fm, group = Tx) )
f2<-f1+
  geom_point(aes(shape=Tx, fill=Tx), size=4)
f3 <- f2 +  scale_x_continuous("Temperature (°C)", limits=c(25,55)) +
  scale_y_continuous("Fv/Fm", limits = c(0, 1)) +
  scale_shape_manual(values=c(24,21)) +
  scale_fill_manual(values=c("#4D4D4D","#E6E6E6")) +
  theme_bw()
d4 <- f3 + theme(panel.grid.major = element_blank(), 
                 panel.grid.minor = element_blank(),
                 axis.text.x = element_text(size = 11),
                 axis.text.y = element_text(size = 11),
                 legend.title = element_blank(),
                 legend.text = element_text(size=12))

所有这一切都运行良好,但是当我创建一个nls对象并尝试将其与stat_function一起使用时,我得到一条被截断的曲线。 我想将曲线向下延伸到x轴。 还会生成错误。

my.nls<-nls(Fv.Fm~a*(-exp(Temp.)+b), data = df, start=list(a=1*10^-20, b=5*10^21))
new<-d4+stat_function(fun=function(x){coef(my.nls)[1]*(-exp(x)+coef(my.nls)[2])})
new

我想要的是更像下面使用基本的r功能的东西,但是ggplot2提供了所有的铃声和​​口哨声,并且最终每个治疗(Tx)都有一个nls曲线。

x<-seq(27, 55, length.out = 200)
y <- predict(my.nls,list(Temp. = x))
plot(df$Temp., df$Fv.Fm)
lines(x,y)

I am trying to use ggplot2 to plot some data and fitted non-linear curves. I want to use stat_function with nls objects that I have already defined, but the result produces a truncated curve. I have looked at the following pages but so far I haven't found a solution:

R - ggplot2 extrapolated regression lines in linear region

Plotting a large number of custom functions in ggplot in R using stat_function()

http://docs.ggplot2.org/0.9.3/stat_function.html

Equivalent of curve() for ggplot

I am not very experienced with ggplot2, so I apologize if I am missing something simple and I'm grateful for any help. Here my example data:

df=structure(list(Spp = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Dugentia", "Eugenia", 
"Faramea", "Licania", "Mouriri"), class = "factor"), Tx = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Control", 
"Drought"), class = "factor"), no. = structure(c(1L, 3L, 4L, 
5L, 6L, 1L, 3L, 4L, 5L, 6L, 1L, 3L, 4L, 5L, 6L, 1L, 3L, 4L, 5L, 
6L, 1L, 3L, 4L, 5L, 6L, 1L, 3L, 4L, 5L, 6L, 1L, 3L, 4L, 5L, 6L, 
5L, 6L, 7L, 8L, 5L, 6L, 7L, 8L, 5L, 6L, 7L, 8L, 5L, 6L, 7L, 8L, 
5L, 6L, 7L, 8L, 5L, 6L, 7L, 8L, 5L, 6L, 7L, 8L), .Label = c("1", 
"101", "2", "3", "4", "5", "6", "7", "7A", "9"), class = "factor"), 
Fv.Fm = c(0.74, 0.702, 0.797, 0.782, 0.769, 0.759, 0.701, 
0.805, 0.79, 0.775, 0.763, 0.725, 0.8, 0.786, 0.774, 0.759, 
0.664, 0.791, 0.776, 0.758, 0.729, 0.592, 0.757, 0.722, 0.681, 
0.66, 0.084, 0.652, 0.633, 0.63, 0.569, 0.259, 0.424, 0.376, 
0.432, 0.771, 0.696, 0.685, 0.761, 0.782, 0.772, 0.736, 0.775, 
0.784, 0.755, 0.707, 0.746, 0.777, 0.765, 0.705, 0.744, 0.706, 
0.55, 0.582, 0.635, 0.615, 0.384, 0.504, 0.513, 0.584, 0.378, 
0.328, 0.302), Temp. = c(27L, 27L, 27L, 27L, 27L, 30L, 30L, 
30L, 30L, 30L, 35L, 35L, 35L, 35L, 35L, 40L, 40L, 40L, 40L, 
40L, 45L, 45L, 45L, 45L, 45L, 48L, 48L, 48L, 48L, 48L, 50L, 
50L, 50L, 50L, 50L, 27L, 27L, 27L, 27L, 30L, 30L, 30L, 30L, 
35L, 35L, 35L, 35L, 40L, 40L, 40L, 40L, 45L, 45L, 45L, 45L, 
48L, 48L, 48L, 48L, 50L, 50L, 50L, 50L)), .Names = c("Spp", 
"Tx", "no.", "Fv.Fm", "Temp."), class = "data.frame", row.names = c(NA, 
63L))

Here is what I have so far for making the plot with ggplot2:

library(ggplot2)
f1 = ggplot(data = df, aes(x = Temp., y = Fv.Fm, group = Tx) )
f2<-f1+
  geom_point(aes(shape=Tx, fill=Tx), size=4)
f3 <- f2 +  scale_x_continuous("Temperature (°C)", limits=c(25,55)) +
  scale_y_continuous("Fv/Fm", limits = c(0, 1)) +
  scale_shape_manual(values=c(24,21)) +
  scale_fill_manual(values=c("#4D4D4D","#E6E6E6")) +
  theme_bw()
d4 <- f3 + theme(panel.grid.major = element_blank(), 
                 panel.grid.minor = element_blank(),
                 axis.text.x = element_text(size = 11),
                 axis.text.y = element_text(size = 11),
                 legend.title = element_blank(),
                 legend.text = element_text(size=12))

All of that works well, but then when I make an nls object and try to use it with stat_function I get a curve that is truncated. I would like to extend the curve down to the x-axis. There is also an error generated.

my.nls<-nls(Fv.Fm~a*(-exp(Temp.)+b), data = df, start=list(a=1*10^-20, b=5*10^21))
new<-d4+stat_function(fun=function(x){coef(my.nls)[1]*(-exp(x)+coef(my.nls)[2])})
new

What I want is something more like the following using basic r functionality, but with all the bells and whistles offered by ggplot2, and ultimately one nls curve for each treatment (Tx).

x<-seq(27, 55, length.out = 200)
y <- predict(my.nls,list(Temp. = x))
plot(df$Temp., df$Fv.Fm)
lines(x,y)

原文:https://stackoverflow.com/questions/21716775
更新时间:2023-09-22 17:09

最满意答案

以下基于SFINAE的方法似乎产生了预期的结果。 作为额外的奖励, BaseTempl模板可以采用可变参数,而不仅仅是一个参数:

#include <iostream>

template<typename ...Args> class BaseTempl {};

template<typename T> class inherits_from_basetempl {

public:

    template<typename ...Args>
    static const bool sfinae_param(const BaseTempl<Args...> &a);

    template<typename V=T>
    static constexpr auto is_inherits(int)
        -> decltype(sfinae_param(std::declval<V &>()))
    {
        return true;
    }

    static constexpr bool is_inherits(...)
    {
        return false;
    }

    static const bool value=is_inherits(0);
};

class alpha : public BaseTempl< int >{};

class bravo : BaseTempl< int >{};

class charlie{};

class delta : public BaseTempl< int >, public charlie {};

class echo : public delta {};

int main()
{
    std::cout << inherits_from_basetempl<alpha>::value << std::endl;
    std::cout << inherits_from_basetempl<bravo>::value << std::endl;
    std::cout << inherits_from_basetempl<charlie>::value << std::endl;
    std::cout << inherits_from_basetempl<delta>::value << std::endl;
    std::cout << inherits_from_basetempl<echo>::value << std::endl;
}

结果:

$ gcc --version
gcc (GCC) 5.3.1 20151207 (Red Hat 5.3.1-2)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++ -std=c++14 -o t t.C 2>&1 | less
$ ./t
1
0
0
1
1

The following SFINAE-based approach appears to produce the expected result. As an extra bonus, the BaseTempl template can take variadic parameters, and not just one parameter:

#include <iostream>

template<typename ...Args> class BaseTempl {};

template<typename T> class inherits_from_basetempl {

public:

    template<typename ...Args>
    static const bool sfinae_param(const BaseTempl<Args...> &a);

    template<typename V=T>
    static constexpr auto is_inherits(int)
        -> decltype(sfinae_param(std::declval<V &>()))
    {
        return true;
    }

    static constexpr bool is_inherits(...)
    {
        return false;
    }

    static const bool value=is_inherits(0);
};

class alpha : public BaseTempl< int >{};

class bravo : BaseTempl< int >{};

class charlie{};

class delta : public BaseTempl< int >, public charlie {};

class echo : public delta {};

int main()
{
    std::cout << inherits_from_basetempl<alpha>::value << std::endl;
    std::cout << inherits_from_basetempl<bravo>::value << std::endl;
    std::cout << inherits_from_basetempl<charlie>::value << std::endl;
    std::cout << inherits_from_basetempl<delta>::value << std::endl;
    std::cout << inherits_from_basetempl<echo>::value << std::endl;
}

Results:

$ gcc --version
gcc (GCC) 5.3.1 20151207 (Red Hat 5.3.1-2)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++ -std=c++14 -o t t.C 2>&1 | less
$ ./t
1
0
0
1
1

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)