簡體   English   中英

ggplot2 - x 軸上的上標刻度文本

[英]ggplot2 - superscript in x axis ticks text

我試圖讓 x 軸上的兩個變量的文本具有上標。 我希望“+”是上標,但無論我嘗試什么,它最終都會顯示整個代碼。 我嘗試為它制作單獨的標簽,並嘗試使其更簡單

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"))

在另一個圖表中(經過大量搜索!)我設法讓標題在下面的代碼中包含上標,但它不適用於刻度標簽。

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

這是圖表的代碼。 希望有人能夠告訴我我做錯了什么?

  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"))

x 軸刻度標簽中沒有上標的箱線圖

您可以使用ggtext

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())

reprex 包於 2022-06-14 創建 (v2.0.1)

您可以做的是使用bquote手動定義帶有上標的命名標簽向量,然后使用scale_x_discrete(labels = ...)讓這些標簽顯示在正確的刻度上。

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)

reprex 包於 2022-06-14 創建 (v2.0.1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM