Home > Net >  Rearrange stacked barplot legend labels without changing plot (and fix tick marks) in R
Rearrange stacked barplot legend labels without changing plot (and fix tick marks) in R

Time:05-30

Is there a way to change the order of factor levels in a stacked barplot legend without changing the order of the plot too (and without mislabeling the data)? I'd like to change the order to "Presence" first, then "Absence".

I'm also having trouble with the tick marks being slightly shifted to one side.

dput(prop)
structure(list(WYR = c(2005L, 2005L, 2006L, 2006L, 2007L, 2007L, 
2008L, 2008L, 2009L, 2009L, 2010L, 2010L, 2011L, 2011L, 2012L, 
2012L, 2013L, 2013L, 2014L, 2014L, 2015L, 2015L, 2016L, 2016L, 
2017L, 2017L, 2018L, 2018L, 2019L, 2019L, 2020L, 2020L), CYR = c(2005L, 
2005L, 2006L, 2006L, 2007L, 2007L, 2008L, 2008L, 2009L, 2009L, 
2010L, 2010L, 2011L, 2011L, 2012L, 2012L, 2013L, 2013L, 2014L, 
2014L, 2015L, 2015L, 2016L, 2016L, 2017L, 2017L, 2018L, 2018L, 
2019L, 2019L, 2020L, 2020L), class = structure(c(1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("prop_zero", 
"prop_nonzero"), class = "factor"), proportions = c(0.170212765957447, 
0.829787234042553, 0.170212765957447, 0.829787234042553, 0.361702127659574, 
0.638297872340426, 0.234042553191489, 0.765957446808511, 0.234042553191489, 
0.765957446808511, 0.434782608695652, 0.565217391304348, 0.58695652173913, 
0.41304347826087, 0.574468085106383, 0.425531914893617, 0.51063829787234, 
0.48936170212766, 0.595744680851064, 0.404255319148936, 0.608695652173913, 
0.391304347826087, 0.51063829787234, 0.48936170212766, 0.404255319148936, 
0.595744680851064, 0.319148936170213, 0.680851063829787, 0.468085106382979, 
0.531914893617021, 0.608695652173913, 0.391304347826087)), row.names = c(NA, 
-32L), class = c("tbl_df", "tbl", "data.frame"))

ggplot(prop, aes(x = CYR, y = proportions, fill = class))  
  geom_bar(position = "fill", stat = "identity")  
  scale_fill_manual(values = c("grey70", "grey20"), labels = c("Absence", "Presence"))   
  scale_y_continuous(limits = c(0, 1.0), expand = expansion(mult = c(0, 0.05)))  
  scale_x_continuous(breaks = years, labels = ~ rep("", length(.x)))  
  # CYR labels
  annotate(
    geom = "text",
    x = prop$CYR,
    y = -Inf,
    label = prop$CYR,
    size = 6.5 / .pt,
    vjust = 2.5
  )  
  # WYR labels
  annotate(
    geom = "text",
    x = prop$CYR,
    y = -Inf,
    label = prop$WYR,
    size = 6.5 / .pt,
    vjust = 4,
    color = "grey"
  )  
  # CYR title
  annotate(
    geom = "text",
    x = -Inf,
    y = -Inf,
    label = c("CYR"),
    vjust = 2.5, hjust = 1,
    size = 6.5 / .pt
  )  
  # WYR title
  annotate(
    geom = "text",
    x = -Inf,
    y = -Inf,
    label = c("WYR"),
    vjust = 4, hjust = 1,
    size = 6.5 / .pt,
    color = "grey")  
  coord_cartesian(clip = "off")  
  theme(
    axis.text.x.bottom = element_text(margin = margin(t = 8.8, b = 8.8)),
    axis.title.x = element_blank(),
    axis.text.y = element_text(size = 10),
    axis.title.y = element_text(margin = margin(t = 0, r = 10, b = 0, l = 0), size = 14),
    axis.ticks = element_line(colour = "black", size = 1), 
    legend.title=element_blank(),
    panel.border = element_rect(fill = NA, color = "black", size = 1), 
    plot.title = element_text(hjust = 0.5))  
  labs(y = "% presence/absence")  
  ggtitle("DRY SEASONS")

enter image description here

CodePudding user response:

Found the answer here!: Flip ordering of legend without altering ordering in plot

Just add this code to the end of the ggplot: guides(fill = guide_legend(reverse = TRUE))

  • Related