简体   繁体   English

GGPLOT2 scale_x_discrete(labels = abbreviate) 十月问题

[英]GGPLOT2 scale_x_discrete(labels = abbreviate) problem with october

Recently I tried to make monthly distribution of one value as a geom bar in GGPLOT.最近,我尝试在 GGPLOT 中将一个值作为几何条进行每月分布。 I have a problem with plots labels where for october it shows on X-axis 2019-1 instead 2019-10.我对绘图标签有疑问,它在 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)

My data is a tibble:我的数据是一个小标题:

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

You should work with the dates on youe axis directly.您应该直接使用您轴上的dates Then you can use scale_x_date to further tune the axis, for instance:然后您可以使用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")

See ?scale_x_date for more options.有关更多选项,请参阅?scale_x_date

Graphs图表在此处输入图像描述 在此处输入图像描述

Data数据

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM