rggplot2graphscaleitalic

Set limits for a discrete y-axis based on data and edit the y-axis labels


I am struggling to make a graph, using ggplot2. below, you can see the output I get and the relative code: enter image description here

ggplot(resu_fac_final, aes(x = reorder(species, Delta), y = Delta)) +
  geom_bar(stat = "identity",
           show.legend = FALSE,
           fill = color,     
           color = "white") +
  geom_hline(yintercept = 0, color = 1, lwd = 0.2) +
  geom_text(aes(label = round(Delta, 2), # Text with groups
                hjust = ifelse(Delta < 0, 1.5, -1),
                vjust = 0.5), size = 2.5) +
  xlab("Species") +
  ylab("Expansion and contraction of the climatic niche") +
  coord_flip() +
  theme_minimal()

I expected to have a chart that shows the expansion or contraction of the distribution of these bat species when compared at different times. However, what I got was a chart that does explain these results, but not in such a didactic way. With these ideas that I had, I hope it exemplifies the reader's understanding. I searched through various posts on this forum for something that could help me, but I didn't find it.

What I would like is to set the limits, perhaps with scale_x_discrete(), but I'm not managing it with it, in order to delimit the x-axis from 0 to 2 so that I have the values in pink on the left side and the ones in blue on the right side as in the image below. Additionally, I would like to edit the y-axis labels by removing the underscores and making them italic.

enter image description here

And this is the dput of resu_fac_final:

dput(resu_fac_final[1:10, ])
structure(list(species = c("Artibeus_cinereus", "Artibeus_concolor", 
"Artibeus_gnomus", "Artibeus_jamaicensis", "Artibeus_lituratus", 
"Artibeus_phaeotis", "Artibeus_planirostris", "Artibeus_toltecus", 
"Artibeus_watsoni", "Carollia_brevicaudum"), iniDist2 = c(98095, 
69677, 43781, 140175, 157200, 52678, 118224, 15658, 13574, 113735
), iniDist = c(111511, 71610, 61954, 129815, 132053, 50888, 127966, 
22795, 15096, 119665), Delta = c(0.87968899929155, 0.973006563329144, 
0.70666946444136, 1.07980587759504, 1.19043111477967, 1.03517528690457, 
0.923870403075817, 0.686905023031367, 0.899178590355061, 0.950444992270087
)), row.names = c(NA, 10L), class = "data.frame")

Is it possible to do somehow?

thank you in advance for every eventual help.


Solution

  • I think this does what you want, or will with some minor adjustments:

    ggplot(resu_fac_final, aes(
        ## swap x and y to get rid of coord_flip
        y = reorder(species, Delta), 
        x = Delta - 1,    ## we will subtract 1 from all x values
        fill = Delta < 1  ## set fill color dynamically based on delta
      )) +
      geom_bar(stat = "identity",
               show.legend = FALSE,
               color = "white") +
      geom_vline(xintercept = 0, color = 1, lwd = 0.2) +
      geom_text(aes(label = round(Delta, 2), # Text with groups
                    hjust = ifelse(Delta < 1, 1.5, -1), ## changed to `Delta < 1`
                    vjust = 0.5), size = 2.5) +
      labs(
        x = "Expansion and contraction of the climatic niche",
        y = "Species"
      ) +
      ## use a label function to replace underscores with spaces
      scale_y_discrete(labels = \(x) sub(pattern = "_", replacement = " ", x, fixed = TRUE)) +
      ## set the x scale to add 1 to the labels, undoing the transform above
      ## and set the limits accordingly to the transformed x values
      scale_x_continuous(labels = \(x) x + 1, limits = c(-1, 1)) +
      ## guess at your colors
      scale_fill_manual(values = scales::alpha(c("lightskyblue", "pink"), 0.8)) +
      theme_minimal() +
      theme(
        ## italicize y labels
        axis.text.y = element_text(face = "italic"),
        ## I like to get rid of horizontal grids on plots like this
        panel.grid.major.y = element_blank()
      )
    

    enter image description here