Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the expression for partial derivative in ggplot()?

Tags:

r

ggplot2

Is it possible to get the partial derivative symbol via expression() in ggplot2, e.g. to be used in axis labels?

I am talking about this symbol, often also refered to as 'del' or 'curly d': https://en.wikipedia.org/wiki/%E2%88%82

It has unicode number U+2202, but when I try to include it in ggplot, it fails:

a <- b <- rnorm(100)
plot.df <- data.frame(a,b)

ggplot(plot.df,aes(a,b)) + 
  geom_point() + 
  xlab(expression('\u2202'))

del

For comparison, using e.g. the plus/minus sign with unicode number U+00B1 works fine:

ggplot(plot.df,aes(a,b)) + 
  geom_point() + 
  xlab(expression('\u00b1'))

plusminus

like image 878
yrx1702 Avatar asked Jan 25 '23 04:01

yrx1702


2 Answers

With the ggtext package you can use HTML entities:

library(ggplot2)
library(ggtext)

a <- b <- rnorm(100)
plot.df <- data.frame(a,b)

ggplot(plot.df, aes(a,b)) + 
  geom_point() + 
  xlab("&part;") + 
  theme(axis.title.x = element_markdown(size = 20))

enter image description here

like image 21
Stéphane Laurent Avatar answered Feb 04 '23 13:02

Stéphane Laurent


you can achieve this using the keyword partialdiff. using your example:

ggplot(plot.df,aes(a,b)) + 
  geom_point() + 
  xlab(expression(paste(partialdiff,"y","/",partialdiff,"x")))

here

This link provides some good reference on the matter.

Depending how far you want to go. You can eventually use TikzDevice library to save the plot directly as a tex.file. It might take longer to compile the graph but I find it more flexible.

library(tikzDevice)
tikz("/tmp/test.tex",standAlone = TRUE)
 ggplot(plot.df,aes(a,b)) + 
     geom_point() + 
     xlab("$\\frac{\\partial{y}}{\\partial{x}}$")
dev.off()
like image 107
DJJ Avatar answered Feb 04 '23 13:02

DJJ