Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

change single categorical geom_point interval label to greek expression ggplot

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)

>Solution :

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading