How to remove select labels in ggplot geom_bar plot and center these labels?

FishyFishies

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:

current plot

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?

Desired plot: desired plot

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: remove duplicate labels & remove selected labels

Also, I know this is a separate question but maybe the answer is simple... is it possible to arrange the legend in the same order as the plot (have the legend in this order: Byths, Chiro.Pupae, Amphipod, Chiro.Larvae, etc...)?

Allan Cameron

I think this just requires a bit of data manipulation. Remember a discrete axis is "really" a numeric axis with factor level labels at the integers, so some data wrangling allows calculation of the x, y positions of each label.

To get the legend in the same order as the x axis, simply reorder the fill aesthetic the same as you reorder the x aesthetic:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to vertically center labels with geom_bar (ggplot2)

ggplot: geom_bar stacking order and labels

Remove select labels from plot ggplot2

How to put labels over geom_bar for each bar in R with ggplot2

How insert labels to the ggplot plot?

How to add custom labels from a dataset on top of bars using ggplot/geom_bar in R?

How can I put labels on geom_bar() in ggplot2 (without a manual y_position)

ggplot : how add labels to multiple plot (with geom_text, no legend)?

R ggplot2 geom_bar add labels with % sign

How to position labels on grouped bar plot columns in ggplot2

How to round off data labels for a bar plot in geom_text in ggplot2?

How to add percentage labels in proper positions on ggplot bar plot with geom_text when using facet wrap and remove empty space between bars?

geom_bar overlapping labels

How to remove the zero labels in Histogram plot in R?

How to align bar plot labels to the left and right of a horizontal bar plot in ggplot2?

How to select which labels to see on plot axis

Remove repeated category labels from faceted bar plot created with ggplot2

R: ggplot2 geom_text data labels for binned geom_bar

How do I remove tick labels in a ggplot without affecting the plot size?

In ggplot geom_bar, the y axis labels are cluttered at the bottom of bar chart

R: change bar labels in geom_bar

How to plot labels to corresponding line (on this plot) in ggplot2?

Add labels to the center of a geom_curve line (ggplot)

Add labels to bar plot

How to center x/y labels on the visible axes spine, not the plot area

How to place geom_text labels in the correct position with a grouped box plot in ggplot

R ggplot2: labels inside bars, no stacked geom_bar

ggplot2: Value labels in stacked barplot with geom_bar(position = 'fill')

Set starting point for breaks for labels in ggplot using geom_bar [R]