Consider the following:
考虑以下:
set.seed(1)
RandData <- rnorm(100,sd=20)
Locations <- rep(c('England','Wales'),each=50)
today <- Sys.Date()
dseq <- (seq(today, by = "1 days", length = 100))
Date <- as.POSIXct(dseq, format = "%Y-%m-%d")
Final <- cbind(Loc = Locations, Doy = as.numeric(format(Date,format = "%j")), Temp = RandData)
In this example how is it possible to produce two plots in the same figure window, where the first plot shows the temperature in England against Doy and the second shows temperature in Wales against Doy?
在这个例子中,如何在同一个数字窗口中生成两个图,其中第一个图显示英格兰对Doy的温度,第二个图显示威尔士对Doy的温度?
2 个解决方案
#1
1
Note that your data is a character matrix. Better if the Final
object is created via:
请注意,您的数据是字符矩阵。如果通过以下方式创建Final对象,则更好:
Final <- data.frame(Loc = Locations,
Doy = as.numeric(format(Date,format = "%j")),
Temp = RandData)
With that, the code below draws two plots on the one window, side by side. I use the formula interface to plot()
to make use of it's subset
argument, which works like the subset()
function.
这样,下面的代码在一个窗口上并排绘制两个图。我使用plot接口的公式接口来使用它的子集参数,它的作用类似于subset()函数。
ylab <- "Temperature"
xlab <- "Day of year"
layout(matrix(1:2, ncol = 2))
plot(Temp ~ Doy, data = Final, subset = Loc == "England", main = "England",
ylab = ylab, xlab = xlab)
plot(Temp ~ Doy, data = Final, subset = Loc == "Wales", main = "Wales",
ylab = ylab, xlab = xlab)
layout(1)
Which produces this plot:
这产生了这个情节:
If you want them both on the same scale then we modify it a bit:
如果你想要它们都在相同的比例,那么我们稍微修改它:
ylab <- "Temperature"
xlab <- "Day of year"
xlim <- with(Final, range(Doy))
ylim <- with(Final, range(Temp))
layout(matrix(1:2, ncol = 2))
plot(Temp ~ Doy, data = Final, subset = Loc == "England", main = "England",
ylab = ylab, xlab = xlab, xlim = xlim, ylim = ylim)
plot(Temp ~ Doy, data = Final, subset = Loc == "Wales", main = "Wales",
ylab = ylab, xlab = xlab, xlim = xlim, ylim = ylim)
layout(1)
which produces this version of the plot
它产生了这个版本的情节
For a line-plot you'd need to get the data in Doy
order and then add type = "l"
to the plot()
calls.
对于线图,您需要以Doy顺序获取数据,然后将type =“l”添加到plot()调用中。
For completeness, @Justin has shown how to use one of the high level plotting packages to achieve something similar but with less user-effort via ggplot2. The lattice package is another major high-level plotting package in R. You can achieve the same plot using lattice via:
为了完整起见,@ Justin已经展示了如何使用其中一个高级绘图软件包来实现类似的功能,但通过ggplot2可以减少用户的工作量。晶格封装是R中另一个主要的高级绘图包。您可以使用晶格通过以下方式实现相同的绘图:
require(lattice)
xyplot(Temp ~ Doy | Loc, data = Final, type = c("l","p")
The latter produces
后者产生
Use type = "p"
for just points and type = "l"
for just lines. As you can see, the higher-level packages make producing these plots a bit easier than with the base graphics package.
只使用type =“p”作为点,而对于行仅使用type =“l”。正如您所看到的,更高级别的包使得生成这些图比使用基本图形包更容易。
#2
0
by using cbind to create your data, they are all coerced to character
. instead use data.frame()
通过使用cbind创建数据,它们都被强制转换为字符。而是使用data.frame()
Final <- data.frame(Loc = Locations,
Doy = as.numeric(format(Date,format = "%j")),
Temp = RandData)
ggplot
does things like this very nicely.
ggplot做得非常好。
library(ggplot2)
ggplot(Final, aes(x=Doy, y=Temp)) + geom_path() + facet_wrap( ~ Loc)
Or you can use coloring:
或者你可以使用着色:
ggplot(Final, aes(x=Doy, y=Temp, color=Loc)) + geom_path()
#1
1
Note that your data is a character matrix. Better if the Final
object is created via:
请注意,您的数据是字符矩阵。如果通过以下方式创建Final对象,则更好:
Final <- data.frame(Loc = Locations,
Doy = as.numeric(format(Date,format = "%j")),
Temp = RandData)
With that, the code below draws two plots on the one window, side by side. I use the formula interface to plot()
to make use of it's subset
argument, which works like the subset()
function.
这样,下面的代码在一个窗口上并排绘制两个图。我使用plot接口的公式接口来使用它的子集参数,它的作用类似于subset()函数。
ylab <- "Temperature"
xlab <- "Day of year"
layout(matrix(1:2, ncol = 2))
plot(Temp ~ Doy, data = Final, subset = Loc == "England", main = "England",
ylab = ylab, xlab = xlab)
plot(Temp ~ Doy, data = Final, subset = Loc == "Wales", main = "Wales",
ylab = ylab, xlab = xlab)
layout(1)
Which produces this plot:
这产生了这个情节:
If you want them both on the same scale then we modify it a bit:
如果你想要它们都在相同的比例,那么我们稍微修改它:
ylab <- "Temperature"
xlab <- "Day of year"
xlim <- with(Final, range(Doy))
ylim <- with(Final, range(Temp))
layout(matrix(1:2, ncol = 2))
plot(Temp ~ Doy, data = Final, subset = Loc == "England", main = "England",
ylab = ylab, xlab = xlab, xlim = xlim, ylim = ylim)
plot(Temp ~ Doy, data = Final, subset = Loc == "Wales", main = "Wales",
ylab = ylab, xlab = xlab, xlim = xlim, ylim = ylim)
layout(1)
which produces this version of the plot
它产生了这个版本的情节
For a line-plot you'd need to get the data in Doy
order and then add type = "l"
to the plot()
calls.
对于线图,您需要以Doy顺序获取数据,然后将type =“l”添加到plot()调用中。
For completeness, @Justin has shown how to use one of the high level plotting packages to achieve something similar but with less user-effort via ggplot2. The lattice package is another major high-level plotting package in R. You can achieve the same plot using lattice via:
为了完整起见,@ Justin已经展示了如何使用其中一个高级绘图软件包来实现类似的功能,但通过ggplot2可以减少用户的工作量。晶格封装是R中另一个主要的高级绘图包。您可以使用晶格通过以下方式实现相同的绘图:
require(lattice)
xyplot(Temp ~ Doy | Loc, data = Final, type = c("l","p")
The latter produces
后者产生
Use type = "p"
for just points and type = "l"
for just lines. As you can see, the higher-level packages make producing these plots a bit easier than with the base graphics package.
只使用type =“p”作为点,而对于行仅使用type =“l”。正如您所看到的,更高级别的包使得生成这些图比使用基本图形包更容易。
#2
0
by using cbind to create your data, they are all coerced to character
. instead use data.frame()
通过使用cbind创建数据,它们都被强制转换为字符。而是使用data.frame()
Final <- data.frame(Loc = Locations,
Doy = as.numeric(format(Date,format = "%j")),
Temp = RandData)
ggplot
does things like this very nicely.
ggplot做得非常好。
library(ggplot2)
ggplot(Final, aes(x=Doy, y=Temp)) + geom_path() + facet_wrap( ~ Loc)
Or you can use coloring:
或者你可以使用着色:
ggplot(Final, aes(x=Doy, y=Temp, color=Loc)) + geom_path()