I am making a potential relationship between two variables, I can create the graph and place the equation inside, but the problem occurs when I want to change the scale of the axes to logarithmic Log10, for this I use the package library(sales)
and when applying it the equation stops being displayed.
Here some data:
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)
)
This is the script I use:
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)
Thanks.
Inside you you
geom_text()
have ax
andy
outside ofaes
. If you try it in the following way it will work as expected:In the total of your example the code looks like this:
aes
contains the "aesthetic map" that describes how variables in the data are attributed to (aesthetic) components of the differentgeom_
ones you use in the figure definition. By not putting them insideaes()
ggplot2 it didn't recognize your coordinates for what they were.