gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 21:57:26 27 4
gpt4 key购买 nike

我有以下数据框:

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

gr 派生自 base::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-ticks 被标记为 (0,5e+05] (5e+05,1e+06] etc。我怎样才能将它简化为 5e+05 1e+06 etc... 当然这是在之后仅将 x-ticks 减少到 10。

最佳答案

看看这是不是你想要的:

# 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

关于r - 如何使用从 base::cut() 函数派生的轴减少 ggplot 的刻度数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55789198/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com