Home > database >  ggplot2 - superscript in x axis ticks text
ggplot2 - superscript in x axis ticks text

Time:06-14

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

CodePudding user response:

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)

CodePudding user response:

You can use 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)

  • Related