Home Articles

R ggplot - 按日期缩放x轴,但在轴上标记数据点

Asked
Viewed 594 times
0

我有一个跨时间(在x轴上)由此代码生成的折线图:

farb <- c("#5675D6", "#CE2929", "#FF8B07", "#428953", "#D0B100", "#77E599", "#A3DD57")
ggplot(data_long, aes(x=time, y=wert, group=partei, colour=partei)) +
    geom_line() +
    geom_point(shape = 20) +
    labs(y = "Wähleranteil [ % ]", x = NULL, fill = NULL) +
    theme_bw() + theme( strip.background  = element_blank(),
                        panel.grid.major = element_line(colour = "grey80"),
                        panel.border = element_blank(),
                        axis.ticks.y = element_blank(),
                        panel.grid.minor.x=element_blank(),
                        panel.grid.major.x=element_blank() ) +
    theme(legend.position="bottom") +
    scale_color_manual(values=farb)

该图表如下所示:

line chart

它根据日期进行缩放 - 并非所有数据点彼此之间的距离都相同,这就是我想要的 . 但是,我想根据 kat 列标记x轴 . 这意味着,例如,最右边的最后一个点应该有一个标记为 ZH 的刻度,但仍然根据日期进行缩放 .

轴标签应与此处类似:
axis labels

我该如何获得这样的标签?任何帮助是极大的赞赏!

这是数据:

partei               kat  wert       time
1      CVP         indexiert  0.00 2011-10-18
2      FDP         indexiert  0.00 2011-10-18
3      GPS         indexiert  0.00 2011-10-18
4      SVP         indexiert  0.00 2011-10-18
5      BDP         indexiert  0.00 2011-10-18
6       SP         indexiert  0.00 2011-10-18
7      GLP         indexiert  0.00 2011-10-18
8      CVP                FR -0.25 2012-01-28
9      FDP                FR -0.01 2012-01-28
10     GPS                FR -0.24 2012-01-28
11     SVP                FR -0.25 2012-01-28
12     BDP                FR  0.14 2012-01-28
13      SP                FR -0.23 2012-01-28
14     GLP                FR  0.10 2012-01-28
15     CVP UR\\nSZ\\nSG\\nVD -0.03 2012-05-28
16     FDP UR\\nSZ\\nSG\\nVD  0.22 2012-05-28
17     GPS UR\\nSZ\\nSG\\nVD -0.20 2012-05-28
18     SVP UR\\nSZ\\nSG\\nVD  0.00 2012-05-28
19     BDP UR\\nSZ\\nSG\\nVD  0.23 2012-05-28
20      SP UR\\nSZ\\nSG\\nVD -0.13 2012-05-28
21     GLP UR\\nSZ\\nSG\\nVD -0.18 2012-05-28
22     CVP                TG -0.25 2012-09-10
23     FDP                TG  0.21 2012-09-10
24     GPS                TG -0.23 2012-09-10
25     SVP                TG -0.02 2012-09-10
26     BDP                TG  0.17 2012-09-10
27      SP                TG  0.04 2012-09-10
28     GLP                TG -0.31 2012-09-10
29     CVP                SH -0.47 2012-11-30
30     FDP                SH  0.34 2012-11-30
31     GPS                SH -0.29 2012-11-30
32     SVP                SH -0.13 2012-11-30
33     BDP                SH  0.06 2012-11-30
34      SP                SH  0.06 2012-11-30
35     GLP                SH -0.34 2012-11-30
36     CVP                AG -0.66 2013-03-15
37     FDP                AG  0.34 2013-03-15
38     GPS                AG -0.19 2013-03-15
39     SVP                AG -0.27 2013-03-15
40     BDP                AG -0.10 2013-03-15
41      SP                AG -0.04 2013-03-15
42     GLP                AG -0.50 2013-03-15
43     CVP                BS -0.68 2013-04-25
44     FDP                BS  0.30 2013-04-25
45     GPS                BS -0.15 2013-04-25
46     SVP                BS -0.21 2013-04-25
47     BDP                BS -0.12 2013-04-25
48      SP                BS  0.13 2013-04-25
49     GLP                BS -0.64 2013-04-25
50     CVP           VS\\nSO -0.73 2013-07-15
51     FDP           VS\\nSO  0.23 2013-07-15
52     GPS           VS\\nSO -0.07 2013-07-15
53     SVP           VS\\nSO -0.16 2013-07-15
54     BDP           VS\\nSO -0.13 2013-07-15
55      SP           VS\\nSO  0.30 2013-07-15
56     GLP           VS\\nSO -0.85 2013-07-15
57     CVP                NE -0.63 2013-09-15
58     FDP                NE  0.06 2013-09-15
59     GPS                NE -0.23 2013-09-15
60     SVP                NE  0.03 2013-09-15
61     BDP                NE -0.16 2013-09-15
62      SP                NE  0.59 2013-09-15
63     GLP                NE -0.88 2013-09-15
64     CVP                GE -0.43 2013-10-15
65     FDP                GE  0.18 2013-10-15
66     GPS                GE -0.40 2013-10-15
67     SVP                GE  0.22 2013-10-15
68     BDP                GE -0.10 2013-10-15
69      SP                GE  0.87 2013-10-15
70     GLP                GE -0.86 2013-10-15
71     CVP                OW -0.32 2014-03-02
72     FDP                OW  0.39 2014-03-02
73     GPS                OW -0.46 2014-03-02
74     SVP                OW  0.37 2014-03-02
75     BDP                OW -0.07 2014-03-02
76      SP                OW  1.16 2014-03-02
77     GLP                OW -0.78 2014-03-02
78     CVP                NW -0.14 2014-05-01
79     FDP                NW  0.59 2014-05-01
80     GPS                NW -0.63 2014-05-01
81     SVP                NW  0.52 2014-05-01
82     BDP                NW -0.20 2014-05-01
83      SP                NW  1.36 2014-05-01
84     GLP                NW -0.88 2014-05-01
85     CVP                BE -0.20 2014-10-01
86     FDP                BE  0.64 2014-10-01
87     GPS                BE -0.85 2014-10-01
88     SVP                BE  0.41 2014-10-01
89     BDP                BE -0.25 2014-10-01
90      SP                BE  1.55 2014-10-01
91     GLP                BE -0.94 2014-10-01
92     CVP                GL -0.06 2014-11-20
93     FDP                GL  0.81 2014-11-20
94     GPS                GL -1.01 2014-11-20
95     SVP                GL  0.56 2014-11-20
96     BDP                GL -0.30 2014-11-20
97      SP                GL  1.37 2014-11-20
98     GLP                GL -0.95 2014-11-20
99     CVP                ZG  0.14 2015-01-10
100    FDP                ZG  1.09 2015-01-10
101    GPS                ZG -1.20 2015-01-10
102    SVP                ZG  0.57 2015-01-10
103    BDP                ZG -0.16 2015-01-10
104     SP                ZG  1.17 2015-01-10
105    GLP                ZG -1.07 2015-01-10
106    CVP                BL  0.34 2015-03-10
107    FDP                BL  1.40 2015-03-10
108    GPS                BL -1.25 2015-03-10
109    SVP                BL  0.52 2015-03-10
110    BDP                BL -0.10 2015-03-10
111     SP                BL  1.14 2015-03-10
112    GLP                BL -0.93 2015-03-10
113    CVP                LU  0.63 2015-03-24
114    FDP                LU  1.61 2015-03-24
115    GPS                LU -1.74 2015-03-24
116    SVP                LU  0.59 2015-03-24
117    BDP                LU -0.35 2015-03-24
118     SP                LU  1.26 2015-03-24
119    GLP                LU -0.39 2015-03-24
120    CVP                ZH  0.35 2015-05-01
121    FDP                ZH  1.41 2015-05-01
122    GPS                ZH -1.04 2015-05-01
123    SVP                ZH  0.17 2015-05-01
124    BDP                ZH -0.29 2015-05-01
125     SP                ZH  1.14 2015-05-01
126    GLP                ZH -0.80 2015-05-01

1 Answer

  • 1

    我不认为这是一个好的情节,但我们走了:

    data_long$time <- as.Date(data_long$time)
    
    farb <- c("#5675D6", "#CE2929", "#FF8B07", "#428953", "#D0B100", "#77E599", "#A3DD57")
    ggplot(data_long, aes(x=time, y=wert, group=partei, colour=partei)) +
      geom_line() +
      geom_point(shape = 20) +
      labs(y = "Wähleranteil [ % ]", x = NULL, fill = NULL) +
      theme_bw() + theme( strip.background  = element_blank(),
                          panel.grid.major = element_line(colour = "grey80"),
                          panel.border = element_blank(),
                          axis.ticks.y = element_blank(),
                          panel.grid.minor.x=element_blank(),
                          panel.grid.major.x=element_blank() ) +
      theme(legend.position="bottom") +
      scale_color_manual(values=farb) +
      #define the breaks and labels
      scale_x_date(breaks = unique(data_long$time),
                   labels = unique(data_long$kat))
    

    resulting plot

Related