用ggplot2绘制方程

时间:2016-05-18 21:38:09

标签: r ggplot2

我正在做两个变量之间的幂相关 问题是当我使用库(scale)时,将轴刻度更改为log(10),方程式不再显示,但是当我运行scritp whit out更改缩放轴时,方程出现。

这是我使用的脚本

library(ggplot2)
library(scales)


power_eqn = function(df, start = list(a =1,b=1)){
m = nls(QSolido ~ a*QLiquido^b, start = start, data = df);
eq <- substitute(italic(y) == a  ~italic(x)^b, 
               list(a = format(coef(m)[1], digits = 2), 
                    b = format(coef(m)[2], digits = 2)))
as.character(as.expression(eq))                 
}

RPotencial <- ggplot(Datos_Grafico,aes(x=QLiquido,y=QSolido)) +
          geom_point() + 
          stat_smooth(method = 'nls', formula = 'y~a*x^b', se=FALSE) +
          scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
                        labels = trans_format("log10", math_format(10^.x)))+
          scale_x_log10(breaks = trans_breaks("log10", function(x) 10^x),
                        labels = trans_format("log10", math_format(10^.x)))+
          geom_text(x = 100, y = 10000, label = power_eqn(Datos_Grafico), parse = TRUE)

有一些数据

Datos_Grafico <- data.frame (
QLiquido <- c(31.020, 33.590, 435.500, 269.600, 477.925, 425.800, 389.100, 164.375, 104.700,  80.000,  60.320, 51.110,  39.000,  23.210,  20.390,  19.080, 135.300, 146.950, 238.960, 270.850, 350.425, 165.900,128.800)
QSolido <- c(6.936072e+01, 5.488606e+00, 2.083510e+04, 5.724525e+03, 2.229348e+04, 6.335052e+04, 2.056611e+04, 1.547919e+03, 4.322016e+02, 4.059200e+03, 1.110129e+02, 5.318507e+01, 5.265780e+01, 1.756533e+01, 7.364868e+00, 1.197842e+01, 8.994473e+02, 2.907935e+03, 4.038185e+03, 4.963760e+03, 8.983706e+03, 1.398205e+03, 1.074450e+03)
)

由于

0 个答案:

没有答案