由于重新排序,缩放 x 轴不起作用?

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

我正在尝试制作等级丰度曲线。这是头

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

排名从 792 到 8538,没有任何离散模式。我只是想在 x 轴和 y 轴上绘制排名,但我无法让它不仅显示所有排名,而不是分解它,我不知道是不是因为那里不是 x 轴的图案,或者因为我不得不重新排序。

x 轴是数字

这是我的代码

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))+

和对应的情节

理想情况下,x 轴不会包含所有标签。

使用 scale_x_discrete() 您可以指定要在 x 轴上放置的标签,例如

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))

问题出在 scale_x_continuous 并且 reorder 的输出是 factor。下面详细解释

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

请注意,当您按 ab 重新排序时,数据可能不会像以前那样按连续顺序排列,因此如果人们误认为 x 轴是连续的,而事实并非如此,则情节会令人困惑。

你可能想重新考虑一下你想在这里实现什么,你想通过你的图表讲述什么故事。

# 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))

reprex package (v2.0.0)

于 2021-04-28 创建