I have problems in writing down some LaTeX expressions in the graph in Rstudio, i already did it using
axis(..., label=TeX("x_{min}")
The problem arises when I try to put the command \mathcal{M}
in that label. Any way to have the M that LaTeX produces with that command in an R plot using axis
or mtext
? I'd rather use mtext
but using it, it doesn't even produce the x_{min}
i produced using axis like i wrote before.
Thank you in advance for any help
EDIT: as requested i put here an example of my code
library(latex2exp)
b=7/5
fx <- function (x,b) x^(4*(b-1)/(b+1)) *(1/(b-1)+1/x)
fmin <- optimize(fx, interval=c(0,2), b=7/5)
x <- seq(10^(-4),20, by = 0.01)
y <- fx(x,b)
plot(x, y, log="xy", ann="FALSE", pch=20, type="l",lwd=2, xlim=c(10^(-3),10), ylim=c(1.5,40),
xaxt="n", yaxt="n")
mtext("x", side=1, line=2, cex=1, col="black")
mtext("f(x)", side=2, line=2, cex=1, col="black")
#assex
axis(1,at=fmin[1], label=TeX("x_{min}"))
axis(1,at=0.001, label=0.001)
And other axis lines, I think this code should run. it produces something like the image below, where the blu lines are obviously part of the code i didn't paste here
As requested I post the solution i found. First of all you can find a complete documentation here: https://cran.r-project.org/web/packages/tikzDevice/vignettes/tikzDevice.pdf
With this package your whole graph get the LaTeX math formatting style. So you get better numbers and even letters which are now in mathstyle (if you use the $$ obviously). You can also add some letters through some commands you wouldn't be able to add without it. I needed for example to type this LaTeX command: \mathcal{M}
This is the solution i found, using the the package tikzDevice. This package allows you to create a file we'll call "plot.tex" out of your R plot. You won't see the plot immediately (or at least i still haven't learned it). Once the package creates this plot.tex you have to include it in another of yours .tex files. The file where you want it to appear. This is the way:
\begin{figure}
\input{plot.tex}
\end{figure}
obviously putting
\usepackage{tikz}
in the preamble.
The code in Rstudio is more or less like this, referring to the code in my question:
#Defintion of f
b=7/5
fx <- function (x,b) x^(4*(b-1)/(b+1)) *(1/(b-1)+1/x)
#find minimum of f (oviously optional)
fmin <- optimize(fx, interval=c(0,2), b=7/5)
#Density of points in the x-asxis
x <- seq(10^(-4),20, by = 0.01)
#Assign value to y
y <- fx(x,b)
#call the tikzDevice package, don't forget the dev.off() at the end or you won't be able to produce a proper file
library(tikzDevice)
#name the file you want to create and decide its dimensions
tikz('faggy.tex',
width=5.5,height=4)
#do your normal plot as usual: you can add any LaTeX symbol as you can see below
#plot_f(x)_______________
plot(x, y, log="xy", ann="FALSE", pch=20, type="l",lwd=2, xlim=c(10^(-3),10),
ylim=c(1.5,40), xaxt="n", yaxt="n")
#code useful to plot the lines that touch of the minimum of the function
linea1 <- seq(1,fmin[[2]], by = 0.01)
xminimo <- numeric(length(linea1))
xminimo <- rep(fmin[1],length(linea1))
linea2 <- seq(10^(-4),fmin[[1]], by = 0.01)
fminimo <- numeric(length(linea2))
fminimo <- rep(fmin[2],length(linea2))
#plot_lines___
points(xminimo,linea1, pch=20, type="l", lwd=2, lty=2, col="blue")
points(linea2,fminimo, pch=20, type="l", lwd=2, lty=2, col="blue")
#writing on the axis: you can use LaTeX here!!! any symbol or command, like \mathcal{}
mtext("$x$", side=1, line=2, cex=1, col="black")
mtext("$f(x)$", side=2, line=2, cex=1, col="black", las=2)
#assex, example of the use \mathcal, I didn't need it there oviously :)
axis(1,at=fmin[1], label=paste("$\\mathcal{M}_{min}$"))
axis(1,at=0.001, label=0.001)
axis(1,at=0.01, label=0.01)
axis(1,at=1, label=1)
axis(1,at=10, label=10)
#assey
axis(2,at=fmin[2], label=paste("$f_{min}$"),las = 2)
axis(2,at=2, label=2,las = 2)
axis(2,at=5, label=5,las = 2)
axis(2,at=10, label=10,las = 2)
axis(2,at=20, label=20,las = 2)
axis(2,at=40, label=40,las = 2)
dev.off()