Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

downwards arrow in R-plot axis label

I have a Variable named \LW$_\downarrow$ in Latex. As you can imagine, i would like to use the same phrase as axis label and add the Unit which is [W/m²] or [W m^-2]

I managed to do this:

library(ggplot2)
data <- data.frame(x=c(1:5), y=c(2:6))
ggplot(data = data, aes(x=x, y=y))+
  geom_point()+
  labs(x=expression(LW%down%. ))

but

ggplot(data = data, aes(x=x, y=y))+
  geom_point()+
  labs(x=expression(LW%down% )) #no point behind the second %

gives "Error:

unexpected ')' in: " geom_point()+ labs(x=expression(LW%down% )"

and

ggplot(data = data, aes(x=x, y=y))+
  geom_point()+
  labs(x=paste(expression(LW%down%. ), "[W/m²]"))

gives this: with %down% as word

enter image description here

unfortunately not very helpful.

I hope someone can help. I have a lot of plots so I hope someone so manual editing with inkscape does not really sound like a good option.


tyumru gave me a good hint to the right direction with

ggplot(data = data, aes(x=x, y=y))+
  geom_point()+
  labs(x=expression("LW"%down%"[W/m²]"))

Whe I try to put the arrow in subscript with

ggplot(data = data, aes(x=x, y=y))+
  geom_point()+
  labs(x=expression("LW"[%down%]"[W/m²]"))

Unfortunately I get

Error: unexpected SPECIAL in: " geom_point()+ labs(x=expression("LW"[%down%"

like image 816
Birte Avatar asked Apr 11 '26 20:04

Birte


2 Answers

I guess this is what you are looking for:

ggplot(data = data, aes(x=x, y=y))+
  geom_point()+
  labs(x=expression("LW"%down%"[W/m²]"))

EDIT:

Ok this is kind of a hack but it works. In the documentation of plotmath you can see that %down% works like an operator, so it requires two strings before and after. The two empy strings serve for that:

ggplot(data = data, aes(x=x, y=y))+
  geom_point()+
  labs(x=expression(paste(LW[""%down%""]," [W/m²]")))

enter image description here

like image 115
tyumru Avatar answered Apr 14 '26 11:04

tyumru


From help('plotmath'), you can use group('[',w / m^2, ']') to wrap your units in delimiters

ggplot(data = data, aes(x=x, y=y))+
  geom_point()+
  labs(x=expression(LW %down% group("[", w/m^2, "]")))

enter image description here

or bgroup if you want scaleable delimiters

ggplot(data = data, aes(x=x, y=y))+
  geom_point()+
  labs(x=expression(LW %down% bgroup("[", over(w, m^2), "]")))

enter image description here

like image 24
mnel Avatar answered Apr 14 '26 12:04

mnel