gpt4 book ai didi

R ggplot2在同一类别标签中使用斜体和非斜体

转载 作者:行者123 更新时间:2023-12-04 11:29:21 24 4
gpt4 key购买 nike

对于我的 ggplot 图,我想在条形图上标记类别,第一个单词为斜体,而以下单词为非斜体。我希望类别标签如下所示:

葡萄球菌 (OTU 1)

链球菌 (OTU 300)

我找到了使用 expression() 的例子我可以将一些类别标签斜体化,但我希望能够为许多不同的类别执行此操作。

绘制图的代码如下(但我的数据有更多的条要绘制)。

library(ggplot2)

data <- data.frame(
bactname = c("Staphylococcaceae", "Moraxella", "Streptococcus", "Acinetobacter"),
OTUname = c("OTU_1", "OTU_2", "OTU_3", "OTU_4"),
value = c(-0.5, 0.5, 2, 3)
)

data$name <- paste0(
data$bactname, " (", data$OTUname, ")"
)
data$name <- factor(
data$name,
levels = data$name[order(data$value)], ordered = TRUE
)

ggplot(data, aes(name, value)) +
geom_col() + coord_flip()



创建于 2020-01-28 由 reprex package (v0.3.0)

最佳答案

我会使用胶水和 ggtext 包。

library(tidyverse)
library(ggtext)
library(glue)

data <- data.frame(
bactname = c("Staphylococcaceae", "Moraxella", "Streptococcus", "Acinetobacter"),
OTUname = c("OTU_1", "OTU_2", "OTU_3", "OTU_4"),
value = c(-0.5, 0.5, 2, 3)
)

data %>% mutate(
name = glue("*{bactname}* ({OTUname})"),
name = fct_reorder(name, value)
) %>%
ggplot(aes(name, value)) +
geom_col() + coord_flip() +
theme(axis.text.y = element_markdown())

创建于 2020-01-29 由 reprex package (v0.3.0)

关于R ggplot2在同一类别标签中使用斜体和非斜体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39282293/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com