gpt4 book ai didi

r - 如何在ggplot2中将表达式设置为构面的轴文本?

转载 作者:行者123 更新时间:2023-12-04 14:45:26 24 4
gpt4 key购买 nike

我试图在 ggplot2 中的 facet 环境中将表达式设置为 x 轴文本标签长度不等。例如:

dat <- structure(list(Species = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L), .Label = c("A", "B"), class = "factor"), Individual = structure(c(1L,
2L, 3L, 5L, 1L, 2L, 3L, 4L, 5L), .Label = c("1", "2", "3", "4",
"expression(bar(\"x\"))"), class = "factor"), mean = c(45, 32,
100, 59, 65, 110, 87, 93, 88.75), min = c(34, 20, 89, 47.66666667,
54, 100, 67, 85, 76.5), max = c(54, 42, 110, 68.66666667, 76,
120, 99, 105, 100)), .Names = c("Species", "Individual", "mean",
"min", "max"), class = "data.frame", row.names = c(NA, -9L))

require(ggplot2)

enter image description here

This answer描述了如何在没有分面的情况下做到这一点。我管理设置 labels ,但我找不到如何分别为每个方面设置标签:
ggplot(dat, aes(x = Individual, y = mean, ymin = min, ymax = max, color = Species)) + 
geom_pointrange() +
facet_wrap(~Species, scales = "free") +
scale_x_discrete(labels = c("1", "2", "3", expression(bar("x")))) +
theme_grey(base_size = 18)

enter image description here

指定 labels 的长向量或列表似乎不起作用:
ggplot(dat, aes(x = Individual, y = mean, ymin = min, ymax = max, color = Species)) + 
geom_pointrange() +
facet_wrap(~Species, scales = "free") +
scale_x_discrete(labels = c("1", "2", "3", expression(bar("x")), "1", "2", "3", "4", expression(bar("x")))) +
theme_grey(base_size = 18)

enter image description here
ggplot(dat, aes(x = Individual, y = mean, ymin = min, ymax = max, color = Species)) + 
geom_pointrange() +
facet_wrap(~Species, scales = "free") +
scale_x_discrete(labels = list(c("1", "2", "3", expression(bar("x")), c("1", "2", "3", "4", expression(bar("x")))))) +
theme_grey(base_size = 18)

enter image description here

有没有办法在 ggplot2 中做到这一点? ?

最佳答案

您可以将中断参数添加到 scale_x_discrete:

ggplot(dat, aes(x=Individual, y = mean,ymin = min, ymax = max, color=Species)) + 
geom_pointrange() + facet_wrap(~Species, scales = "free_x") +
scale_x_discrete(breaks=levels(factor(dat$Individual)),
labels = c("1", "2", "3", "4", expression(bar("x")))) +
theme_grey(base_size = 18)

enter image description here

关于r - 如何在ggplot2中将表达式设置为构面的轴文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21646100/

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