1

enter image description hereI am trying to create a graph in ggplot with percentage of time along the x axis and depth bins along the y axis. I would like to reverse the depth axis on the plot above so 0 - 10 is at the top and 90 - 100 at the bottom.

I used adapted some code from this question Simpler population pyramid in ggplot2 and used it to create my plot. However, I don't understand how I can get the depth bins axis to reverse so depth starts with 0. I have tried scale_x_reverse but my bins are categorical and I'm not sure how to adapt this properly to make my code work. Ideally I would also like to be able to add some more ticks along the y axis instead of just a single one at 10, I would prefer it to appear in integers of 5.

ggplot(data = A_pct,
       mapping = aes(x = Depth, fill = Diel, 
                     y = ifelse(test = Diel == "Day", 
                                yes = -percentage, no = percentage))) +
  geom_bar(stat = "identity") +
  labs(y = "Percent of Time") +
  scale_y_continuous(labels = abs, limits = max(A_pct$percentage) * c(-1,1))+
  coord_flip()

Data:

structure(list(X = 1:20, deployID = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = "A", class = "factor"), Diel = structure(c(1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L), .Label = c("Day", "Night"), class = "factor"), Depth = structure(c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L), .Label = c("0 - 10", "10 - 20", "20 - 30", 
"30 - 40", "40 - 50", "50 - 60", "60 - 70", "70 - 80", "80 - 90", 
"90 - 100"), class = "factor"), len = c(51L, 87L, 283L, 1816L, 
3773L, 3180L, 1325L, 246L, 49L, 15L, 87L, 300L, 1029L, 2799L, 
3643L, 1530L, 549L, 294L, 123L, 32L), percentage = c(0.24044128, 
0.410164537, 1.33421338, 8.561595399, 17.78794022, 14.99222102, 
6.246758757, 1.159775588, 0.231012211, 0.070718024, 0.410164537, 
1.414360473, 4.851256424, 13.19598322, 17.17505068, 7.213238414, 
2.588279666, 1.386073264, 0.579887794, 0.150865117)), class = "data.frame", row.names = c(NA, 
-20L))
Fionn
  • 197
  • 8

1 Answers1

1

The solution to the problem of changing the order of categorical variables in a plot axis is to change the factor levels. In the code below I change both the levels and the labels.

The secondary problem is to increase the number of axis ticks. This is done with argument breaks in scale_y_continuous.

library(ggplot2)

A_pct$Depth <- factor(A_pct$Depth, 
                      levels = rev(levels(A_pct$Depth)),
                      labels = rev(levels(A_pct$Depth)))

ggplot(data = A_pct,
       mapping = aes(x = Depth, fill = Diel, 
                     y = ifelse(test = Diel == "Day", 
                                yes = -percentage, no = percentage))) +
  geom_bar(stat = "identity") +
  labs(y = "Percent of Time") +
  scale_y_continuous(breaks = seq(-15, 15, by = 5),
                     labels = abs, 
                     limits = max(A_pct$percentage) * c(-1,1))+
  coord_flip()

enter image description here

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66