ggplot2 +使用比例X的日期结构 [英] ggplot2 + Date structure using scale X

查看:535
本文介绍了ggplot2 +使用比例X的日期结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试图创建一个折线图,显示一年内几支球队的表现。我把这一年分为四个季度:1/1/2012,4/1/12。 12年8月1日。 2012年12月1日,并将csv数据框加载到R.

 月份团队职位
1 1/1 / 12南非56
2 1/1/12安哥拉85
3 1/1/12摩洛哥61
4 1/1/12佛得角群岛58
5 4/1 / 12南非71
6 4/1/12安哥拉78
7 4/1/12摩洛哥62
8 4/1/12佛得角群岛76
9 8 / 1/12南非67
10 8/1/12安哥拉85
11 8/1/1摩洛哥68
12 8/1/12佛得角群岛78
13 12 / 1/12南非87
14 12/1/12安哥拉84
15 12/1/12摩洛哥72
16 12/1/12佛得角群岛69

当我尝试使用ggplot2生成图形时,第四季度的12/1/12莫名其妙地移动到第二个位置。

  ggplot(groupA,aes(x = Month,y = Position,color = Team,group = Team))+ geom_line )

然后我把这个图表放入一个变量GA中,以便尝试使用scale_x格式化日期:

  GA + scale_x_date(labels = date_format(%m /%d))

但是我一直在收到这个错误:

 结构错误(list(call = match.call(),aesthetics = aesthetics,:

找不到函数date_format



如果我运行这段代码:

  GA + scale_x_date()

我得到这个错误:

 错误:无效的输入:date_trans只能处理类对象

我正在使用运行R 2.15.2的Mac OS X



请帮助。

df $ Month ,(假设你的 data.frame df ),它是一个因子

 > (df $ Month)
#[1]1/1/1212/1/124/1/128/1/12


$ b

解决方案是重新排序因子的水平。

<$ p $ (df $ Month,levels = df $ Month [!duplicated(df $ Month)])
> (df $ Month)
#[1]1/1/124/1/128/1/1212/1/12

p>

编辑:使用 strptime

的替代解决方案

 #您可以先将月份转换为:
df $ Month< - strptime(df $ Month,'%m /%d /%y')

然后你的代码应该可以工作。检查下面的图:




I really need help here because I am way beyond lost.

I am trying to create a line chart showing several teams' performance over a year. I divided the year into quarters: 1/1/2012, 4/1/12. 8/1/12. 12/1/12 and loaded the csv data frame into R.

     Month               Team Position
1   1/1/12       South Africa       56
2   1/1/12             Angola       85
3   1/1/12            Morocco       61
4   1/1/12 Cape Verde Islands       58
5   4/1/12       South Africa       71
6   4/1/12             Angola       78
7   4/1/12            Morocco       62
8   4/1/12 Cape Verde Islands       76
9   8/1/12       South Africa       67
10  8/1/12             Angola       85
11  8/1/12            Morocco       68
12  8/1/12 Cape Verde Islands       78
13 12/1/12       South Africa       87
14 12/1/12             Angola       84
15 12/1/12            Morocco       72
16 12/1/12 Cape Verde Islands       69

When I try using ggplot2 to generate the graph the fourth quarter 12/1/12 inexplicably moves to the second spot.

ggplot(groupA, aes(x=Month, y=Position, colour=Team, group=Team)) + geom_line()

I then put this plot into a variable GA in order to try to use scale_x to format the date:

GA + scale_x_date(labels = date_format("%m/%d"))

But I keep getting this Error:

Error in structure(list(call = match.call(), aesthetics = aesthetics,  : 

could not find function "date_format"

And if I run this code:

GA + scale_x_date()

I get this error:

Error: Invalid input: date_trans works with objects of class Date only

I am using a Mac OS X running R 2.15.2

Please help.

解决方案

Its because, df$Month, (assuming your data.frame is df), which is a factor has its levels in this order.

> levels(df$Month)
# [1] "1/1/12"  "12/1/12" "4/1/12"  "8/1/12" 

The solution is to re-order the levels of your factor.

df$Month <- factor(df$Month, levels=df$Month[!duplicated(df$Month)])
> levels(df$Month)
# [1] "1/1/12"  "4/1/12"  "8/1/12"  "12/1/12"

Edit: Alternate solution using strptime

# You could convert Month first:
 df$Month <- strptime(df$Month, '%m/%d/%y')

Then your code should work. Check the plot below:

这篇关于ggplot2 +使用比例X的日期结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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