rggplot2legendgeom-bar

Showing a legend in a faceted plot with both bars and points


My legend is overlaying the point on the legend entries for the bars. enter image description here I don't want the dots in my legend entries for "Shoot 1", etc.

This answer partially fixed my problem, although I think my line for scale_shape_manual is being ignored for some reason. Here's my data:

shootdatalong <- structure(list(Location = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 
1L, 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 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
    6L, 6L, 6L, 6L, 6L, 6L), levels = c("Veg_shoots", "Rep_shoots", 
    "Canopy_height", "Shoot_1_height", "Shoot_2_height", "Shoot_3_height"
    ), class = "factor"), value = c(18L, 9L, 9L, 12L, 7L, 10L, 
    11L, 11L, 9L, 10L, 12L, 10L, 15L, 28L, 22L, 16L, 39L, 26L, 
    22L, 23L, 47L, 14L, 17L, 47L, 70L, 58L, 9L, 35L, 34L, 28L, 
    11L, 6L, 14L, 5L, 4L, 9L, 2L, 5L, 8L, 6L, 41L, 39L, 54L, 
    82L, 83L, 68L, 76L, 63L, 72L, 71L, 7L, 15L, 48L, 41L, 18L, 
    30L, 27L, 0L, 44L, 13L, 21L, 23L, 32L, 14L, 26L, 22L, 42L, 
    21L, 30L, 16L, 1L, 3L, 0L, 4L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 
    2L, 2L, 2L, 3L, 2L, 3L, 5L, 1L, 0L, 5L, 1L, 1L, 2L, 2L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 
    4L, 12L, 2L, 27L, 31L, 12L, 10L, 0L, 22L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 3L, 2L, 0L, 0L, 0L, 1L, 10L, 0L, 
    3L, 1L, 40L, 45L, 50L, 50L, 60L, 50L, 50L, 45L, 55L, 55L, 
    55L, 45L, 40L, 40L, 40L, 35L, 45L, 50L, 55L, 45L, 18L, 15L, 
    15L, 18L, 15L, 12L, 15L, 25L, 20L, 30L, 100L, 60L, 45L, 45L, 
    45L, 50L, 55L, 40L, 50L, 55L, 18L, 0L, 25L, 25L, 25L, 20L, 
    20L, 15L, 15L, 18L, 10L, 15L, 20L, 12L, 13L, 18L, 15L, 0L, 
    17L, 15L, 30L, 35L, 25L, 15L, 25L, 20L, 25L, 25L, 30L, 30L, 
    45L, 62L, 62L, 65L, 37L, 57L, 59L, 40L, 70L, 75L, 60L, 35L, 
    55L, 45L, 38L, 37L, 47L, 47L, 48L, 50L, 21L, 16L, 23L, 15L, 
    17L, 16L, 15L, 25L, 23L, 35L, 60L, 48L, 49L, 40L, 65L, 55L, 
    65L, 45L, 55L, 70L, 24L, 29L, 24L, 25L, 26L, 28L, 22L, 20L, 
    22L, 22L, 15L, 18L, 25L, 16L, 22L, 15L, 20L, 0L, 20L, 18L, 
    38L, 40L, 26L, 22L, 27L, 22L, 30L, 15L, 30L, 50L, 52L, 50L, 
    58L, 65L, 77L, 52L, 50L, 42L, 65L, 70L, 65L, 55L, 52L, 35L, 
    40L, 32L, 45L, 42L, 38L, 48L, 22L, 18L, 12L, 25L, 19L, 17L, 
    18L, 26L, 19L, 26L, 80L, 35L, 45L, 60L, 62L, 48L, 25L, 42L, 
    52L, 55L, 25L, 22L, 15L, 28L, 28L, 22L, 28L, 22L, 40L, 25L, 
    11L, 12L, 22L, 14L, 14L, 22L, 20L, 0L, 21L, 16L, 37L, 36L, 
    18L, 20L, 22L, 25L, 37L, 40L, 37L, 47L, 50L, 50L, 50L, 58L, 
    60L, 57L, 55L, 45L, 55L, 78L, 58L, 60L, 52L, 30L, 35L, 32L, 
    48L, 42L, 45L, 45L, 15L, 15L, 15L, 15L, 14L, 14L, 18L, 27L, 
    15L, 18L, 110L, 55L, 55L, 62L, 48L, 22L, 68L, 46L, 45L, 38L, 
    25L, 30L, 28L, 32L, 31L, 30L, 18L, 18L, 17L, 21L, 14L, 18L, 
    15L, 17L, 12L, 22L, 16L, 0L, 19L, 18L, 39L, 30L, 23L, 18L, 
    32L, 38L, 33L, 25L, 46L, 29L)), row.names = c(NA, -420L), class = "data.frame")

Here's my code for the plot:

ggplot(shootdatalong, aes(x=Location, y=value, fill=variable)
             ) +
  geom_bar(position="dodge", stat="identity", 
           data=subset(shootdatalong, variable==c("Shoot_1_height", "Shoot_2_height", "Shoot_3_height"))
           ) +
  stat_summary(data=subset(shootdatalong, variable=="Canopy_height"), 
               fun='mean', geom='point', size=3
               ) +
  facet_wrap(~Transect_type
             ) +
  theme_bw() +
  theme(panel.grid.major.x = element_blank(),
        panel.grid.major.y = element_line(linewidth =.1),
        axis.text.x = element_text(size=10),
        axis.title.x = element_blank(),
        axis.title.y = element_text(size=14),
        axis.text.y = element_text(size=12),
        axis.ticks.x = element_blank(),
        strip.background = element_rect(fill="#FFFFFF"),
        strip.text.x = element_text(size=10)
        ) +
  ggtitle("Average heights per transect type"
          ) +
  labs(y="Average height per transect (cm)", shape="NULL", color=NULL, fill=NULL
       ) +
  scale_fill_manual(labels = c("Canopy_height"="Canopy height", "Shoot_1_height"="Shoot 1", "Shoot_2_height"="Shoot 2", "Shoot_3_height"="Shoot 3"), 
                    values = c("Canopy_height"="#154360","Shoot_1_height"="#FF5733", "Shoot_2_height"="#1ABC9C", "Shoot_3_height"="#FFC300"),
                    aesthetics = c("color", "fill")
                    ) +
  scale_shape_manual(values=c("Canopy_height"=21, "Shoot_1_height"=NA, "Shoot_2_height"=NA, "Shoot_3_height"=NA)
                     ) +
  guides(fill=guide_legend(override.aes = list(fill=c(NA, "#FF5733", "#1ABC9C", "#FFC300")))
         )

Solution

  • Simply set shape = 21 in stat_summary and get rid of all the then unnecessary stuff like scale_shape_manual and guides():

    library(ggplot2)
    
    ggplot(
      shootdatalong,
      aes(x = Location, y = value, fill = variable)
    ) +
      geom_col(
        position = "dodge",
        data = subset(
          shootdatalong,
          variable %in% c(
            "Shoot_1_height",
            "Shoot_2_height",
            "Shoot_3_height"
          )
        )
      ) +
      stat_summary(
        data = subset(shootdatalong, variable == "Canopy_height"),
        fun = "mean", geom = "point", size = 3, shape = 21
      ) +
      facet_wrap(~Transect_type) +
      theme_bw() +
      theme(
        panel.grid.major.x = element_blank(),
        panel.grid.major.y = element_line(linewidth = .1),
        axis.text.x = element_text(size = 10),
        axis.title.x = element_blank(),
        axis.title.y = element_text(size = 14),
        axis.text.y = element_text(size = 12),
        axis.ticks.x = element_blank(),
        strip.background = element_rect(fill = "#FFFFFF"),
        strip.text.x = element_text(size = 10)
      ) +
      labs(
        title = "Average heights per transect type",
        y = "Average height per transect (cm)",
        fill = NULL
      ) +
      scale_fill_manual(
        labels = c(
          "Canopy_height" = "Canopy height",
          "Shoot_1_height" = "Shoot 1",
          "Shoot_2_height" = "Shoot 2",
          "Shoot_3_height" = "Shoot 3"
        ),
        values = c(
          "Canopy_height" = "#154360",
          "Shoot_1_height" = "#FF5733",
          "Shoot_2_height" = "#1ABC9C",
          "Shoot_3_height" = "#FFC300"
        )
      )
    

    enter image description here