3

I'm trying to get the text of two variables on the x axis to have superscript. I want the '+' to be superscript but it just ends up displaying the whole code no matter what I try. I've tried making separate labels for it and also tried to keep it more simple

test.labs=as_labeller(c('CD4Gated' = 'CD4^+ cells', 'CD8Gated' = 'CD8^+ cells', 'Bcell' 
= "B cells"), default = label_parsed)

bFICOLL + scale_x_discrete(labels=c(CD4Gated = "CD4^+ cells", CD8Gated = "CD8^+ cells", 
Bcell = "B cells"))

In another graph (after lots of searching!) I managed to get the title to include superscript with the code below but it doesn't work with tick labels.

ggtitle('Spring FEC &' ~~ CD4^'+' ~~ 'cells')

This is the code for the graph. Hopefully somebody is able to tell me what I'm doing wrong?

  bFICOLL <-  ggplot(LymphOrder, aes(x=WBC, y=Percentage, fill=Diagnosis, 
  shape=Diagnosis)) + 
  geom_violin() +
  facet_wrap(~Season, scale="free")+
  theme(strip.text.x = element_text(size = 30))+
  geom_point(pch = 21, position = position_jitterdodge())+
  scale_fill_manual(values=c("#56B4E9", "#009E73"))+ 
  scale_shape_manual(values=c(1,2))+
  theme(panel.background = element_blank(), axis.line = element_line(colour = "black"))+
  theme(strip.background = element_rect( color="black", fill="#FFFFFF", size=1.5, 
  linetype="solid"))+
  ggtitle('Lymphocyte subsets')+
  theme(plot.title = element_text(size = 40, face = "bold")) +
  ylab('Percentage') +
  xlab("Lymphocytes")+
  ylim(0,80)+
  theme(axis.ticks.length = unit(5, "pt"))+
  theme(axis.title.x = element_text(size=20))+
  theme(axis.text.x = element_text(size=15))+
  theme(axis.text.x = element_text(angle = 30, hjust = 1))+
  theme(axis.text.y = element_text(size=20))+
  theme(axis.title.y = element_text(size=20))+
  theme(legend.title = element_blank(),
    legend.text = element_text(size = 20))
  bFICOLL + scale_x_discrete(labels=c(CD4Gated = "CD4^+ cells", CD8Gated = "CD8^+ 
  cells", 
  Bcell = "B cells"))

Boxplot without superscript in x axis tick labels

tjebo
  • 21,977
  • 7
  • 58
  • 94
Len
  • 31
  • 2
  • related https://stackoverflow.com/questions/17334759/subscript-letters-in-ggplot-axis-label – tjebo Jun 14 '22 at 10:49
  • could you *please* create a much simpler example? This question is rather interesting, but the code you're using is overwhelming and also not even reproducible. Please work for example with a simple plot like `ggplot(iris, aes(Species, Sepal.Length)) + geom_boxplot()` – tjebo Jun 14 '22 at 10:50

2 Answers2

3

Using the ggtext package:

library(ggplot2)
library(ggtext)

my_labs <- c(setosa = "CD4^+ cells", versicolor = "CD8^+ 
  cells", virginica = "B cells")

ggplot(iris, aes(Species, Sepal.Length)) + 
  geom_boxplot() +
  scale_x_discrete(labels = my_labs) +
  theme(axis.text.x = element_markdown())

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

zx8754
  • 52,746
  • 12
  • 114
  • 209
tjebo
  • 21,977
  • 7
  • 58
  • 94
  • Sorry for the lengthy code, I'll keep that in mind for any future questions I may have! Thanks for your help. With a more simple graph, your code works. I had ggtext loaded and thought it was not what I needed. Now I'll go through each line of the original graph code to find what it doesn't like. Thanks again, I've been working on this for ages! – Len Jun 14 '22 at 12:58
  • @len This is one basic principle of problem solving: Cut down the problem to the minimum. Trying to reproduce a problem with a minimal example helps to see where the problem really lies and to see possible solutions. This can then reveal problems in the added complexities rather than in the actual basic solution. This is also another reason why we really encourage people to create minimal examples - it requires a thought and analysis process which can help you find the solution yourself. – tjebo Jun 14 '22 at 13:01
0

What you can do is using bquote to manually define a named labels vector with superscript then using scale_x_discrete(labels = ...) to have those labels display at the right ticks.

library(ggplot2)
library(tidyr)
library(dplyr)

# this is just a sample using mtcars data
trial <- mtcars %>%
  mutate(car = rownames(.)) %>%
  pivot_longer(mpg:carb)

first_3 <- trial %>% filter(name == "mpg") %>% head(3)
car_names <- first_3$car
# manually create a vector of 3 labels with superscript
super_labels <- c(bquote(AMC~Javelin^super), bquote(Cadillac~another^super),
                  bquote(Camaro~third^super))
super_labels
#> [[1]]
#> AMC ~ Javelin^super
#> 
#> [[2]]
#> Cadillac ~ another^super
#> 
#> [[3]]
#> Camaro ~ third^super

# assign names to the labels vector which will be used by ggplot
names(super_labels) <- car_names[1:3]
super_labels
#> $`Mazda RX4`
#> AMC ~ Javelin^super
#> 
#> $`Mazda RX4 Wag`
#> Cadillac ~ another^super
#> 
#> $`Datsun 710`
#> Camaro ~ third^super
# plot without using the scale_x_discrete - superscript labels
ggplot(data = first_3) +
  geom_point(aes(x = car, y= value)) +
  theme(axis.text.x = element_text(angle = 30, hjust = 1))

# plot using the scale_x_discrete - superscript labels
ggplot(data = first_3) +
  geom_point(aes(x = car, y= value)) +
  theme(axis.text.x = element_text(angle = 30, hjust = 1)) +
  scale_x_discrete(labels = super_labels)

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

Sinh Nguyen
  • 4,277
  • 3
  • 18
  • 26
  • can you maybe show an example with a superscript "+" in the middle of the label? I guess this is relevant because it changes the expression slightly ? – tjebo Jun 14 '22 at 10:56
  • Thanks for your time on this. You're right about the superscript in the middle changing things. I tried having just the '+' in superscript but got cells as well. This is your code modified to the text I want. super_labels <- c(bquote('B cells'), bquote(CD4^+ cells), bquote(CD8^+ cells)) super_labels Only if you have time to look at this. I should be able to work with these answers. I'm just curious to get your code to work – Len Jun 14 '22 at 13:10