I have a dataset that looks like this:
我有一个如下所示的数据集:
Date AE AA AEF Percent
1/1/2012 1211 1000 3556 0.03
1/2/2012 100 2000 3221 0.43
1/3/2012 3423 10000 2343 0.54
1/4/2012 10000 3000 332 0.43
1/5/2012 2342 500 4435 0.43
1/6/2012 2342 800 2342 0.23
1/7/2012 2342 1500 1231 0.12
1/8/2012 111 2300 333
1/9/2012 1231 1313 3433
1/10/2012 3453 5654 222
1/11/2012 3453 3453 454
1/12/2012 5654 7685 3452
I am trying to plot this set with ggplot, but ggplot does not plot the dates in order because they are not numeric. I am trying to convert the dates using as.Date().
我试图用ggplot绘制这个集合,但是ggplot没有按顺序绘制日期,因为它们不是数字。我正在尝试使用as.Date()转换日期。
library(ggplot2)
data <- read.csv("GCdataViz/test2.csv")
newDates <- as.Date(data$Date)
ggplot(data, aes(x = newDates, y = Percent)) +
geom_point(size = 3)
However, the date plots are not what I expected them to be. Whereas the dataset is all January data (mm/dd/yyyy), I see different months in the ggplot.
但是,日期图不是我预期的那样。虽然数据集是1月份的所有数据(mm / dd / yyyy),但我在ggplot中看到了不同的月份。
Can anyone reproduce this and diagnose the problem? Thanks.
任何人都可以复制这个并诊断问题吗?谢谢。
2 个解决方案
#1
2
Read help(as.Date)
-- you need to supply a format string as well:
阅读帮助(as.Date) - 您还需要提供格式字符串:
R> as.Date(c("1/1/2001", "1/2/2001", "1/3/2001"), "%m/%d/%Y")
[1] "2001-01-01" "2001-01-02" "2001-01-03"
R>
#2
1
ggplot does not necessarily interpret dates well. Have you tried transforming the data into a timeseries
ggplot不一定能很好地解释日期。您是否尝试将数据转换为时间序列
Something like:
就像是:
# Make data a time series, starting Jan 2009
data.ts<-ts(data, start=c(2009,1),frequency=52)
Then plot it using ggplot.
然后使用ggplot绘制它。
#1
2
Read help(as.Date)
-- you need to supply a format string as well:
阅读帮助(as.Date) - 您还需要提供格式字符串:
R> as.Date(c("1/1/2001", "1/2/2001", "1/3/2001"), "%m/%d/%Y")
[1] "2001-01-01" "2001-01-02" "2001-01-03"
R>
#2
1
ggplot does not necessarily interpret dates well. Have you tried transforming the data into a timeseries
ggplot不一定能很好地解释日期。您是否尝试将数据转换为时间序列
Something like:
就像是:
# Make data a time series, starting Jan 2009
data.ts<-ts(data, start=c(2009,1),frequency=52)
Then plot it using ggplot.
然后使用ggplot绘制它。