Home > Back-end >  change single categorical geom_point interval label to greek expression ggplot
change single categorical geom_point interval label to greek expression ggplot

Time:06-04

I need to change a single categorical y axis value in my plot to a greek character, and I can't figure it out.

library(tidyverse)
df = data.frame(y = c('a', 'b', 'c', 'd'), 
                xmin = rep(-2, 4), 
                x = rep(0, 4), 
                xmax = rep(2, 4))

df %>% ggplot(aes(y = y, x = x, xmin = xmin, xmax = xmax))  
              geom_pointrange(show.legend = F) 

I would like to plot the 'd' axis tick label as δ15D and leave the other 3 y values just as they are.

I have tried to change the 'd' to the greek script using expression like this

names <- df %>% pull(y)%>% recode( "d" = 'expression(delta^15~D)' )

df %>% ggplot(aes(y = y, x = x, xmin = xmin, xmax = xmax))  
              geom_pointrange(show.legend = F)  
              scale_y_discrete(labels = names)

But that just prints the words 'expression(delta^15~D)'

This doesn't work either

names <- df %>% pull(y)%>% recode( "d" = paste0('expression(delta^15~D)') )

df %>% ggplot(aes(y = y, x = x, xmin = xmin, xmax = xmax))  
              geom_pointrange(show.legend = F)  
              scale_y_discrete(labels = names)

CodePudding user response:

You can use expression in scale_y_discrete. From there, specify the expression for "d" only.

library(ggplot2)
df = data.frame(y = c('a', 'b', 'c', 'd'), 
                xmin = rep(-2, 4), 
                x = rep(0, 4), 
                xmax = rep(2, 4))

ggplot(df, aes(y = y, x = x, xmin = xmin, xmax = xmax))  
  geom_pointrange(show.legend = F)   
  scale_y_discrete(labels = c("d" = expression(delta^15~D)))

Created on 2022-06-04 by the reprex package (v2.0.1)

  • Related