如何使用从base :: cut()函数派生的轴减少ggplot的刻度数

时间:2019-04-22 04:40:45

标签: r ggplot2

我有以下数据框:

library(tidyverse)
dat <- structure(list(gr = structure(1:68, .Label = c("(0,5e+05]", "(5e+05,1e+06]", 
"(1e+06,1.5e+06]", "(1.5e+06,2e+06]", "(2e+06,2.5e+06]", "(2.5e+06,3e+06]", 
"(3e+06,3.5e+06]", "(3.5e+06,4e+06]", "(4e+06,4.5e+06]", "(4.5e+06,5e+06]", 
"(5e+06,5.5e+06]", "(5.5e+06,6e+06]", "(6e+06,6.5e+06]", "(6.5e+06,7e+06]", 
"(7e+06,7.5e+06]", "(7.5e+06,8e+06]", "(8e+06,8.5e+06]", "(8.5e+06,9e+06]", 
"(9e+06,9.5e+06]", "(9.5e+06,1e+07]", "(1e+07,1.05e+07]", "(1.05e+07,1.1e+07]", 
"(1.1e+07,1.15e+07]", "(1.15e+07,1.2e+07]", "(1.2e+07,1.25e+07]", 
"(1.25e+07,1.3e+07]", "(1.3e+07,1.35e+07]", "(1.35e+07,1.4e+07]", 
"(1.4e+07,1.45e+07]", "(1.45e+07,1.5e+07]", "(1.5e+07,1.55e+07]", 
"(1.55e+07,1.6e+07]", "(1.6e+07,1.65e+07]", "(1.65e+07,1.7e+07]", 
"(1.7e+07,1.75e+07]", "(1.75e+07,1.8e+07]", "(1.8e+07,1.85e+07]", 
"(1.85e+07,1.9e+07]", "(1.9e+07,1.95e+07]", "(1.95e+07,2e+07]", 
"(2e+07,2.05e+07]", "(2.05e+07,2.1e+07]", "(2.1e+07,2.15e+07]", 
"(2.15e+07,2.2e+07]", "(2.2e+07,2.25e+07]", "(2.25e+07,2.3e+07]", 
"(2.3e+07,2.35e+07]", "(2.35e+07,2.4e+07]", "(2.4e+07,2.45e+07]", 
"(2.45e+07,2.5e+07]", "(2.5e+07,2.55e+07]", "(2.55e+07,2.6e+07]", 
"(2.6e+07,2.65e+07]", "(2.65e+07,2.7e+07]", "(2.7e+07,2.75e+07]", 
"(2.75e+07,2.8e+07]", "(2.8e+07,2.85e+07]", "(2.85e+07,2.9e+07]", 
"(2.9e+07,2.95e+07]", "(2.95e+07,3e+07]", "(3e+07,3.05e+07]", 
"(3.05e+07,3.1e+07]", "(3.1e+07,3.15e+07]", "(3.15e+07,3.2e+07]", 
"(3.2e+07,3.25e+07]", "(3.25e+07,3.3e+07]", "(3.3e+07,3.35e+07]", 
"(3.35e+07,3.4e+07]"), class = "factor"), n = c(434543, 3219, 
867, 375, 217, 189, 131, 111, 125, 103, 53, 69, 71, 81, 61, 43, 
47, 49, 43, 45, 33, 45, 27, 51, 15, 19, 33, 23, 19, 29, 23, 19, 
15, 25, 27, 37, 17, 21, 21, 19, 11, 9, 13, 15, 13, 19, 9, 13, 
9, 9, 5, 3, 11, 13, 3, 13, 7, 3, 5, 7, 5, 5, 3, 1, 3, 5, 3, 1
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-68L))

dat
#> # A tibble: 68 x 2
#>    gr                   n
#>    <fct>            <dbl>
#>  1 (0,5e+05]       434543
#>  2 (5e+05,1e+06]     3219
#>  3 (1e+06,1.5e+06]    867
#>  4 (1.5e+06,2e+06]    375
#>  5 (2e+06,2.5e+06]    217
#>  6 (2.5e+06,3e+06]    189
#>  7 (3e+06,3.5e+06]    131
#>  8 (3.5e+06,4e+06]    111
#>  9 (4e+06,4.5e+06]    125
#> 10 (4.5e+06,5e+06]    103
#> # … with 58 more rows

grbase::cut()函数派生。 使用此代码:

ggplot(dat, aes(x = gr, y = n)) +
  geom_point() +
  # geom_line() +
  scale_y_continuous(trans = "log10") +
  theme_bw() +
  theme(legend.title = element_blank()) + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))

我可以创建该图:

enter image description here

注意到x轴上有68个刻度,每个点之间的间隔为5e5。我的问题是:

  1. 如何将刻度线减少到仅10个刻度线?但是仍然保持图中的点。

  2. 当前,x标记标记为(0,5e+05] (5e+05,1e+06] etc。我如何将其简化为5e+05 1e+06 etc...当然是 将x-ticks减少到10个。

1 个答案:

答案 0 :(得分:1)

看看这是否是您想要的:

# simplify labels
levels(dat$gr) <- gsub("\\(.+,|]", "", levels(dat$gr))

ggplot(dat, aes(x = gr, y = n)) +
  geom_point() +

  # specify 10 breaks of approximately equal size for x axis
  scale_x_discrete(breaks = levels(dat$gr)[floor(seq(1, 
                                                     nlevels(dat$gr), 
                                                     length.out = 10))]) +

  scale_y_continuous(trans = "log10") +
  theme_bw() +
  theme(legend.title = element_blank(),
        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))

plot