如何在同一个图上从单个文件中获取多年的Y轴数据?

时间:2022-08-21 12:48:26

I have gas meter readings over three years which I'm trying to graph in R, to highlight the changing daily usage over the months in a year, and to compare different years' usage.

我有三年的燃气表读数,我试图在R中绘图,以突出一年中每月变化的日常使用情况,并比较不同年份的使用情况。

Data sample:

Date,Gas  
02/01/2010,4460.9  
13/01/2010,4543  
04/02/2010,4656  
16/02/2010,4733  
07/03/2010,4842  
26/03/2010,4933.8  

I can calculate the average daily usage from the periodic readings, and plot the whole of the data across several years as a single data series:

我可以从周期性读数计算平均每日使用量,并将几年内的整个数据绘制为单个数据系列:

A <- read.table("energy.csv", header=TRUE, fill=TRUE, sep=',')
A$Dates <- as.Date(A$Date, format="%d/%m/%Y")
for (j in 2:length(A$Gas)) {
  A$GasDiff[j-1] = A$Gas[j] - A$Gas[j-1]
}
plot(A$Dates, A$GasDiff, type="o", lty=1, pch=20, ylab="Daily Consumption",
     main="Gas Consumption")

But I can't figure out how to get R to automatically split the data into different ?frames? for each year, so that I can plot separate lines for each year. I can manually create different input files with just the data for each year, but it's inelegant, and will need the code changed every year.

但我无法弄清楚如何让R自动将数据分成不同的帧?每年,所以我可以为每年绘制单独的行。我可以用每年的数据手动创建不同的输入文件,但它不够优雅,并且需要每年更改代码。

I'm sure it's a simple question, but I've stared at manuals, and can't figure it out.

我确定这是一个简单的问题,但我盯着手册,无法弄清楚。

2 个解决方案

#1


13  

There's no need to split the data into data-frames by year; you can use the ggplot2 package quite easily to differentiate the plots by year. First I'll make up some data:

没有必要按年将数据分成数据帧;您可以非常轻松地使用ggplot2软件包来区分年份的图表。首先,我将编制一些数据:

dts <- as.Date("20050101", '%Y%m%d') + seq(0,1000,15)
A <- data.frame( Dates = dts, Gas = 4000 + cumsum(abs( rnorm(length(dts), 100, 30))))

Next I'll add two columns to A: DayOfYear which is the "day-number" within the year, and the GasDiff column (same as yours but generated more easily, with no loops!):

接下来我将向A添加两列:DayOfYear,即年内的“day-number”和GasDiff列(与您的相同,但更容易生成,没有循环!):

A <- transform( A,               
               Year = format(Dates, '%Y'),
               DayOfYear = as.numeric( format(Dates, '%j')),
               GasDiff = c(diff( Gas ),NA))

Next we use ggplot2 to first plot all the years one after the other, but with different colors:

接下来我们使用ggplot2首先一个接一个地绘制所有年份,但使用不同的颜色:

require(ggplot2)
ggplot(A, aes(Dates, GasDiff)) + geom_line( aes(colour = Year))

which gives you this: 如何在同一个图上从单个文件中获取多年的Y轴数据?

这给你这个:

Alternatively you can plot the different years in a vertical grid:

或者,您可以在垂直网格中绘制不同年份:

ggplot(A, aes(DayOfYear, GasDiff)) + geom_line( )  + facet_grid(Year ~ .)

and you get this: 如何在同一个图上从单个文件中获取多年的Y轴数据?

你得到这个:

UPDATE: A third way is to plot all the years on the same plot with different colors/points, which may be useful if you are looking for seasonal patterns (but looks bad in my case because I made up random data).

更新:第三种方法是在不同颜色/点的相同图上绘制所有年份,如果您正在寻找季节性模式(但在我的情况下因为我编写随机数据而看起来很糟糕),这可能很有用。

ggplot(A, aes(DayOfYear, GasDiff)) + 
  geom_line( aes(colour = Year) )  + 
  geom_point( aes(shape = Year))

如何在同一个图上从单个文件中获取多年的Y轴数据?

#2


7  

Here are the plots corresponding to Prasad's ggplot examples (1) done using xyplot in lattice. (2) After that we show how to do it using xyplot.zoo from the zoo package and then (3) we show how to do each yet again using the plot.zoo which uses the zoo package's classic graphics facilities.

以下是对应于Prasad的ggplot示例(1)的图,这些示例使用格子中的xyplot完成。 (2)之后我们将展示如何使用动物园包中的xyplot.zoo进行操作,然后(3)我们将使用使用zoo包的经典图形工具的plot.zoo再次展示如何进行操作。

In each of these cases we also show a 4th style which is based on xblocks.

在每种情况下,我们还展示了基于xblocks的第4种风格。

First lets repeat Prasad's setup:

首先让我们重复Prasad的设置:

dts <- as.Date("20050101", '%Y%m%d') + seq(0,1000,15)
A <- data.frame( Dates = dts, Gas = 4000 + cumsum(abs( rnorm(length(dts), 100, 30))))

A <- transform( A,               
               Year = format(Dates, '%Y'),
               DayOfYear = as.numeric( format(Dates, '%j')),
               GasDiff = c(diff( Gas ),NA))

Now lets try using lattice

现在让我们尝试使用晶格

library(lattice) # xyplot
library(latticeExtra) # layer_, panel.xblocks
library(gridExtra) # grid.arrange
library(RColorBrewer) # brewer.pal

png("png1.png")
p1 <- xyplot(GasDiff ~ Dates, group = Year, A, type = "l",
    par.settings = list(superpose.line = list(col = 1:nlevels(A$Year))),
    auto.key = list(lines = TRUE, points = FALSE))

p2 <- xyplot(GasDiff ~ DayOfYear | Year, A, type = "l", layout = c(1, 3))

p3 <- xyplot(GasDiff ~ DayOfYear, A, group = Year, type = "l",
    auto.key = list(lines = TRUE, points = FALSE))

# and here is another style:

myPalette <- brewer.pal(nlevels(A$Year), "Set3")
p4 <- xyplot(GasDiff ~ Dates, A, type = "l", col = 1) + 
    layer_(panel.xblocks(A$Dates, myPalette[A$Year]))

grid.arrange(nrow = 2, p1, p2, p3, p4)
dev.off()

This gives these 4 plots:

这给出了这4个图:

如何在同一个图上从单个文件中获取多年的Y轴数据?

and now lets repeat this using using zoo in conjunction with the lattice and the other packages:

现在让我们使用动物园结合格子和其他包来重复这个:

png("png2.png")
library(zoo)
library(lattice)
library(latticeExtra) # layer_, panel.xblocks
library(gridExtra) # grid.arrange
library(RColorBrewer) # brewer.pal

z <- with(A, zoo(GasDiff, Dates))
year <- format(time(z), "%Y")

# split years into separate columns and plot
P1 <- xyplot(do.call("merge", split(z, year)), screen = 1, col = 1:3)

# split years into separate columns and use day.of.year as time
day.of.year <- function(x) as.numeric(format(x, "%j"))
zz <- read.zoo(A[c(1, 5, 3)], FUN = day.of.year, split = 3)
colnames(zz) <- unique(year)
P2 <- xyplot(na.approx(zz, na.rm = FALSE))

P3 <- xyplot(na.approx(zz, na.rm = FALSE), screen = 1, col = 1:3, auto.key = TRUE)

pal <- brewer.pal(nlevels(factor(year)), "Set3")
P4 <- xyplot(z, screen = 1) + layer_(panel.xblocks(time(z), pal[factor(year)]))

grid.arrange(nrow = 2, P1, P2, P3, P4)
dev.off()

Here is the output:

这是输出:

如何在同一个图上从单个文件中获取多年的Y轴数据?

A third set of ways is to use classic graphics with zoo where we use the same z, zz and pal calculated above:

第三种方法是使用动物园的经典图形,我们使用上面计算的相同的z,zz和pal:

library(zoo)
library(RColorBrewer) # brewer.pal

png("png3a.png")
plot(do.call("merge", split(z, year)), screen = 1, col = 1:3)
dev.off()
png("png3b.png")
plot(na.approx(zz, na.rm = FALSE))
dev.off()
png("png3c.png")
plot(na.approx(zz, na.rm = FALSE), screen = 1, col = 1:3)
legend("topleft", colnames(zz), lty = 1, col = 1:3, bty = "n")
dev.off()
png("png3d.png")
plot(z, type = "n")
xblocks(time(z), pal[factor(year)])
lines(z)
dev.off()

and here is the output

这是输出

如何在同一个图上从单个文件中获取多年的Y轴数据?

如何在同一个图上从单个文件中获取多年的Y轴数据?

如何在同一个图上从单个文件中获取多年的Y轴数据?

如何在同一个图上从单个文件中获取多年的Y轴数据?

#1


13  

There's no need to split the data into data-frames by year; you can use the ggplot2 package quite easily to differentiate the plots by year. First I'll make up some data:

没有必要按年将数据分成数据帧;您可以非常轻松地使用ggplot2软件包来区分年份的图表。首先,我将编制一些数据:

dts <- as.Date("20050101", '%Y%m%d') + seq(0,1000,15)
A <- data.frame( Dates = dts, Gas = 4000 + cumsum(abs( rnorm(length(dts), 100, 30))))

Next I'll add two columns to A: DayOfYear which is the "day-number" within the year, and the GasDiff column (same as yours but generated more easily, with no loops!):

接下来我将向A添加两列:DayOfYear,即年内的“day-number”和GasDiff列(与您的相同,但更容易生成,没有循环!):

A <- transform( A,               
               Year = format(Dates, '%Y'),
               DayOfYear = as.numeric( format(Dates, '%j')),
               GasDiff = c(diff( Gas ),NA))

Next we use ggplot2 to first plot all the years one after the other, but with different colors:

接下来我们使用ggplot2首先一个接一个地绘制所有年份,但使用不同的颜色:

require(ggplot2)
ggplot(A, aes(Dates, GasDiff)) + geom_line( aes(colour = Year))

which gives you this: 如何在同一个图上从单个文件中获取多年的Y轴数据?

这给你这个:

Alternatively you can plot the different years in a vertical grid:

或者,您可以在垂直网格中绘制不同年份:

ggplot(A, aes(DayOfYear, GasDiff)) + geom_line( )  + facet_grid(Year ~ .)

and you get this: 如何在同一个图上从单个文件中获取多年的Y轴数据?

你得到这个:

UPDATE: A third way is to plot all the years on the same plot with different colors/points, which may be useful if you are looking for seasonal patterns (but looks bad in my case because I made up random data).

更新:第三种方法是在不同颜色/点的相同图上绘制所有年份,如果您正在寻找季节性模式(但在我的情况下因为我编写随机数据而看起来很糟糕),这可能很有用。

ggplot(A, aes(DayOfYear, GasDiff)) + 
  geom_line( aes(colour = Year) )  + 
  geom_point( aes(shape = Year))

如何在同一个图上从单个文件中获取多年的Y轴数据?

#2


7  

Here are the plots corresponding to Prasad's ggplot examples (1) done using xyplot in lattice. (2) After that we show how to do it using xyplot.zoo from the zoo package and then (3) we show how to do each yet again using the plot.zoo which uses the zoo package's classic graphics facilities.

以下是对应于Prasad的ggplot示例(1)的图,这些示例使用格子中的xyplot完成。 (2)之后我们将展示如何使用动物园包中的xyplot.zoo进行操作,然后(3)我们将使用使用zoo包的经典图形工具的plot.zoo再次展示如何进行操作。

In each of these cases we also show a 4th style which is based on xblocks.

在每种情况下,我们还展示了基于xblocks的第4种风格。

First lets repeat Prasad's setup:

首先让我们重复Prasad的设置:

dts <- as.Date("20050101", '%Y%m%d') + seq(0,1000,15)
A <- data.frame( Dates = dts, Gas = 4000 + cumsum(abs( rnorm(length(dts), 100, 30))))

A <- transform( A,               
               Year = format(Dates, '%Y'),
               DayOfYear = as.numeric( format(Dates, '%j')),
               GasDiff = c(diff( Gas ),NA))

Now lets try using lattice

现在让我们尝试使用晶格

library(lattice) # xyplot
library(latticeExtra) # layer_, panel.xblocks
library(gridExtra) # grid.arrange
library(RColorBrewer) # brewer.pal

png("png1.png")
p1 <- xyplot(GasDiff ~ Dates, group = Year, A, type = "l",
    par.settings = list(superpose.line = list(col = 1:nlevels(A$Year))),
    auto.key = list(lines = TRUE, points = FALSE))

p2 <- xyplot(GasDiff ~ DayOfYear | Year, A, type = "l", layout = c(1, 3))

p3 <- xyplot(GasDiff ~ DayOfYear, A, group = Year, type = "l",
    auto.key = list(lines = TRUE, points = FALSE))

# and here is another style:

myPalette <- brewer.pal(nlevels(A$Year), "Set3")
p4 <- xyplot(GasDiff ~ Dates, A, type = "l", col = 1) + 
    layer_(panel.xblocks(A$Dates, myPalette[A$Year]))

grid.arrange(nrow = 2, p1, p2, p3, p4)
dev.off()

This gives these 4 plots:

这给出了这4个图:

如何在同一个图上从单个文件中获取多年的Y轴数据?

and now lets repeat this using using zoo in conjunction with the lattice and the other packages:

现在让我们使用动物园结合格子和其他包来重复这个:

png("png2.png")
library(zoo)
library(lattice)
library(latticeExtra) # layer_, panel.xblocks
library(gridExtra) # grid.arrange
library(RColorBrewer) # brewer.pal

z <- with(A, zoo(GasDiff, Dates))
year <- format(time(z), "%Y")

# split years into separate columns and plot
P1 <- xyplot(do.call("merge", split(z, year)), screen = 1, col = 1:3)

# split years into separate columns and use day.of.year as time
day.of.year <- function(x) as.numeric(format(x, "%j"))
zz <- read.zoo(A[c(1, 5, 3)], FUN = day.of.year, split = 3)
colnames(zz) <- unique(year)
P2 <- xyplot(na.approx(zz, na.rm = FALSE))

P3 <- xyplot(na.approx(zz, na.rm = FALSE), screen = 1, col = 1:3, auto.key = TRUE)

pal <- brewer.pal(nlevels(factor(year)), "Set3")
P4 <- xyplot(z, screen = 1) + layer_(panel.xblocks(time(z), pal[factor(year)]))

grid.arrange(nrow = 2, P1, P2, P3, P4)
dev.off()

Here is the output:

这是输出:

如何在同一个图上从单个文件中获取多年的Y轴数据?

A third set of ways is to use classic graphics with zoo where we use the same z, zz and pal calculated above:

第三种方法是使用动物园的经典图形,我们使用上面计算的相同的z,zz和pal:

library(zoo)
library(RColorBrewer) # brewer.pal

png("png3a.png")
plot(do.call("merge", split(z, year)), screen = 1, col = 1:3)
dev.off()
png("png3b.png")
plot(na.approx(zz, na.rm = FALSE))
dev.off()
png("png3c.png")
plot(na.approx(zz, na.rm = FALSE), screen = 1, col = 1:3)
legend("topleft", colnames(zz), lty = 1, col = 1:3, bty = "n")
dev.off()
png("png3d.png")
plot(z, type = "n")
xblocks(time(z), pal[factor(year)])
lines(z)
dev.off()

and here is the output

这是输出

如何在同一个图上从单个文件中获取多年的Y轴数据?

如何在同一个图上从单个文件中获取多年的Y轴数据?

如何在同一个图上从单个文件中获取多年的Y轴数据?

如何在同一个图上从单个文件中获取多年的Y轴数据?