简体   繁体   中英

Add a mark in specific bar and/or x-axis label in a faceted geom_bar()

I have the following data:

structure(list(validated_1 = c("sombra", "sombra", "sombra", 
"sombra", "sombra", "sombra", "sombra", "sombra", "sombra", "sombra", 
"coscinodiscus", "sombra", "coscinodiscus", "coscinodiscus", 
"sombra", "coscinodiscus", "sombra", "coscinodiscus", "sombra", 
"coscinodiscus", "coscinodiscus", "detritos", "detritos", "coscinodiscus", 
"appendicularia", "detritos", "coscinodiscus", "coscinodiscus", 
"detritos", "coscinodiscus", "langanho", "detritos", "copepodo", 
"langanho", "copepodo", "langanho", "langanho", "coscinodiscus", 
"coscinodiscus", "coscinodiscus"), validated_2 = c("sombra", 
"sombra", "sombra", "sombra", "sombra", "sombra", "sombra", "sombra", 
"sombra", "sombra", "coscinodiscus", "sombra", "coscinodiscus", 
"coscinodiscus", "sombra", "coscinodiscus", "sombra", "coscinodiscus", 
"sombra", "coscinodiscus", "coscinodiscus", "detritos", "detritos", 
"coscinodiscus", "zooplâncton", "detritos", "coscinodiscus", 
"coscinodiscus", "detritos", "coscinodiscus", "langanho", "detritos", 
"zooplâncton", "langanho", "zooplâncton", "langanho", "langanho", 
"coscinodiscus", "coscinodiscus", "coscinodiscus")), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -40L))

I produce this plot:

df %>%
  count(validated_1, validated_2, sort = TRUE, name = "count") %>%
  mutate(groups = c(rep("high N", 2), rep("lower N", 4))) %>%
  ggplot(aes(x = reorder(validated_1, -count), y = count)) +
  geom_col(aes(fill = validated_2)) +
  facet_wrap(~groups, nrow = 2, scales = "free") +
  geom_text(aes(label = count), vjust = -0.5, size = 3)

I would to like add some marks in level copepodo , for example. This mark could be a rectangle over only this bar (It's more adequate) and/or increase the size of this name label.

Thanks all

Answer after comment

You could use geom_label like this:

library(dplyr)
library(ggplot2)
df %>%
  count(validated_1, validated_2, sort = TRUE, name = "count") %>%
  mutate(groups = c(rep("high N", 2), rep("lower N", 4))) %>%
  mutate(mark = case_when(validated_1 == "copepodo" ~ TRUE,
                          TRUE ~ FALSE)) %>%
  ggplot(aes(x = reorder(validated_1, -count), y = count)) +
  geom_col(aes(fill = validated_2)) +
  facet_wrap(~groups, nrow = 2, scales = "free") +
  geom_text(aes(label = count), vjust = -0.5, size = 3) +
  geom_label(aes(label = ifelse(mark, "your mark", NA), group = validated_2), vjust = 2) 
#> Warning: Removed 5 rows containing missing values (geom_label).

Created on 2022-08-10 by the reprex package (v2.0.1)

I am not sure what you want to show as mark, but what you could do is create a new column that indicates which row has "copepodo" to show that in the corresponding bar like this:

library(dplyr)
library(ggplot2)
df %>%
  count(validated_1, validated_2, sort = TRUE, name = "count") %>%
  mutate(groups = c(rep("high N", 2), rep("lower N", 4))) %>%
  mutate(mark = case_when(validated_1 == "copepodo" ~ TRUE,
                          TRUE ~ FALSE)) %>%
  ggplot(aes(x = reorder(validated_1, -count), y = count)) +
  geom_col(aes(fill = validated_2)) +
  facet_wrap(~groups, nrow = 2, scales = "free") +
  geom_text(aes(label = count), vjust = -0.5, size = 3) +
  geom_text(aes(label = ifelse(mark, "your mark", ""), group = validated_2), vjust = 2) 

Created on 2022-08-10 by the reprex package (v2.0.1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM