gpt4 book ai didi

r - 如何删除 ggplot geom_bar 图中的选择标签并将这些标签居中?

转载 作者:行者123 更新时间:2023-12-02 15:47:37 24 4
gpt4 key购买 nike

是否可以从 geom_bar ggplot (geom_text) 中删除重复值的某些/特定标签并将该值置于图/条的中心?

我的数据:

structure(list(prey_name = c("Amphipod", "Byths", "Chiro.Adult", 
"Chiro.Larvae", "Chiro.Pupae", "Chironomidae", "Chydoridae",
"Copepoda", "Cyclopoid", "Daphnia", "Dreissena", "EggMass", "Eurycercidae",
"Fish.Eggs", "Goby", "Hemimysis", "Isopod", "Sphaeriidae", "Trichopteran",
"UID.Fish"), Fi = c(0.189473684210526, 0.515789473684211, 0.0526315789473684,
0.157894736842105, 0.252631578947368, 0.0526315789473684, 0.0105263157894737,
0.0210526315789474, 0.0105263157894737, 0.147368421052632, 0.0842105263157895,
0.0210526315789474, 0.0210526315789474, 0.0105263157894737, 0.147368421052632,
0.0105263157894737, 0.0947368421052632, 0.0421052631578947, 0.0105263157894737,
0.0210526315789474)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -20L))

我的情节:

ggplot(FO_adult, aes(x=reorder(prey_name, -Fi), Fi, fill=prey_name)) +
geom_bar(stat = "identity") +
geom_text(aes(y = Fi, label = round(Fi, digits=3)), vjust = -0.5,
check_overlap = TRUE) +
ggtitle("Frequency of Occurrence") +
labs(x="Prey", fill = "Prey Name", y = "Frequency of Occurrence (%)",
caption = "Source: DNR Diet Data") +
scale_fill_igv(palette = "default") +
theme_bw() +
theme(legend.position = "right",
plot.title = element_text(hjust=0.5),
legend.background = element_rect(fill = "white", color = 1),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.length = unit(0.2,"cm")) +
scale_y_continuous(expand = expansion(mult = c(0,0.1)))

给出:

current plot

我的问题是,是否可以为重复的值设置一个标签?例如,0.147 出现了两次(水蚤和虾虎鱼);其他猎物0.053; 0.021 等。我可以只在图上使用这些值一次并将其居中吗?

想要的情节: desired plot

我知道我可以子集化并做这样的事情:

ggplot(FO_adult, aes(x=reorder(prey_name, -Fi), Fi, fill=prey_name)) +
geom_bar(stat = "identity") +
geom_text(data = subset(FO_adult, Fi > 0.10),
aes(y = Fi, label = round(Fi, digits=3)), vjust = -0.5) +
ggtitle("Frequency of Occurrence") +
labs(x="Prey", fill = "Prey Name", y = "Frequency of Occurrence (%)",
caption = "Source: DNR Diet Data") +
scale_fill_igv(palette = "default") +
theme_bw() +
theme(legend.position = "right",
plot.title = element_text(hjust=0.5),
legend.background = element_rect(fill = "white", color = 1),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.length = unit(0.2,"cm")) +
scale_y_continuous(expand = expansion(mult = c(0,0.1)))

它会删除小于 10% 的值的标签,但如果可能的话,我更愿意保留其他标签...

我查看了以下 SO 帖子,但没有真正回答我的问题:remove duplicate labels & remove selected labels

此外,我知道这是一个单独的问题,但也许答案很简单...是否可以按照与情节相同的顺序排列图例(按以下顺序排列图例:Byths、Chiro.Pupae、Amphipod , Chiro.Larvae, 等等...)?

最佳答案

我认为这只需要一些数据操作。请记住,离散轴“实际上”是一个带有整数因子水平标签的数字轴,因此一些数据整理允许计算每个标签的 x、y 位置。

要获得与 x 轴相同顺序的图例,只需像重新排序 x 美学一样重新排序填充美学:

library(tidyverse)

ggplot(FO_adult, aes(x = reorder(prey_name, -Fi), Fi,
fill = reorder(prey_name, -Fi))) +
geom_col() +
geom_text(data = FO_adult %>%
mutate(label = round(Fi, digits = 3),
prey_num = as.numeric(reorder(prey_name, -Fi))) %>%
group_by(label) %>%
summarize(n = n(),
label = first(label),
Fi = first(Fi),
prey_num = first(prey_num),
prey_name = first(prey_name)),
aes(x = prey_num + (n - 1)/2, y = Fi, label = label), vjust = -0.5,
check_overlap = TRUE) +
ggtitle("Frequency of Occurrence") +
labs(x="Prey", fill = "Prey Name", y = "Frequency of Occurrence (%)",
caption = "Source: DNR Diet Data") +
scale_fill_igv(palette = "default") +
theme_bw() +
theme(legend.position = "right",
plot.title = element_text(hjust=0.5),
legend.background = element_rect(fill = "white", color = 1),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.length = unit(0.2,"cm")) +
scale_y_continuous(expand = expansion(mult = c(0,0.1)))

enter image description here

关于r - 如何删除 ggplot geom_bar 图中的选择标签并将这些标签居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73639807/

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