Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 212017

Barplot subgroup factor reordering doesn't work

$
0
0

I want to order each bar within year like this: A, B, C.

I have looked at this post: https://ilari.scheinin.fi/ggplot-2-0-and-the-missing-order-aesthetic/

but it doesn't work at all.

Here's my code:

# data --------------------------------------------------------------------


ID <- c('1','2','3','4','5','6','7','7','8','9','10','11')
TypeCourtier <- c('A','A','A','A','B','B','B','B','C','C','C','C')
année_survenance <- c('2009','2010','2011','2012','2009','2010','2011','2012','2009','2010','2011','2012')
moyenne_charge <- c('1515','1551','89754','9148','787','9848','8474','3465','7488','884','8948','8484')
mediane_charge <- c('8185','5919','20409','8979','7777','9294','87484','8488','1881','18819','8484','84444')
totalComptage <- c('9989','849444','848','684','9845','1448','9844','2151','7171','5051','3959','9896')

data <- data.frame(ID, TypeCourtier, année_survenance, moyenne_charge, mediane_charge,totalComptage)


# main --------------------------------------------------------------------

install.packages("ggplot2")
library(ggplot2)
library(magrittr)
library(dplyr)

data$TypeCourtier <- factor(data$TypeCourtier, levels = c("A","B","C"), ordered= TRUE)

data %>% 
  ungroup() %>% 
  arrange(as.integer(TypeCourtier)) %>% 
  ggplot( aes(x=année_survenance, y = moyenne_charge, fill=TypeCourtier)) +
  geom_bar(stat="identity", position=position_dodge())+
  geom_text(aes(label=TypeCourtier),position = position_dodge(width = 0.9),vjust=-0.25) + theme(legend.position = "none")

Viewing all articles
Browse latest Browse all 212017

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>