0

I am creating a scatter plot ans want to label the data points, giving them chemical salt names. How do I create subscripts of the numbers in the names of the data points (e.g. 2 in MgCl2)?

The general method of including subscripts MgCl[2] doesn't work, see below for several examples. Also using expression() doesn't work, see below for several examples.

salt.list<-c('NaCl', expression('MgCl'{}[2]*), 'CaCl2') 
salt.list<-c('NaCl', expression(MgCl{}[2]*), 'CaCl2') 
salt.list<-c('NaCl', expression(MgCl[2]*), 'CaCl2') 
salt.list<-c('NaCl', expression('MgCl'[2]*), 'CaCl2') 
salt.list<-c('NaCl', 'MgCl'[2]), 'CaCl2') 
salt.list<-c('NaCl', 'MgCl'['2'], 'CaCl2') 
salt.list<-c('NaCl', 'MgCl'{}['2']), 'CaCl2') 
library(tidyr)
library(dplyr)
library(ggplot2)
library(readr)
library(gridExtra)
library(ggpmisc)
library(ggrepel)
str(saltdata)

#saltdata was imported in RStudio and contained the following data:
#Salt a b
#NaCl 1 1
#MgCl2 2 2
#CaCl2 3 3 

saltdata$x <-as.numeric(saltdata$x)
saltdata$y <-as.numeric(saltdata$y)

z<-expression('NaClO[4]')
z

salt.list<-c('NaCl','MgCl2','CaCl2')
salt.list

(salt_scatter<-ggplot(saltdata, aes(x=a, y=b)) +
    geom_point(size=3) +
    geom_smooth(method = "lm", se=F, col='black') +
    geom_label_repel(aes(a,b,label=paste(salt.list)), size=8) +
    geom_text(x=1.3, y=2, label= expression("R"*{}^2*"=1"), size=8, parse=T) +
    theme(panel.background = element_blank(),
          panel.grid.major = element_blank(),
          axis.line = element_line(size=0.5, linetype = 'solid', colour = 'black')))

The data point label usually shows up just as text, including the added code (which should indicate that it is a subscript), such as :MgCl2, "MgCl"[2], "MgCl"["2"] or MgCl[2].

Annemiek
  • 9
  • 1

0 Answers0