scale_x_discrete not showing data some data and hi

2019-08-08 18:37发布

问题:

I want to plot a graph showing data by fiscal year. The problem I have is that ggplot is showing a point that does not exist in the data frame containing the information, and not painting another that really exists. Could you please masters of ggplot help me? Thank you very much!!

The code is:

    plot = ggplot(data = headcountPerMonth,
              aes(x = Month.Number, 
                  y = Headcount, 
                  group = Fiscal.Year, 
                  colour = Fiscal.Year)) +
       geom_line() +    
       geom_point() +  
       labs(colour = "Fiscal year") +
       scale_x_discrete(
         name = "Month", 
         labels = headcountPerMonth$Month.Name
       ) +
       scale_y_continuous(name = "Headcount");
    print(plot);

The plot is the next one. As you can see there is a value Month = March, Fiscal.Year = 2012, but not on Month = June, Fiscal.Year = 2012.

The data from the plot is the next one. As you can see there is no value Month = March, Fiscal.Year = 2012, but exists on Month = June, Fiscal.Year = 2012.

        Month Headcount Fiscal.Year Month.Number Month.Name
1  2010-04-01        48        2010            4        abr
2  2010-05-01        49        2010            5        may
3  2010-06-01        52        2010            6        jun
4  2010-07-01        51        2010            7        jul
5  2010-08-01        51        2010            8        ago
6  2010-09-01        51        2010            9        sep
7  2010-10-01        52        2010           10        oct
8  2010-11-01        54        2010           11        nov
9  2010-12-01        54        2010           12        dic
10 2011-01-01        56        2010            1        ene
11 2011-02-01        56        2010            2        feb
12 2011-03-01        61        2010            3        mar
13 2011-04-01        61        2011            4        abr
14 2011-05-01        59        2011            5        may
15 2011-06-01        59        2011            6        jun
16 2011-07-01        61        2011            7        jul
17 2011-08-01        59        2011            8        ago
18 2011-09-01        58        2011            9        sep
19 2011-10-01        59        2011           10        oct
20 2011-11-01        65        2011           11        nov
21 2011-12-01        68        2011           12        dic
22 2012-01-01        72        2011            1        ene
23 2012-02-01        72        2011            2        feb
24 2012-03-01        72        2011            3        mar
25 2012-04-01        77        2012            4        abr
26 2012-05-01        77        2012            5        may
27 2012-06-01        80        2012            6        jun
28 2012-07-01        80        2012            7        jul
29 2012-08-01        77        2012            8        ago
30 2012-09-01        81        2012            9        sep
31 2012-10-01        86        2012           10        oct
32 2012-11-01        86        2012           11        nov
33 2012-12-01        85        2012           12        dic
34 2013-01-01        88        2012            1        ene
35 2013-02-01        86        2012            2        feb

Thanks in advance for your help.

回答1:

Problem arise due to fact that variable Month.Number in your data is numeric. When you use in ggplot() values are plotted from 1:12, not from 4:12 and 1:3 as you expect.

To solve the problem you can convert Month.Number to factor with levels in expected order (it can be done also inside aes() with function factor()).

ggplot(data = headcountPerMonth,
       aes(x = factor(Month.Number,levels=c(4:12,1,2,3)), 
           y = Headcount, group = Fiscal.Year, 
           colour = as.factor(Fiscal.Year))) +
  geom_line() +    
  geom_point() +  
  labs(colour = "Fiscal year") +
  scale_y_continuous(name = "Headcount")+
  scale_x_discrete(name = "Month", labels = headcountPerMonth$Month.Name)


标签: r ggplot2