Home > database >  bar chart R geom_bar changing legend and x axis
bar chart R geom_bar changing legend and x axis

Time:06-21

i am trying to plot in a bar chart the number of doctors per medical speciality. I have the following code but i can't manage to change the angle of the text (speciality names) on the x axis, they overlap and it's unreadable. Likewise, i can't manage to remove the key of the legend (its redundant with what is written on the x axis). How could i proceed ?

library(ggplot2)
library(dplyr)

"Spécialité" = Evol_praticiens_spé_amb[,1]
"Nombre de praticiens en 2021" = Evol_praticiens_spé_amb[,2]

ggplot(Evol_praticiens_spé_amb, aes(x = Spécialité, y = `Nombre de praticiens en 2021`,
                                     fill = as.factor(Spécialité)))   
  geom_bar(stat = "identity", position = "dodge")  
  ggtitle("Repartition des praticiens en ambulatoire en 2021")
  theme(plot.title = element_text(hjust = 0.5, vjust = 1, size = 10),
    axis.text.x = element_text(angle = 90, hjust = 0.5, size = 5),
    legend.key = NULL,
    axis.text.y = element_text(size = 5))

CodePudding user response:

show.legend = FALSE will remove the legend. And there is a missing after the ggtitle.

library(ggplot2)
library(dplyr)

Evol_praticiens_spé_amb <- tribble(
  ~Spécialité, ~`Nombre de praticiens en 2021`,
  "a", 10,
  "b", 20
)

"Spécialité" <- Evol_praticiens_spé_amb[, 1]
"Nombre de praticiens en 2021" <- Evol_praticiens_spé_amb[, 2]

ggplot(Evol_praticiens_spé_amb, aes(
  x = Spécialité, y = `Nombre de praticiens en 2021`,
  fill = as.factor(Spécialité)
))  
  geom_bar(stat = "identity", position = "dodge", show.legend = FALSE)  
  ggtitle("Repartition des praticiens en ambulatoire en 2021")  
theme(
  plot.title = element_text(hjust = 0.5, vjust = 1, size = 10),
  axis.text.x = element_text(angle = 90, hjust = 0.5, size = 5),
  legend.key = NULL,
  axis.text.y = element_text(size = 5)
)

Created on 2022-06-21 by the reprex package (v2.0.1)

  • Related