R 如何在ggplot中定义分组因子中的记号数
我有以下数据:R 如何在ggplot中定义分组因子中的记号数,r,ggplot2,tidyverse,R,Ggplot2,Tidyverse,我有以下数据: library(tidyverse) mydat <- structure(list(group = structure(1:6, .Label = c( "(0,1e+06]", "(1e+06,2e+06]", "(2e+06,3e+06]", "(3e+06,4e+06]", "(4e+06,5e+06]", "(5e+06,6e+06]"
library(tidyverse)
mydat <- structure(list(group = structure(1:6, .Label = c(
"(0,1e+06]",
"(1e+06,2e+06]", "(2e+06,3e+06]", "(3e+06,4e+06]", "(4e+06,5e+06]",
"(5e+06,6e+06]"
), class = "factor"), n = c(
1446801L, 47133L,
22577L, 14941L, 10747L, 8703L
), log_n = c(
6.16040880026537, 4.67332508334081,
4.3536662329829, 4.17437966574899, 4.031287248877, 3.93966898352233
)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))
mydat
#> # A tibble: 6 x 3
#> group n log_n
#> <fct> <int> <dbl>
#> 1 (0,1e+06] 1446801 6.16
#> 2 (1e+06,2e+06] 47133 4.67
#> 3 (2e+06,3e+06] 22577 4.35
#> 4 (3e+06,4e+06] 14941 4.17
#> 5 (4e+06,5e+06] 10747 4.03
#> 6 (5e+06,6e+06] 8703 3.94
正确的方法是什么
更新 在我尝试了@astrofunkswag方法之后。我得到了一个顺序相反的图。我想要的是维持与以前相同的秩序: 有几个问题:
只是将coord_flip
值放在垂直轴上,但它仍然是x
x
- 您的
变量是一个因子,而不是数字,因此您需要像我在上面的代码中所做的那样使用组
,或者以其他方式变换该列缩放x_离散
我从
group
中选择了3个元素,使用的pretty
与您的操作类似,但是如果您不喜欢生成的绘图,您可以得到不同的元素谢谢,但绘图是相反的。我想维持我原来的职位秩序。查看我的更新图像。
p <- mydat %>%
ggplot(aes(x = group, y = log_n)) +
geom_col() +
coord_flip() +
scale_x_discrete(limits = rev(levels(mydat$group))) +
theme_bw() +
theme(strip.background = element_blank(), strip.text = element_text(size = 12))
p
p + scale_y_continuous(breaks = pretty(mydat$group, n = 3))
p + scale_x_discrete(limits = rev(levels(mydat$group)), breaks = mydat$group[pretty(1:nrow(mydat), 3)])