1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| plotBar3 = function(df, variable1, variable2, value, facette) {
# COULEURS
nvx = levels(as.factor(df[[variable2]]))
myColors <- colorRampPalette(c("blue", "darkblue"))(length(nvx))
names(myColors) <- nvx
# PLOT
p = ggplot(df, aes_string(x=variable1, y=value, fill=variable2)) +
geom_bar(width = 1, stat = "identity", position=position_dodge(width=1)) +
geom_text(aes_string(label=value), size = 3, position=position_dodge(width=1), vjust=-0.2) +
geom_vline(xintercept=seq(1.5, length(unique(df[[variable1]]))-0.5, 1), lwd=0.5, colour="grey") +
facet_wrap(as.formula(paste("~", facette))) +
theme(
panel.spacing = unit(1.2, "lines"),
axis.line = element_blank(),
axis.line.x=element_line(size = 0.5, colour = 'grey'),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
axis.text.y =element_text(color="grey", size=6),
axis.text.x =element_text(color="grey"),
panel.grid.major.y = element_line(size = 0.5, colour = 'grey'),
panel.border=element_blank(),
axis.ticks.x=element_line(size = 0.5, colour = 'grey'),
axis.ticks.y=element_line(size = 0.5, colour = 'grey'),
)
# VARIABLE DE REMPLISSAGE FILL
labels = paste0("T", df[[variable2]])
p = p + scale_fill_manual(name = "Nb de pièces", values = myColors, labels = labels)
# AXE DES Y
p = p + scale_y_continuous(labels=comma, breaks=seq(0, max(df[[value]]), length.out=5))
return(p)
}
plotBar3(df.gl4,
variable1="mode_acquisition",
variable2="nbpprin",
value="prix_median",
facette = "libniv3",
scale_y=FALSE) |
Partager