ggplot:同一月份按月份计的多年 [英] ggplot: Multiple years on same plot by month

查看:87
本文介绍了ggplot:同一月份按月份计的多年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我遇到了一些我认为没有遇到的事情.我搜寻了Google以寻找答案,但还没有找到任何东西...

So, I've hit something I don't think I have every come across. I scoured Google looking for the answer, but have not found anything (yet)...

我有两个数据集-一个用于2015年,一个用于2016年.它们表示IT系统的可用性.数据帧按以下方式读取:

I have two data sets - one for 2015 and one for 2016. They represent the availability of an IT system. The data frames read as such:

2015年数据集:

variable value
Jan 2015 100
Feb 2015 99.95
...      ...

2015年数据集:

variable value
Jan 2016 99.99
Feb 2016 99.90
...      ...

它们只是从一月到十二月列出系统的可用性. 变量"列是as.yearmon数据类型,值是一个简单的数字.

They just go from Jan - Dec listing the availability of the system. The "variable" column is a as.yearmon data type and the value is a simple numeric.

我想用ggplot2创建一个geom_line()图表,该图表基本上将百分比作为y轴,将月份作为x轴.我能够在有两条线的情况下执行此操作,但是x轴的运行时间为2015年1月-2016年12月.我想让它们仅按月绘制,因此它们可以重叠.我用秤等尝试了各种方法,但是还没有弄清楚该怎么做.

I want to create a geom_line() chart with ggplot2 that will basically have the percentages as the y-axis and the months as the x-axis. I have been able to do this where there are two lines, but the x-axis runs from Jan 2015 - Dec 2016. What I'd like is to have them only be plotted by month, so they overlap. I have tried some various things with the scales and so forth, but I have yet to figure out how to do this.

基本上,我需要x轴按时间顺序读取1月-12月,但我想在同一张图表上同时绘制2015年和2016年.这是我现在的ggplot代码(无效):

Basically, I need the x-axis to read January - December in chronological order, but I want to plot both 2015 and 2016 on the same chart. Here is my ggplot code (non-working) as I have it now:

ggplot(data2015,aes(variable,value)) +
geom_line(aes(color="2015")) +
geom_line(data=data2016,aes(color="2016")) +
scale_x_yearmon() +
theme_classic()

当我正在处理yearmon()数据类型时,它以连续流的形式绘制.我已经尝试过这样的事情:

This plots in a continuous stream as I am dealing with a yearmon() data type. I have tried something like this:

ggplot(data2015,aes(months(variable),value)) +
geom_line(aes(color="2015")) +
geom_line(data=data2016,aes(color="2016")) +
theme_classic()

显然那是行不通的.我认为months()可能仍会以某种方式保留年份.如果将它们绘制为factors(),则它们的顺序不正确.任何帮助将不胜感激.预先谢谢你!

Obviously that won't work. I figure the months() is probably still carrying the year somehow. If I plot them as factors() they are not in order. Any help would be very much appreciated. Thank you in advance!

推荐答案

要获取每年的单独行,您需要从每个日期中提取年份并将其映射为颜色.要在x轴上获取月份(没有年份),您需要从每个日期中提取月份并映射到x轴.

To get a separate line for each year, you need to extract the year from each date and map it to colour. To get months (without year) on the x-axis, you need to extract the month from each date and map to the x-axis.

library(zoo)
library(lubridate)
library(ggplot2)

让我们创建一些日期为as.yearmon格式的伪造数据.我将创建两个单独的数据框,以匹配您在问题中描述的内容:

Let's create some fake data with the dates in as.yearmon format. I'll create two separate data frames so as to match what you describe in your question:

# Fake data
set.seed(49)
dat1 = data.frame(date = seq(as.Date("2015-01-15"), as.Date("2015-12-15"), "1 month"),
                 value = cumsum(rnorm(12)))
dat1$date = as.yearmon(dat1$date)

dat2 = data.frame(date = seq(as.Date("2016-01-15"), as.Date("2016-12-15"), "1 month"),
                  value = cumsum(rnorm(12)))
dat2$date = as.yearmon(dat2$date)

现在该情节.我们将分别从lubridate包中使用yearmonth函数从date中提取年份和月份.我们还将把年份变成一个因素,以便ggplot将为年份使用分类调色板,而不是连续的颜色渐变:

Now for the plot. We'll extract the year and month from date with the year and month functions, respectively, from the lubridate package. We'll also turn the year into a factor, so that ggplot will use a categorical color palette for year, rather than a continuous color gradient:

ggplot(rbind(dat1,dat2), aes(month(date, label=TRUE, abbr=TRUE), 
                value, group=factor(year(date)), colour=factor(year(date)))) +
  geom_line() +
  geom_point() +
  labs(x="Month", colour="Year") +
  theme_classic()

这篇关于ggplot:同一月份按月份计的多年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆