【问题标题】:Why does this code plot by day of the week?为什么这段代码按星期几绘制?
【发布时间】:2017-12-28 00:45:40
【问题描述】:

链接到数据集,该数据集是日期和时间列以及用电量列 https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip

 power1 <- read.csv(file = "c:/datasets/household_power_consumption.txt", stringsAsFactors=F, header = TRUE,
               sep=";", dec = ".", na.strings="?", col.names = c("date1","time1","Global_active_power", "Global_reactive_power",
                                                                 "Voltage","Global_intensity","Sub_metering_1","Sub_metering_2",
                                                                 "Sub_metering_3")) 

power1$date1 <- as.Date(power1$date1, format="%d/%m/%Y")

power2 <- subset(power1, subset=(date1  >= "2007-02-01" & date1 <= "2007-02-02"))

datetime1 <- paste(as.Date(power2$date1), power2$time1)

power2$Datetime <- as.POSIXct(datetime1)

plot(power2$Global_active_power~power2$Datetime, type="l", ylab="Global Active Power (kilowatts)", xlab="")

当我运行上述内容时,我得到的图表就像我应该在 x 轴上显示一周中的几天一样,即使我运行 summary、head 和 str() 我在数据中看不到任何关于一周中的几天。

我尝试使用 mutate 添加我自己的 day 列,但没有成功。

当我像下面这样子集它时它不起作用。它在我只有我需要的数据的地方正确子集,但它不会与 date1 列或我通过 mutate 创建的星期几列一起绘制

power2 <- subset(power1, subset=(as.Date(date1,  format = "%d/%m/%Y") >= "2007-02-01" 
                             & as.Date(date1,  format = "%d/%m/%Y") <= "2007-02-02"))

我知道 as.Posixct 将在其中包含所有元数据,但我不明白为什么当我将日期和时间列合并到它自己的列中时,它才按星期几绘制 graph不用我问。

当我这样运行时,合并的日期和时间列数据因错误的年份而损坏

power11 <- read.csv(file = "c:/datasets/household_power_consumption.txt", stringsAsFactors=F, header = TRUE,
                sep=";", dec = ".", col.names = c("date1","time1","Global_active_power", "Global_reactive_power",
                                       "Voltage","Global_intensity","Sub_metering_1","Sub_metering_2",
                                       "Sub_metering_3")) 
                #colClasses = c("Date", "character", "factor", "numeric","numeric","numeric","numeric","numeric","numeric"))
power22 <- subset(power11, subset=(as.Date(date1,  format = "%d/%m/%Y") >= "2007-02-01" 
                             & as.Date(date1,  format = "%d/%m/%Y") <= "2007-02-02"))
datetime1 <- paste(as.Date(power22$date1), power22$time1)
power22$Datetime <- as.POSIXct(datetime1)

【问题讨论】:

    标签: r plot


    【解决方案1】:

    也许这个链接会有所帮助: http://earlh.com/blog/2009/07/07/plotting-with-custom-x-axis-labels-in-r-part-5-in-a-series/

    在你的 plot() 调用中添加一个参数:xaxt='n'

    plot(power2$Global_active_power~power2$Datetime, type="l", ylab="Global Active Power (kilowatts)", xlab="", xaxt='n') 
    

    告诉 plot 不要添加 x 轴标签。然后添加一个axis()调用:

    axis(side=1, at=power22$Datetime, labels=format(power22$Datetime, '%b-%y'))
    

    我在这里使用了“%b-%y”,因为这是我在我引用的网站上看到的,但您可能希望使用适合您需要的格式代码。

    【讨论】:

    • 谢谢,但我一直在寻找它为何如此运作的原因。如果我按照这个确切的顺序运行第一批代码,我会得到我需要的答案。但是,如果我以不同的顺序运行它,例如导入文件、子集并在 date1 和 time1 仍然是字符列时创建新列,则新的 datetime1 列已损坏年份或 0001 而不是 2007。如果我将 date1 更改为日期然后创建列然后就可以了。即使我使用 as.POSIXct 和 as.Date 运行绘图,它也不起作用。仅当在绘图运行之前更改日期。
    猜你喜欢
    • 2017-06-08
    • 2018-07-20
    • 2018-10-18
    • 2020-11-01
    • 1970-01-01
    • 2013-09-04
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多