GGPLOT2 scale_x_discrete(labels =缩写)问题,十月

问题描述 投票:0回答:1

最近,我试图每月在GGPLOT中分配一个值作为geom条。我对地块标签有疑问,该标签在10月份的X轴显示为2019-1,而不是2019-10。

ggplot(data = temp.bk,aes(x = factor(excel_date), y = n)) +
  geom_bar(aes_string(fill = names(temp.bk)[3]),stat="identity",
           position = "dodge") +
  scale_x_discrete(labels = abbreviate)

我的数据是小标题:

excel_date group value     n percent
   <date>     <chr> <chr> <int>   <dbl>
 1 2018-11-30 710   ge1   32385  0.960 
 2 2018-11-30 710   ge2     928  0.0275
 3 2018-11-30 710   ge3     416  0.0123
 4 2018-12-31 710   ge1   32568  0.962 
 5 2018-12-31 710   ge2     880  0.0260
 6 2018-12-31 710   ge3     392  0.0116
 7 2019-01-31 710   ge1   32455  0.965 
 8 2019-01-31 710   ge2     774  0.0230
 9 2019-01-31 710   ge3     389  0.0116
10 2019-02-28 710   ge1   32525  0.967 
11 2019-02-28 710   ge2     707  0.0210
12 2019-02-28 710   ge3     391  0.0116
13 2019-03-31 710   ge1   32539  0.968 
14 2019-03-31 710   ge2     739  0.0220
15 2019-03-31 710   ge3     350  0.0104
16 2019-04-30 710   ge1   32544  0.968 
17 2019-04-30 710   ge2     676  0.0201
18 2019-04-30 710   ge3     412  0.0123
19 2019-05-31 710   ge1   32520  0.967 
20 2019-05-31 710   ge2     702  0.0209
21 2019-05-31 710   ge3     411  0.0122
22 2019-06-30 710   ge1   32517  0.967 
23 2019-06-30 710   ge2     712  0.0212
24 2019-06-30 710   ge3     406  0.0121
25 2019-07-31 710   ge1   32561  0.968 
26 2019-07-31 710   ge2     663  0.0197
27 2019-07-31 710   ge3     411  0.0122
28 2019-08-31 710   ge1   32434  0.964 
29 2019-08-31 710   ge2     791  0.0235
30 2019-08-31 710   ge3     414  0.0123
31 2019-09-30 710   ge1   32572  0.968 
32 2019-09-30 710   ge2     648  0.0193
33 2019-09-30 710   ge3     417  0.0124
34 2019-10-31 710   ge1   32627  0.970 
35 2019-10-31 710   ge2     596  0.0177
36 2019-10-31 710   ge3     422  0.0125
r ggplot2 label
1个回答
0
投票

您应直接在轴上使用dates。然后,您可以使用scale_x_date进一步调整轴,例如:

library(ggplot2)
p <- ggplot(data = temp.bk, aes(x = excel_date, y = n)) +
  geom_bar(aes_string(fill = names(temp.bk)[3]), stat = "identity",
           position = "dodge") + coord_flip()

p + scale_x_date(date_breaks = "1 month")
p + scale_x_date(date_breaks = "3 month")

请参阅?scale_x_date了解更多选项。

图形enter image description hereenter image description here

数据

temp.bk <- structure(list(excel_date = structure(c(17865, 17865, 17865, 
17896, 17896, 17896, 17927, 17927, 17927, 17955, 17955, 17955, 
17986, 17986, 17986, 18016, 18016, 18016, 18047, 18047, 18047, 
18077, 18077, 18077, 18108, 18108, 18108, 18139, 18139, 18139, 
18169, 18169, 18169, 18200, 18200, 18200), class = "Date"), 
group = c(710L,710L, 710L, 710L, 710L, 710L, 710L, 710L, 710L, 710L, 710L, 710L, 
710L, 710L, 710L, 710L, 710L, 710L, 710L, 710L, 710L, 710L, 710L, 
710L, 710L, 710L, 710L, 710L, 710L, 710L, 710L, 710L, 710L, 710L, 
710L, 710L), value = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("ge1", 
"ge2", "ge3"), class = "factor"), n = c(32385L, 928L, 416L, 32568L, 
880L, 392L, 32455L, 774L, 389L, 32525L, 707L, 391L, 32539L, 739L, 
350L, 32544L, 676L, 412L, 32520L, 702L, 411L, 32517L, 712L, 406L, 
32561L, 663L, 411L, 32434L, 791L, 414L, 32572L, 648L, 417L, 32627L, 
596L, 422L), percent = c(0.96, 0.0275, 0.0123, 0.962, 0.026, 
0.0116, 0.965, 0.023, 0.0116, 0.967, 0.021, 0.0116, 0.968, 0.022, 
0.0104, 0.968, 0.0201, 0.0123, 0.967, 0.0209, 0.0122, 0.967, 
0.0212, 0.0121, 0.968, 0.0197, 0.0122, 0.964, 0.0235, 0.0123, 
0.968, 0.0193, 0.0124, 0.97, 0.0177, 0.0125)), row.names = c(NA, 
-36L), class = "data.frame")
© www.soinside.com 2019 - 2024. All rights reserved.