gpt4 book ai didi

r - geom_col 值乱序

转载 作者:行者123 更新时间:2023-12-02 02:50:39 36 4
gpt4 key购买 nike

我是 R 新手。我在以下 geom_col 图表中的 geom_text 元素的排序方面遇到问题。

我相信它与 position =position_dodge2(preserve = "single") 行有关,但我不确定。

附加示例代码和输出。正如您所看到的,标签是错误的 - eb 应该互换,ad 也应该互换>.

眼睛更敏锐(头脑可能更敏锐)的人能看出问题所在吗?

enter image description here

library(ggplot2)
library(stringr)

data <- data.frame(Importance = c("Not important", "Important", "Critical", "Not important", "Important"), Current.Capability = c("Basic", "Undeveloped", "Advanced", "World class", "World class"), Function = c("PM", "Sales", "PM", "Marketing", "Marketing"), Item = c("a","b", "c", "d", "e"))

str(data)
head(data)

width <- 2
position.width <- width - 0.05

ggplot(data, aes(x = Importance, y = Current.Capability, fill=Function)) +
geom_col(position = position_dodge2(preserve = "single"), width = width) +
facet_grid(~Importance, scales = "free_x", space = "free_x") +
geom_text(
aes(
label = stringr::str_wrap(Item, 50)),
lineheight = 0.7,
angle=90,
size = 5,
hjust=0,
vjust=0.5,
y = 0.5,
position = position_dodge2(preserve = "single", width = position.width)) +
theme(axis.text.x = element_blank(), axis.ticks = element_blank()) +
theme(legend.position="bottom")+
scale_y_discrete(limits = c("Undeveloped", "Basic", "Advanced", "World class")) +
xlab("Importance") + ylab("Current Capability")

最佳答案

干得好。也许你可以尝试在geom_text的美学中添加group = Importance。即“显式定义分组结构”,参见grouping 。另外,here是一个相关案例。

ggplot(data, aes(x = Importance, y = Current.Capability, fill=Function)) +
geom_col(position = position_dodge2(preserve = "single"), width = width) +
facet_grid(~Importance, scales = "free_x", space = "free_x") +
geom_text(
aes(
label = stringr::str_wrap(Item, 50),
group = Importance), # explicitly define the grouping structure
lineheight = 0.7,
angle=90,
size = 5,
hjust=0,
vjust=0.5,
y = 0.5,
position = position_dodge2(preserve = "single", width = position.width)) +
theme(axis.text.x = element_blank(), axis.ticks = element_blank()) +
theme(legend.position="bottom")+
scale_y_discrete(limits = c("Undeveloped", "Basic", "Advanced", "World class")) +
xlab("Importance") + ylab("Current Capability")

enter image description here

关于r - geom_col 值乱序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53731096/

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