Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R generate label with whitespaces

Tags:

r

I try to generate a label with superscript and therefore using parse. I seem to cannot use whitespaces, this works:

GTVol <- 1:10
measuredVol <- 1:10
dat <- data.frame(GTVol, measuredVol)
ggplot(dat, aes(GTVol, measuredVol)) + 
  geom_point() + 
  geom_abline(slope=1) +
  xlab(parse(text='HarvestedVolume(m^3)')) + 
  ylab("QSM Volume (m^3)")

but this is not working:

GTVol <- 1:10
measuredVol <- 1:10
dat <- data.frame(GTVol, measuredVol)
ggplot(dat, aes(GTVol, measuredVol)) + 
  geom_point() + 
  geom_abline(slope=1) +
  xlab(parse(text='Harvested Volume (m^3)')) + 
  ylab("QSM Volume (m^3)")

giving me the following error:

Error in parse(text = "Harvested Volume (m^3)") : 
  <text>:1:11: unexpected symbol
1: Harvested Volume
like image 798
Jan Hackenberg Avatar asked Oct 26 '25 09:10

Jan Hackenberg


1 Answers

Change the space character by adding ~

library(ggplot2)
ggplot(dat, aes(GTVol, measuredVol)) + 
  geom_point() + 
   geom_abline(slope=1) +
   xlab(parse(text='Harvested~Volume~(m^3)')) + 
   ylab("QSM Volume (m^3)")

-output

enter image description here


Or use gsub for dynamic replacement

  ...
   xlab(parse(text=gsub("\\s+", "~", 'Harvested Volume (m^3)'))) + 
   ...
like image 62
akrun Avatar answered Oct 28 '25 00:10

akrun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!