How to put an upper subscript in R?

Okay so I am doing a pheatmap, but I am unable to put "+" as an upper subscript on "Li" in my plot.
This is the code I used:

ggplot(data_long, aes(x = Treatment, y = Li_Part, fill = Li_Value)) +
geom_tile(color = "white", linewidth = 0.5) +
scale_fill_gradient(low = "white", high = "blue") +
facet_wrap(~ Species, scales = "free_y", ncol = 2) +
labs(title = "Li Accumulation Across Species",
x = "Lithium Treatments (ppm)",
y = "Li Uptake in Plant Parts") +
theme_minimal() +
scale_fill_gradientn(colours = c("#1f77b4", "#ff7f0e", "#2ca02c", "#d62728"),
values = c(0, 0.1, 0.4, 0.7, 1),
breaks = c(0, 50, 100, 150, 200, 250, 300),
labels = c("0", "50", "100", "150", "200", "250", "300")) +
theme(axis.text.x = element_text(angle = 45, margin = margin(t = 10)),
axis.title.x = element_text(margin = margin(r = 10)),
axis.title.y = element_text(margin = margin(r = 10)),
legend.position = 'right',
plot.title = element_text(hjust = 0.5)) +
theme(text = element_text(family = "Times New Roman"),
strip.text = element_text(face = "italic"))
I tried to get help from Meta AI.. and the code Meta AI was giving me is not working:
This is the code Metal AI gave me:
levels = c("Li_in_Leaves", "Li_in_Shoot", "Li_in_Root"),
labels = c(expression(Li^"+"~"in Leaves"),
expression(Li^"+"~"in Shoot"),
expression(Li^"+"~"in Root")))
When I used this code, the labels in the plot were changed into exactly these symbols and letters.

Hi @Sahar,

use expression("Li"^"+" ~ "Accumulation Across Species")

Data set for those who want to replicate
data_long <- structure(
  list(
    Species = c(
      "Arabidopsis",
      "Arabidopsis",
      "Arabidopsis",
      "Arabidopsis",
      "Arabidopsis",
      "Arabidopsis",
      "Arabidopsis",
      "Arabidopsis",
      "Arabidopsis",
      "Arabidopsis",
      "Arabidopsis",
      "Arabidopsis",
      "Arabidopsis",
      "Arabidopsis",
      "Arabidopsis",
      "Spinach",
      "Spinach",
      "Spinach",
      "Spinach",
      "Spinach",
      "Spinach",
      "Spinach",
      "Spinach",
      "Spinach",
      "Spinach",
      "Spinach",
      "Spinach",
      "Spinach",
      "Spinach",
      "Spinach",
      "Tomato",
      "Tomato",
      "Tomato",
      "Tomato",
      "Tomato",
      "Tomato",
      "Tomato",
      "Tomato",
      "Tomato",
      "Tomato",
      "Tomato",
      "Tomato",
      "Tomato",
      "Tomato",
      "Tomato",
      "Corn",
      "Corn",
      "Corn",
      "Corn",
      "Corn",
      "Corn",
      "Corn",
      "Corn",
      "Corn",
      "Corn",
      "Corn",
      "Corn",
      "Corn",
      "Corn",
      "Corn"
    ),
    Treatment = c(
      "0 ppm",
      "0 ppm",
      "0 ppm",
      "50 ppm",
      "50 ppm",
      "50 ppm",
      "100 ppm",
      "100 ppm",
      "100 ppm",
      "150 ppm",
      "150 ppm",
      "150 ppm",
      "200 ppm",
      "200 ppm",
      "200 ppm",
      "0 ppm",
      "0 ppm",
      "0 ppm",
      "50 ppm",
      "50 ppm",
      "50 ppm",
      "100 ppm",
      "100 ppm",
      "100 ppm",
      "150 ppm",
      "150 ppm",
      "150 ppm",
      "200 ppm",
      "200 ppm",
      "200 ppm",
      "0 ppm",
      "0 ppm",
      "0 ppm",
      "50 ppm",
      "50 ppm",
      "50 ppm",
      "100 ppm",
      "100 ppm",
      "100 ppm",
      "150 ppm",
      "150 ppm",
      "150 ppm",
      "200 ppm",
      "200 ppm",
      "200 ppm",
      "0 ppm",
      "0 ppm",
      "0 ppm",
      "50 ppm",
      "50 ppm",
      "50 ppm",
      "100 ppm",
      "100 ppm",
      "100 ppm",
      "150 ppm",
      "150 ppm",
      "150 ppm",
      "200 ppm",
      "200 ppm",
      "200 ppm"
    ),
    Li_Part = c(
      "Root",
      "Stem",
      "Leaf",
      "Root",
      "Stem",
      "Leaf",
      "Root",
      "Stem",
      "Leaf",
      "Root",
      "Stem",
      "Leaf",
      "Root",
      "Stem",
      "Leaf",
      "Root",
      "Stem",
      "Leaf",
      "Root",
      "Stem",
      "Leaf",
      "Root",
      "Stem",
      "Leaf",
      "Root",
      "Stem",
      "Leaf",
      "Root",
      "Stem",
      "Leaf",
      "Root",
      "Stem",
      "Leaf",
      "Root",
      "Stem",
      "Leaf",
      "Root",
      "Stem",
      "Leaf",
      "Root",
      "Stem",
      "Leaf",
      "Root",
      "Stem",
      "Leaf",
      "Root",
      "Stem",
      "Leaf",
      "Root",
      "Stem",
      "Leaf",
      "Root",
      "Stem",
      "Leaf",
      "Root",
      "Stem",
      "Leaf",
      "Root",
      "Stem",
      "Leaf"
    ),
    Li_Value = c(
      102L,
      270L,
      106L,
      71L,
      188L,
      20L,
      102L,
      121L,
      214L,
      87L,
      99L,
      151L,
      130L,
      149L,
      257L,
      293L,
      191L,
      276L,
      160L,
      21L,
      252L,
      235L,
      48L,
      58L,
      169L,
      187L,
      270L,
      189L,
      174L,
      50L,
      54L,
      243L,
      130L,
      134L,
      20L,
      166L,
      273L,
      88L,
      13L,
      241L,
      264L,
      52L,
      91L,
      263L,
      34L,
      205L,
      80L,
      49L,
      1L,
      53L,
      105L,
      259L,
      190L,
      217L,
      43L,
      161L,
      201L,
      269L,
      270L,
      214L
    )
  ),
  class = "data.frame",
  row.names = c(NA, -60L)
)
# Load necessary libraries
library(ggplot2)

# Load the dataset data_long - its in the collapsible above

# Create the heatmap
ggplot(data_long, aes(x = Treatment, y = Li_Part, fill = Li_Value)) +
  geom_tile(color = "white", linewidth = 0.5) +
  scale_fill_gradient(low = "white", high = "blue") +
  facet_wrap( ~ Species, scales = "free_y", ncol = 2) +
  labs(title = expression("Li"^"+" ~ "Accumulation Across Species"), x = "Lithium Treatments (ppm)", 
       y = "Li Uptake in Plant Parts") +
  theme_minimal() +
  scale_fill_gradientn(
    colours = c("#1f77b4", "#ff7f0e", "#2ca02c", "#d62728"),
    values = c(0, 0.1, 0.4, 0.7, 1),
    breaks = c(0, 50, 100, 150, 200, 250, 300),
    labels = c("0", "50", "100", "150", "200", "250", "300")
  ) +
  theme(
    axis.text.x = element_text(angle = 45, margin = margin(t = 10)),
    axis.title.x = element_text(margin = margin(r = 10)),
    axis.title.y = element_text(margin = margin(r = 10)),
    legend.position = 'right',
    plot.title = element_text(hjust = 0.5),
    text = element_text(family = "Times New Roman"),
    strip.text = element_text(face = "italic")
  )

Result

2 Likes

It worked.. Thank you :grin:

1 Like