ggplot2时间表图表与背景阴影 [英] ggplot2 timeseries chart with background shading
问题描述
我有一个要在R中创建的excel图。
我尝试用一些虚拟数据重建它
a -rnorm(12)
a_ts< ; -ts(a,start = c(2015,1),frequency = 12)
a_time <-time(a_ts)
a_series <-ts.union(ret = a_ts,date = a_time)
a_series_df< -as.data.frame(a_series)
ggplot()+
geom_rect(data = data.frame(xmin = decimal_date(as.Date(c -01-01))),
xmax = decimal_date(as.Date(c(2015-05-31))),ymin = -Inf,ymax = Inf),
aes xs = xmin,xmax = xmax,ymin = ymin,ymax = ymax),fill =pink,alpha = 0.5)+
geom_line(data = a_series_df,aes(x = date,y = ret,color = 'blue'))+
theme(axis.text.x = element_text(angle = 90,hjust = 1,vjust = 0.5))
#这不工作
#scale_x_date =1 month,minor_breaks =1 month,labels = date_format(%B-%d))+
#scale_y_continuous(labels = scales :: percent)
我遇到的日期转换,并设置x和y起点为零,并获得轴标签右,最后两行代码适用于非日期数据点。我还想在图表下面为系列1,系列2和阴影区域输入一个图例。
任何帮助将不胜感激。
应用建议后更新:
解决方案下面是一个例子,应该让你大部分的方式。这使用
lubridate
包来处理日期和时间(在这种情况下为日期)。这显示了一种方法,你可以绘制两个单独的行在同一图上与大多数请求的修改。在此示例中,使用0.05
的alpha
。library(lubridate)
库(ggplot2)
###设置虚拟数据。
dayVec< - seq(ymd('2016-01-01'),ymd('2016-01-10'),by ='1 day')
set.seed(1234)
dayCount < - length(dayVec)
dayValVec1 < - rnorm(dayCount)
dayValVec2 < - rnorm(dayCount)
dayDFDataType = factor(c(rep('A',dayCount),rep('B',dayCount))),
Value = c(dayValVec1,dayValVec2))
###数据框中的虚拟数据(DataType是一个因素)
dayDF
##日期数据类型值
## 1 2016-01-01 A -1.20706575
## 2 2016-01-02 A 0.27742924
## 3 2016-01-03 A 1.08444118
## 4 2016-01-04 A -2.34569770
## 5 2016-01-05 A 0.42912469
## 6 2016-01-06 A 0.50605589
## 7 2016-01-07 A -0.57473996
## 8 2016-01-08 A - 0.54663186
## 9 2016-01-09 A -0.56445200
## 10 2016-01-10 A -0.89003783
## 11 2016-01-01 B -0.47719270
## 12 2016-01-02 B -0.99838644
## 13 2016-01-03 B -0.77625389
## 14 2016-01-04 B 0.06445882
## 15 2016-02 -05 B 0.95949406
## 16 2016-01-06 B -0.11028549
## 17 2016-01-07 B -0.51100951
## 18 2016-01-08 B -0.91119542
## 19 2016-01-09 B -0.83717168
## 20 2016-01-10 B 2.41583518
ggplot(dayDF,aes(Date,Value,color = DataType) )
geom_line()+
geom_rect(aes(xmin = ymd('2016-01-02'),
xmax = ymd('2016-01-06'),
ymin = -Inf,
ymax = Inf),fill ='pink',alpha = 0.05)+
scale_x_datetime(labels = date_format('%b-%d'),breaks = date_breaks '1 day'),expand = c(0,0))+
theme(axis.text.x = element_text(angle = 90),
legend.position ='bottom')
EDIT
请注意,
date_breaks
值可以更改为1个月
如果你想按月绘制。这个例子只是每天。I have an excel graph that I want to create in R.
I tried recreating it with some dummy data
a<-rnorm(12) a_ts<-ts(a, start=c(2015, 1), frequency=12) a_time<-time(a_ts) a_series<-ts.union(ret=a_ts, date=a_time) a_series_df<-as.data.frame(a_series) ggplot() + geom_rect(data=data.frame(xmin=decimal_date(as.Date(c("2015-01-01"))), xmax=decimal_date(as.Date(c("2015-05-31"))), ymin=-Inf, ymax=Inf), aes(xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax), fill="pink", alpha=0.5) + geom_line(data = a_series_df, aes(x=date,y=ret, color='blue')) + theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5)) #this does not work #scale_x_date(breaks = "1 month", minor_breaks = "1 month", labels=date_format("%B-%d")) + #scale_y_continuous(labels = scales::percent)
which looks like this
I am struggling with the date conversions and also setting the x and y origins to zero and getting the axis labels right, the last two lines of code work for non-date data points. I would also like to have a legend below the chart for series1, series2 and and entry for the shaded area.
Any help would be appreciated.
Update after applying the suggestions:
解决方案Below is an example that should get you most of the way there. This uses the
lubridate
package for working with date and times (Dates in this case). This shows you one way you could plot two separate lines on the same plot with most of the requested modifications. In this example analpha
of0.05
is used.library(lubridate) library(ggplot2) ### Set up dummy data. dayVec <- seq(ymd('2016-01-01'), ymd('2016-01-10'), by = '1 day') set.seed(1234) dayCount <- length(dayVec) dayValVec1 <- rnorm(dayCount) dayValVec2 <- rnorm(dayCount) dayDF <- data.frame(Date = rep(dayVec, 2), DataType = factor(c(rep('A', dayCount), rep('B', dayCount))), Value = c(dayValVec1, dayValVec2)) ### Dummy data in data frame (DataType is a factor) dayDF ## Date DataType Value ## 1 2016-01-01 A -1.20706575 ## 2 2016-01-02 A 0.27742924 ## 3 2016-01-03 A 1.08444118 ## 4 2016-01-04 A -2.34569770 ## 5 2016-01-05 A 0.42912469 ## 6 2016-01-06 A 0.50605589 ## 7 2016-01-07 A -0.57473996 ## 8 2016-01-08 A -0.54663186 ## 9 2016-01-09 A -0.56445200 ## 10 2016-01-10 A -0.89003783 ## 11 2016-01-01 B -0.47719270 ## 12 2016-01-02 B -0.99838644 ## 13 2016-01-03 B -0.77625389 ## 14 2016-01-04 B 0.06445882 ## 15 2016-01-05 B 0.95949406 ## 16 2016-01-06 B -0.11028549 ## 17 2016-01-07 B -0.51100951 ## 18 2016-01-08 B -0.91119542 ## 19 2016-01-09 B -0.83717168 ## 20 2016-01-10 B 2.41583518 ggplot(dayDF, aes(Date, Value, colour = DataType)) + geom_line() + geom_rect(aes(xmin=ymd('2016-01-02'), xmax = ymd('2016-01-06'), ymin = -Inf, ymax = Inf), fill = 'pink', alpha = 0.05) + scale_x_datetime(labels = date_format('%b-%d'), breaks = date_breaks('1 day'), expand=c(0,0)) + theme(axis.text.x = element_text(angle=90), legend.position = 'bottom')
EDIT
Note, the
date_breaks
value can be changed to1 month
if you want to plot by month. This example is just per day.这篇关于ggplot2时间表图表与背景阴影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!