scale_x_discrete不显示数据某些数据并隐藏另一个数据 [英] scale_x_discrete not showing data some data and hiding another

查看:2036
本文介绍了scale_x_discrete不显示数据某些数据并隐藏另一个数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一个按财年显示数据的图表。我遇到的问题是, ggplot显示的数据框中不存在包含该信息的点,而不是绘制另一个真正存在的点。你能请ggplot的主人帮助我吗?非常感谢!



代码是:

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

剧情是下一个。您可以看到值为Month = March,Fiscal.Year = 2012,但不在Month = June,Fiscal.Year = 2012





数据从情节是下一个。正如你可以看到没有值Month = March,Fiscal.Year = 2012,但存在Month = June,Fiscal.Year = 2012

 月份总人数Fiscal.Year Month.Number Month.Name 
1 2010-04-01 48 2010 4 abr
2 2010-05-01 49 2010 5可能
3 2010-06-01 52 2010 6 jun
4 2010-07-01 51 2010 7 jul
5 2010-08-01 51 2010 8前
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
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
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
2012-06-01 80 2012 6 jun
28 2012-07-01 80 2012 7 jul
29 2012-08-01 77 2012 8前
30 2012-09-01 81 2012 9 sep
31 2012-10-01 86 2012 10 oct
32 2012-11-01 86 2012 11 nov
2012-12-01 85 2012 12 dic
34 2013-01-01 88 2012 1 ene
35 2013-02-01 86 2012 2 feb

预先感谢您的帮助。

解决方案

由于数据中的变量 Month.Number 是数字,所以出现问题。在中使用时,ggplot()的值从 1:12 绘制,而不是从 4 :12 1:3 如你所料。

可以将 Month.Number 转换为预期顺序的级别因子(也可以在 aes() factor())。

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


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.

解决方案

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)

这篇关于scale_x_discrete不显示数据某些数据并隐藏另一个数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆