Is it possible to remove certain/specific labels from geom_bar ggplot (geom_text) for repeated values and center this value on the plot/bars?
My data:
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))
My plot:
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)))
Which gives:
My question is, is it possible to just have one label for the values that repeat? For example, 0.147 occurs twice (Daphnia and Goby); 0.053 for other prey; 0.021, etc. Can I have these values on the plot just once and have it centered?
I know I could subset and do something like this:
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)))
which removes labels for values that are less than 10%, but I would prefer to keep the others if possible...
I looked at the following SO posts but did not really answer my question:
CodePudding user response:
This is not exactly the desired output. But we could manipulate the values for geom_text
:
library(tidyverse)
library(ggsci)
FO_adult %>%
arrange(-Fi) %>%
group_by(Fi) %>%
mutate(x = ceiling(max(row_number()/2))) %>%
mutate(Fi_new = ifelse(x == row_number(), Fi, NA_real_)) %>%
ggplot(aes(x=reorder(prey_name, -Fi), Fi, fill=prey_name))
geom_bar(stat = "identity")
geom_text(aes(y = Fi, label = round(Fi_new, 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)))