如何更改ggplot标签(scale_x_discrete)的一部分为斜体,另一部分为新行

m1m5dgzv  于 2023-11-14  发布在  其他
关注(0)|答案(2)|浏览(137)

我想修改scale_x_discrete,其中我希望k_mat中的k以斜体显示,而“Absent”和“Present”在新的一行中。我使用这些函数:

p <- ggplot(chr2snp, aes(x = k.mat, y = calories, fill = k.mat)) +
  geom_boxplot(outlier.size = 0.2) +
  scale_fill_manual(values = c("Absent" = "limegreen", "k.mat" = "royalblue1")) +
  scale_x_discrete(
    limits = c("Absent", "Present"),
    labels = c("k-mat\nAbsent", "k-mat\nPresent")
  ) +
  geom_jitter(color = "black", size = 0.0005, alpha = 0.8, width = 0.35) +
  scale_y_log10() +
  theme(text = element_text(size = 15)) +
  xlab(" ") +
  ylab(expression(paste("Cal conc. [µg g" ^-1, "]"))) +
  theme_classic() +
  theme(legend.position = "none", panel.border = element_rect(color = "black", fill = NA, linewidth = 0.5))+
  theme(axis.text.x = element_text(color = "black"))+
  theme(axis.text.y = element_text(color = "black",size=9))

字符串
这是我的数据:
数据输入(chr 2snp)

structure(list(pos = c("a1", "a2", "a3", "a4", "a5", "a6", "a7", 
"a8", "a9", "a10", "a11", "a12", "a13", "a14", "a15", "a16", 
"a17", "a18", "a19", "a20", "a21", "a22", "a23", "a24", "a25", 
"a26", "a27", "a28", "a29", "a30", "a31", "a32", "a33", "a34", 
"a35", "a36", "a37", "a38", "a39", "a40", "a41", "a42", "a43", 
"a44", "a45", "a46", "a47", "a48", "a49", "a50", "a51", "a52", 
"a53", "a54", "a55", "a56", "a57", "a58", "a59", "a60", "a61", 
"a62", "a63", "a64", "a65", "a66", "a67", "a68", "a69", "a70", 
"a71", "a72", "a73", "a74", "a75", "a76", "a77", "a78", "a79", 
"a80", "a81", "a82", "a83", "a84"), calories = c(107.06, 71.8, 
16.31, 37.66, 87.86, 48.43, 142.59, 55.02, 80.98, 8.51, 9.89, 
1.93, 5.4, 9.32, 10.15, 13.45, 16.13, 59.69, 21.3, 487.05, 300.94, 
209.96, 218.53, 34.7, 20.17, 27.53, 306.4, 146.69, 0.25, 0.5, 
158.73, 10.86, 20.01, 27.75, 5.26, 85.34, 132.25, 60.16, 33.88, 
71.42, 223.29, 93.29, 17.21, 31.99, 15.55, 73.91, 31.14, 32.88, 
58.67, 64.19, 54.47, 73.43, 585.93, 192.23, 313.45, 116.85, 190.43, 
190.9, 1013.8, 672.16, 1079.8, 980.96, 1850.05, 271.46, 280.74, 
213.04, 337.04, 589.82, 271.96, 11.45, 1.54, 0.99, 2.26, 459.33, 
227.77, 216.8, 196.47, 14.35, 20.02, 16.02, 27.57, 311.95, 698.46, 
333.56), k.mat = c("Absent", "Absent", "Absent", "Absent", "Absent", 
"Absent", "Absent", "Absent", "Absent", "Absent", "Absent", "Absent", 
"Absent", "Absent", "Absent", "Absent", "Absent", "Absent", "Absent", 
"Absent", "Absent", "Absent", "Absent", "Absent", "Absent", "Absent", 
"Absent", "Absent", "Absent", "Absent", "Absent", "Absent", "Absent", 
"Absent", "Absent", "Absent", "Absent", "Absent", "Absent", "Present", 
"Present", "Present", "Present", "Present", "Present", "Present", 
"Present", "Present", "Present", "Present", "Present", "Present", 
"Present", "Present", "Present", "Present", "Present", "Present", 
"Present", "Present", "Present", "Present", "Present", "Present", 
"Present", "Present", "Present", "Present", "Present", "Present", 
"Present", "Present", "Present", "Present", "Present", "Present", 
"Present", "Present", "Present", "Present", "Present", "Present", 
"Present", "Present")), class = "data.frame", row.names = c(NA, 
-84L))


x1c 0d1x的数据
我只想让k-mat中的k用斜体表示。
谢谢你,

vm0i2vca

vm0i2vca1#

另一种方法是使用ggtext

library(ggplot2)
library(ggtext)

ggplot(chr2snp, aes(x = k.mat, y = calories, fill = k.mat)) +
  geom_boxplot(outlier.size = 0.2) +
  scale_fill_manual(values = c("Absent" = "limegreen", 
                               "k.mat" = "royalblue1")) +
  scale_x_discrete(limits = c("Absent", "Present"),
                   labels = c("*k-mat*<br>Absent", "*k-mat*<br>Present")) +
  geom_jitter(color = "black", size = 0.5, alpha = 0.8, width = 0.35) +
  scale_y_log10() +
  theme(text = element_text(size = 15)) +
  labs(x = NULL, y = expression(paste("Cal conc. [µg g" ^-1, "]"))) +
  theme_classic(base_size = 20) +
  theme(legend.position = "none", 
        panel.border = element_rect(color = "black", fill = NA, linewidth = .5),
        axis.text.x = element_markdown(color = "black"), 
        axis.text.y = element_text(color = "black"))

字符串


的数据

qlckcl4x

qlckcl4x2#

一种方法可以是expressionatopitalic的组合:与@stefan_aus_汉诺威提供的链接相比,这里的新功能是atop

p <- ggplot(chr2snp, aes(x = k.mat, y = calories, fill = k.mat)) +
  geom_boxplot(outlier.size = 0.2) +
  scale_fill_manual(values = c("Absent" = "limegreen", "Present" = "royalblue1")) +
  scale_x_discrete(
    limits = c("Absent", "Present"),
    labels = c(expression(atop(italic("k")[plain("mat")], "Absent")), 
               expression(atop(italic("k")[plain("mat")], "Present")))
  ) +
  geom_jitter(color = "black", size = 0.0005, alpha = 0.8, width = 0.35) +
  scale_y_log10() +
  theme(text = element_text(size = 15)) +
  xlab(" ") +
  ylab(expression(paste("Cal conc. [µg g" ^-1, "]"))) +
  theme_classic() +
  theme(legend.position = "none", panel.border = element_rect(color = "black", fill = NA, linewidth = 0.5))+
  theme(axis.text.x = element_text(color = "black"))+
  theme(axis.text.y = element_text(color = "black", size=9))

print(p)

字符串
x1c 0d1x的数据

相关问题