How to stop geom_point from interfering with geom_bar?

Advertisements

I have a complicated (for me atleast) ggplot. There are 3 things I just can’t figure out how to change. 1) How to I change the color of the geom_point from pink to my defined color code? 2) How do I remove the word colour from the legend? 3) How to I make it so that the geom_bar legend does not show black points on it?

I’m missing how to make the geom_point and geom_bar not interfere with each other. Right now I don’t understand when I’m manipulating wag geom.

AG_PROGNOOS <- data.frame(
  AMETIALAGRUPP = c("Administratiivtöö lihtametnikud","Administratiivtöö lihtametnikud","Administratiivtöö lihtametnikud","Administratiivtöö lihtametnikud"),
  TOOJOU_LIIKUVUS = c("Tööjõupakkumine","Tööjõupakkumine","Tööjõupakkumine","Tööjõuvajadus"),
  TUNNUS = c("HOIVEPROGNOOS_10A","PENSION","Hõive muutus + pension","PROGNOOSITUD_LOPETAMISTE_ARV"),
  INIMEST = c(-990,2570,1580,1690)
)

HOIVE <- data.frame(
  INIMEST = 13348.5
)




 ggplot(AG_PROGNOOS, aes(fill=TUNNUS, x=TOOJOU_LIIKUVUS, y=INIMEST))+
    geom_bar(position=position_dodge(), stat="identity", width = 0.5,
             data=subset(AG_PROGNOOS, TUNNUS !="Hõive muutus + pension"))+
    geom_point(aes(color="Tööjõuvajadus kokku"), 
               data=subset(AG_PROGNOOS, TUNNUS =="Hõive muutus + pension"), 
               size=5, position=position_dodge(width=0.5))+
    scale_fill_manual(values = c("PROGNOOSITUD_LOPETAMISTE_ARV" = "#0071ce",
                                 "PENSION" = "#ffd300",
                                 "HOIVEPROGNOOS_10A" = "#ff9e16"),
                      name=" ",
                      labels=c("Hõive kasv/kahanemine",
                               "Pensionile minejad",
                               "Koolilõpetajad"))+
    labs(x="",
         y="Inimest",
         title = paste("Hõivatute arv 2021 aastal oli", format(round(HOIVE$INIMEST, 0), big.mark = " ")))+
    theme_minimal(base_size = 20)+
    theme(plot.title = element_text(hjust = 0.1))+
    theme(panel.border = element_blank(),
          panel.grid.major.x = element_blank(),
          panel.grid.minor = element_blank(),
          axis.line=element_blank())+
    scale_y_continuous(labels=function(x) format(x, big.mark = " ", scientific = FALSE))+
    theme(axis.text.x = element_text(angle = 25, vjust = 1.0, hjust=1))

>Solution :

  1. Add a scale_color_manual(values = 'gray50') to set the color of the point to gray (and obviously change this to whatever color you like)
  2. You can use colour = NULL inside labs to remove the word "colour" (or use the name argument inside scale_color_manual instead.)
  3. You are getting a black dot inside the legend because the geom_point layer is inheriting the fill aesthetic. Move fill = TUNNUS to inside an aes call inside geom_bar

In addition, note that geom_bar(stat = "identity") is just a long way of writing geom_col, and that it’s a good idea to keep all your theme calls at the end of the plotting code so it’s easier to change.

ggplot(subset(AG_PROGNOOS, TUNNUS !="Hõive muutus + pension"), 
       aes(TOOJOU_LIIKUVUS, INIMEST)) +
  geom_col(aes(fill = TUNNUS), position = 'dodge', width = 0.5) +
  geom_point(aes(color = "Tööjõuvajadus kokku"), 
             data = subset(AG_PROGNOOS, TUNNUS =="Hõive muutus + pension"), 
             size = 5, position = position_dodge(width = 0.5)) +
  labs(x = NULL, y = "Inimest", colour = NULL, fill = NULL,
       title = paste("Hõivatute arv 2021 aastal oli", 
                     format(round(HOIVE$INIMEST, 0), big.mark = " "))) +
  scale_y_continuous(labels = ~format(.x, big.mark = " ", scientific = FALSE)) +
  scale_color_manual(values = 'gray50') +
  scale_fill_manual(values = c("PROGNOOSITUD_LOPETAMISTE_ARV" = "#0071ce",
                               "PENSION" = "#ffd300",
                               "HOIVEPROGNOOS_10A" = "#ff9e16"),
                    labels = c("Hõive kasv/kahanemine",
                               "Pensionile minejad",
                               "Koolilõpetajad")) +
  theme_minimal(base_size = 20) +
  theme(plot.title = element_text(hjust = 0.1),
        panel.border = element_blank(),
        panel.grid.major.x = element_blank(),
        panel.grid.minor = element_blank(),
        axis.text.x = element_text(angle = 25, vjust = 1.0, hjust = 1))

Leave a ReplyCancel reply