stefano-meschiari / latex2exp

Use LaTeX in R graphics.
Other
186 stars 10 forks source link

Line break in TeX()? #8

Closed caijun closed 6 years ago

caijun commented 6 years ago

I would like to add a line break in the plot title using the LaTeX command \newline, however it didn't produce the figure as expectation.

library(plyr)
library(ggplot2)
x <- seq(0, 4, length.out=100)
alpha <- 1:5
data <- mdply(alpha, function(a, x) data.frame(v=a*x^a, x=x), x)

p <- ggplot(data, aes(x=x, y=v, color=X1)) +
  geom_line() + 
  ylab(TeX('$\\alpha  x^\\alpha$, where $\\alpha \\in 1\\ldots 5$')) +
  ggtitle(TeX('Using $\\LaTeX$ for plotting in ggplot2. $\\newline$ I $\\heartsuit$ ggplot!')) +
  coord_cartesian(ylim=c(-1, 10)) +
  guides(color=guide_legend(title=NULL)) +
  scale_color_discrete(labels=lapply(sprintf('$\\alpha = %d$', alpha), TeX)) 

print(p)

image

stefano-meschiari commented 6 years ago

Hi caijun, I can add support for that. In the meantime, you should be able to simply use \n to force a newline.

caijun commented 6 years ago

I tried \n, but it didn't work.

library(plyr)
library(ggplot2)
x <- seq(0, 4, length.out=100)
alpha <- 1:5
data <- mdply(alpha, function(a, x) data.frame(v=a*x^a, x=x), x)

p <- ggplot(data, aes(x=x, y=v, color=X1)) +
  geom_line() + 
  ylab(TeX('$\\alpha  x^\\alpha$, where $\\alpha \\in 1\\ldots 5$')) +
  ggtitle(TeX('Using $\\LaTeX$ for plotting in ggplot2. \n I $\\heartsuit$ ggplot!')) +
  coord_cartesian(ylim=c(-1, 10)) +
  guides(color=guide_legend(title=NULL)) +
  scale_color_discrete(labels=lapply(sprintf('$\\alpha = %d$', alpha), TeX)) 

print(p)

image