The issue is with scale_x_continuous
& the output of reorder
is a factor
. Detail explain in detail below
library(dplyr, warn.conflicts = FALSE)
library(ggplot2)
# to understand we need to know what reorder does
# As you can see from the output below, it output a factor with Levels
with(rank, reorder(rank, -ab))
#> [1] 792 1126 1314 1439 1446 1623
#> attr(,"scores")
#> 792 1126 1314 1439 1446 1623
#> -462.10 -269.70 -207.04 -177.11 -176.00 -140.71
#> Levels: 792 1126 1314 1439 1446 1623
# And as it is a factor it will be treat as discrete value
# Therefore ggplot will display all value in the axis and
# your scale_x_continuous will causing error as it apply continuous to
# discrete value
ggplot(rank, aes(x = reorder(rank, -ab), y = ab))+
geom_bar(stat = "identity")+
scale_x_continuous(breaks=seq(792, 8538, 10))+
labs(x = "Abundance Rank", y = "Abundance") +
theme_bw()+
theme(panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
panel.border = element_blank(),
axis.line.x = element_blank(),
axis.line.y = element_blank(),
axis.text.x = element_text(angle = 90))
#> Error: Discrete value supplied to continuous scale
Note that as you reorder rank by ab, the data may not in continous order as they used to be therefore the plot would be confusing if people mistaken the x-Axis is continous while it's not.
You may want to reconsider what you want to achieve here, what is the story you want to tell through your graph.
# Another way to visualize the order is using fill to visualized the rank
graph_data <- rank %>%
# create a rank variable with negative ab value result
# in biggest value rank 1st
mutate(rank_ab = rank(-ab))
graph_data
#> species ab rank rank_ab
#> 1 PEGR2 462.10 792 1
#> 2 COUM 269.70 1126 2
#> 3 KRGR 207.04 1314 3
#> 4 KRER 177.11 1439 4
#> 5 PEBR 176.00 1446 5
#> 6 CAMI12 140.71 1623 6
ggplot(graph_data, aes(x = rank, y = ab))+
# Then here graph data with fill geom_bar
geom_bar(stat = "identity", mapping = aes(fill = rank_ab)) +
scale_x_continuous(breaks=seq(792, 8538, 100))+
labs(x = "Abundance Rank", y = "Abundance") +
theme_bw()+
theme(panel.grid.minor = element_blank(),
panel.grid.major = element_blank(),
panel.border = element_blank(),
axis.line.x = element_blank(),
axis.line.y = element_blank(),
axis.text.x = element_text(angle = 90))

Created on 2021-04-28 by the reprex package (v2.0.0)