Skotani1
Skotani1

Reputation: 159

scaling x axis isn't working, due to reorder?

I am trying to make a rank abundaunce curve. here's the head

head(rank)

    species     ab rank
792    PEGR2 462.10  792
1126    COUM 269.70 1126
1314    KRGR 207.04 1314
1439    KRER 177.11 1439
1446    PEBR 176.00 1446
1623  CAMI12 140.71 1623

the rank goes from 792-8538 with no kind of discrete pattern. I'm just trying to plot rank on the x axis with the abo n the y axis, but I can't get it to not just show all the ranks, rather than breaking it up, I don't know if it's because there isn't a pattern to the x axis or because I had to reorder it.

the x axis is numeric

here is my code

rank.abun <- ggplot(rank ,aes(x = reorder(rank, -ab), y = ab))+
  geom_line(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))+

and the corresponding plot enter image description here

Ideally, the x axis wouldn't have all of the labels.

Upvotes: 1

Views: 42

Answers (2)

Sinh Nguyen
Sinh Nguyen

Reputation: 4497

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)

Upvotes: 2

jared_mamrot
jared_mamrot

Reputation: 26675

Using scale_x_discrete() you can specify what labels you want to put on the x axis, e.g.

library(tidyverse)

rank <- tribble(~"species", ~"ab", ~"rank",
"PEGR2", 462.10,  792,
"COUM", 269.70, 1126,
"KRGR", 207.04, 1314,
"KRER", 177.11, 1439,
"PEBR", 176.00, 1446,
"CAMI12", 140.71, 1623)

ggplot(rank, aes(x = reorder(rank, -ab), y = ab)) +
  geom_bar(stat = "identity") +
  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)) +
  scale_x_discrete(breaks = c(792, 1623),
                   labels = c(792, 1623))

example_1.png

Upvotes: 2

Related Questions