使用GGPLOT2在同一图上使用GGPLOT2绘制两个变量
一个非常新的问题,但是说我有这样的数据:
test_data <-
data.frame(
var0 = 100 + c(0, cumsum(runif(49, -20, 20))),
var1 = 150 + c(0, cumsum(runif(49, -10, 10))),
date = seq(as.Date("2002-01-01"), by="1 month", length.out=100)
)
我如何在同一图上同时绘制时间序列var0
var1 var1 ,date
在X轴上,使用ggplot2
?如果您制作var0
和var1
不同的颜色,则可以包括传奇!
我确定这很简单,但是我找不到任何示例。
A very newbish question, but say I have data like this:
test_data <-
data.frame(
var0 = 100 + c(0, cumsum(runif(49, -20, 20))),
var1 = 150 + c(0, cumsum(runif(49, -10, 10))),
date = seq(as.Date("2002-01-01"), by="1 month", length.out=100)
)
How can I plot both time series var0
and var1
on the same graph, with date
on the x-axis, using ggplot2
? Bonus points if you make var0
and var1
different colours, and can include a legend!
I'm sure this is very simple, but I can't find any examples out there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于少数变量,您可以自己手动构建情节:
For a small number of variables, you can build the plot manually yourself:
一般方法是将数据转换为长格式(使用
Melt()
来自packagereshape
或reshape2
)或gather()
/pivot_longer()
来自tidyr
软件包:另请参阅这个问题 。
The general approach is to convert the data to long format (using
melt()
from packagereshape
orreshape2
) orgather()
/pivot_longer()
from thetidyr
package:Also see this question on reshaping data from wide to long.
对于GGPLOT2,您需要以“高”格式而不是“宽”数据。 “宽”是指每个变量作为不同的列(就像您现在的列一样)。您需要将其转换为“高个子”格式,其中有一个列,该列告诉您变量的名称和另一列,该列告诉您变量的值。从宽到高的过程通常称为“熔化”。您可以使用
tidyr ::收集
熔化数据框架:为了清楚
data
ggplot
正在消耗通过收集
将其输送后,看起来像这样:You need the data to be in "tall" format instead of "wide" for ggplot2. "wide" means having an observation per row with each variable as a different column (like you have now). You need to convert it to a "tall" format where you have a column that tells you the name of the variable and another column that tells you the value of the variable. The process of passing from wide to tall is usually called "melting". You can use
tidyr::gather
to melt your data frame:Just to be clear the
data
thatggplot
is consuming after piping it viagather
looks like this:continue
I am also new to R but trying to understand how ggplot works I think I get another way to do it. I just share probably not as a complete perfect solution but to add some different points of view.
I know ggplot is made to work with dataframes better but maybe it can be also sometimes useful to know that you can directly plot two vectors without using a dataframe.
Loading data. Original date vector length is 100 while var0 and var1 have length 50 so I only plot the available data (first 50 dates).
Plotting
However I was not able to add a correct legend using this format. Does anyone know how?
使用您的数据:
我创建一个堆叠版本,这是
ggplot()
想要使用的内容:在这种情况下,生产
堆叠
非常容易,因为我们只需要做一个几个操作,但是reshape()
和reshape
和reshape2
,如果您有一个更复杂的真实数据集可以操纵。一旦数据以此堆叠的形式,它只需要一个简单的
ggplot()
调用来制作您想要的所有附加功能的绘图(一个原因是lattice
lattice <的高级绘图包的原因之一/code>和
ggplot2
非常有用):我将其留给您以整理轴标签,传奇标题等
。
Using your data:
I create a stacked version which is what
ggplot()
would like to work with:In this case producing
stacked
was quite easy as we only had to do a couple of manipulations, butreshape()
and thereshape
andreshape2
might be useful if you have a more complex real data set to manipulate.Once the data are in this stacked form, it only requires a simple
ggplot()
call to produce the plot you wanted with all the extras (one reason why higher-level plotting packages likelattice
andggplot2
are so useful):I'll leave it to you to tidy up the axis labels, legend title etc.
HTH