R语言 如何在ggplot2 facet标签中使用数学表达式

kupeojn6  于 2023-10-13  发布在  其他
关注(0)|答案(1)|浏览(138)

在下面的图中,我想用delta代替“d”,在末尾包含一个per mil符号,并在中使用上标:d13C、d15N和d34S。我用双标图(例如,plot + ylab(expression(paste(delta^{15}, "N (\u2030)"))))。然而,在所附的图中,我使用了facet_grid,这可能会改变获取它的方式。下面是使用的脚本:

group.violin<-ggplot(group.data, aes(x=Culture2, group = Culture2, y=value)) +
  stat_summary(aes(x = Culture2, y = value, color= Species, group = Species), fun = "mean", 
               geom = "line", lwd = 2, alpha = 0.5)+
  stat_summary(geom="point")+
  geom_violin(aes(x = Culture2, y = value, fill = Species), alpha = 0.4, color = NA)+
  geom_point(aes(x = Culture2, y = value, fill = Species), size = 3, color = "black", pch = 21,
             position = position_jitter(width = 0.05, seed = 12))+
  facet_grid(variable ~ Species, scales = "free_y") +
  xlab("")+ 
  ylab("")+
  theme_classic()+
  theme(strip.background = element_rect(colour = "black", fill = "grey85", size = 0.1),
        panel.border=element_rect(colour="black",size = 0.1, fill = NA),
        panel.spacing = unit(0.3, "lines"),
        axis.text.y = element_text(size = 12, colour = "black"),
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        legend.position = "bottom",
        legend.text=element_text(size=12, face="italic"),
        legend.title=element_text(size=12,face="bold"),
        strip.text.x=element_text(size=12,face="bold.italic"),
        strip.text.y=element_text(size=12,face="bold"))

谢谢

fumotvh3

fumotvh31#

我们可以使用sub将您的variable更改为您期望的标签,然后将labeller=label_parsed添加到facet_grid的调用中。
使用我的示例数据(如下),原始绘图代码:

group.data |>
  transform(
    variable2 = sub("d([0-9]+)(.*)", "delta^{\\1}*'\\2 (\u2030)'", variable)
  ) |> 
  ggplot(aes(x=Culture2, group = Culture2, y=value)) +
  stat_summary(aes(x = Culture2, y = value, color= Species, group = Species), fun = "mean", 
               geom = "line", lwd = 2, alpha = 0.5)+
  stat_summary(geom="point")+
  geom_violin(aes(x = Culture2, y = value, fill = Species), alpha = 0.4, color = NA)+
  geom_point(aes(x = Culture2, y = value, fill = Species), size = 3, color = "black", pch = 21,
             position = position_jitter(width = 0.05, seed = 12))+
  facet_grid(variable2 ~ Species, scales = "free_y", labeller = label_parsed) +
  xlab("")+ 
  ylab("")+
  theme_classic()+
  theme(strip.background = element_rect(colour = "black", fill = "grey85", size = 0.1),
        panel.border=element_rect(colour="black",size = 0.1, fill = NA),
        panel.spacing = unit(0.3, "lines"),
        axis.text.y = element_text(size = 12, colour = "black"),
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        legend.position = "bottom",
        legend.text=element_text(size=12, face="italic"),
        legend.title=element_text(size=12,face="bold"),
        strip.text.x=element_text(size=12,face="bold.italic"),
        strip.text.y=element_text(size=12,face="bold"))
# No summary function supplied, defaulting to `mean_se()`
# No summary function supplied, defaulting to `mean_se()`
# No summary function supplied, defaulting to `mean_se()`
# No summary function supplied, defaulting to `mean_se()`
# No summary function supplied, defaulting to `mean_se()`
# No summary function supplied, defaulting to `mean_se()`
# No summary function supplied, defaulting to `mean_se()`
# No summary function supplied, defaulting to `mean_se()`
# No summary function supplied, defaulting to `mean_se()`

不幸的是,这会将粗体/斜体格式的theme d放到facet标签中,所以我们需要更清楚地说明这些。此外,在小平面条中使用粗斜体,而在另一个小平面条中仅使用斜体会带来一个小问题......但我将使用类似plotmath"bolditalic(..)"表达式(字符串)来处理所有内容,然后删除字符串中的bold部分,只保留图例。
对于这一步,我们也将转换Species,然后使用scales::label_parse作为labels=参数添加scale_colour_discrete_fill_ hack。

group.data |>
  transform(
    variable2 = sub("d([0-9]+)(.*)", "bold(delta^{\\1}*'\\2 (\u2030)')", variable),
    Species = sprintf("bolditalic(%s)", Species)
  ) |> 
  ggplot(aes(x=Culture2, group = Culture2, y=value)) +
  stat_summary(aes(x = Culture2, y = value, color= Species, group = Species), fun = "mean", 
               geom = "line", lwd = 2, alpha = 0.5)+
  stat_summary(geom="point")+
  geom_violin(aes(x = Culture2, y = value, fill = Species), alpha = 0.4, color = NA)+
  geom_point(aes(x = Culture2, y = value, fill = Species), size = 3, color = "black", pch = 21,
             position = position_jitter(width = 0.05, seed = 12))+
  facet_grid(variable2 ~ Species, scales = "free_y", labeller = label_parsed) +
  xlab("")+ 
  ylab("")+
  scale_colour_discrete(labels = function(z) scales::label_parse()(sub("bold","",z)))+
  scale_fill_discrete(labels = function(z) scales::label_parse()(sub("bold","",z)))+
  theme_classic()+
  theme(strip.background = element_rect(colour = "black", fill = "grey85", size = 0.1),
        panel.border=element_rect(colour="black",size = 0.1, fill = NA),
        panel.spacing = unit(0.3, "lines"),
        axis.text.y = element_text(size = 12, colour = "black"),
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        legend.position = "bottom",
        legend.text=element_text(size=12),
        legend.title=element_text(size=12,face="bold"),
        strip.text.x=element_text(size=12),
        strip.text.y=element_text(size=12))

样本数据:

set.seed(42)
n <- 420
group.data <- data.frame(Culture2 = sample(c("Auri", "Grav"), size=n, replace=TRUE), value=runif(n), Species=sample(c("Bos", "Cerv", "Equus"), size=n, replace=TRUE), variable=sample(c("d13C", "d15N", "d34S"), size=n, replace=TRUE))
head(group.data)
#   Culture2      value Species variable
# 1     Auri 0.11483254   Equus     d15N
# 2     Auri 0.48275690   Equus     d13C
# 3     Auri 0.97917358   Equus     d34S
# 4     Auri 0.81151679     Bos     d13C
# 5     Grav 0.54291282   Equus     d34S
# 6     Grav 0.07236709    Cerv     d34S

相关问题