Home > Back-end >  How to label bars per group based on a column in ggplot2?
How to label bars per group based on a column in ggplot2?

Time:05-11

Note: The enter image description here

CodePudding user response:

Is this what you want?

library(tidyverse)

class <- c(1, 4, 1, 3, 2, 2, 4, 1, 4, 5, 2, 4, 2, 2, 2)
prog <- c(
  "Bac2", "Bac2", "Bac2", "Bac", "Master", "Master", "Bac", "Bac", "DEA", "Doctorat",
  "DEA", "Bac", "DEA", "DEA", "Bac"
)
mydata <- data.frame(height = class, prog)
ress <- mydata %>%
  group_by(prog, height) %>%
  tally() %>%
  mutate(prop = n / sum(n))

dff <- ress %>%
  group_by(prog) %>%
  mutate(new = ifelse(any(prop > 0.5), height[prop > 0.5], 99)) |> 
  ungroup()

label_df <- dff %>% group_by(prog) %>% summarise(label = first(new))

dff %>%
  mutate(prog = fct_reorder(prog, desc(new))) %>%
  ggplot(aes(prog, prop))  
  geom_col(aes(fill = fct_rev(as.factor(height))))  
  geom_vline(xintercept = c(.5, 1.5, 2.5, 4.5, 5.5))  
  geom_label(aes(label = label, y = 0.9), data = label_df)  
  scale_y_continuous(labels = scales::percent)  
  theme(legend.position = "null")

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

CodePudding user response:

Is this what you're looking for:

require(dplyr)
#> Loading required package: dplyr
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
require(ggplot2)
#> Loading required package: ggplot2
require(forcats)
#> Loading required package: forcats
class <- c(1, 4,1,3, 2, 2,4, 1, 4, 5, 2, 4, 2,2,2) 
prog <- c("Bac2", "Bac2","Bac2","Bac", "Master", "Master","Bac", "Bac", "DEA", "Doctorat", 
          "DEA", "Bac", "DEA","DEA","Bac")
mydata <- data.frame(height = class, prog)
ress=mydata %>% 
  group_by(prog,height) %>% 
  tally() %>% 
  mutate(prop = n/sum(n)) %>% 
  ungroup %>% 
  mutate(prog = factor(prog, levels=c("Bac", "Bac2", "Doctorat", "DEA", "Master"))) 
dff=ress %>% 
  group_by(prog) %>% 
  mutate(new = ifelse(any(prop > 0.5), height[prop > 0.5], 99))

dff2 <- dff %>% 
  group_by(prog) %>% 
  slice_tail(n=1) %>% 
  mutate(prog = as.numeric(prog)) %>% 
  group_by(new) %>% 
  summarise(x = mean(as.numeric(prog)))
  


dff%>%ggplot(aes(x=prog, y=prop, fill=fct_rev(as.factor(height)))) 
  geom_col()  
  geom_vline(xintercept=c(.5, 1.5, 2.5, 3.5, 5.5), linetype=2)   
  geom_text(data=dff2, aes(x=x, y=1, label = new), nudge_y = .05, inherit.aes=FALSE)   
  scale_y_continuous(labels = scales::percent) 
  theme_classic()   
  theme(legend.position = 'null')

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

  • Related