Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R根据学期改变ggplot背景的颜色_R - Fatal编程技术网

R根据学期改变ggplot背景的颜色

R根据学期改变ggplot背景的颜色,r,R,我有一个股市数据框,我想创建一个彩色背景的绘图。但是颜色应该以学期为基础(上半年或下半年) 您可以在这里获得数据: library(tidyverse) library(tidyquant) library(lubridate) GDAXI<-tq_get("^GDAXI", get = "stock.prices") GDAXI%>% ggplot(aes(x=date, y=close))+geom_line() 但这是行不通的。有人能帮我解决这个问题吗?你以前试过这个

我有一个股市数据框,我想创建一个彩色背景的绘图。但是颜色应该以学期为基础(上半年或下半年)

您可以在这里获得数据:

library(tidyverse)
library(tidyquant)
library(lubridate)

GDAXI<-tq_get("^GDAXI", get = "stock.prices")

GDAXI%>%
  ggplot(aes(x=date, y=close))+geom_line()

但这是行不通的。有人能帮我解决这个问题吗?

你以前试过这个吗

geom_rect(aes(xmin = date, xmax = date, ymin = -Inf, ymax = Inf, fill = semesters)
这条路是正确的。它不工作,因为xmin与xmax相同,所以没有矩形?此外,我认为geom_rect与-Inf和+Inf不兼容,这与geom_text等不同

library(lubridate)

GDAXI<-GDAXI%>%mutate(semester=semester(date))
# define y limits
LIMS = range(GDAXI$close,na.rm=T)
# we need to define the semesters and year, i.e the boxes we want to plot
GDAXI$semester_grp = semester(GDAXI$date, with_year = TRUE)

# here we create the coordinates of the rectangle
# basically x1 is start of semester
# x2 is end of semester
rectData = GDAXI %>% group_by(semester_grp) %>% 
summarize(x1=min(date),x2=max(date),y1=LIMS[1],y2=LIMS[2],semester=unique(semester))    
# we call ggplot on the two separate data frames
ggplot()+
geom_rect(data=data.frame(rectData),mapping=aes(xmin=x1,xmax=x2,ymin=y1,ymax=y2,fill=factor(semester)),alpha=0.3) + 
geom_line(data=GDAXI,aes(x=date, y=close))+
scale_fill_manual(values=c("blue","red"),name="Semester")+
theme_bw()
库(lubridate)
GDAXI%变异(学期=学期(日期))
#定义y极限
LIMS=范围(GDAXI$close,na.rm=T)
#我们需要定义学期和年份,即我们想要绘制的方框
GDAXI$学期\u grp=学期(GDAXI$日期,年=真)
#这里我们创建矩形的坐标
#基本上x1是学期的开始
#x2是学期末
rectData=GDAXI%>%groupby(学期组)%>%
汇总(x1=最小(日期),x2=最大(日期),y1=最小(日期),y2=最小(日期),学期=唯一(学期))
#我们在两个独立的数据帧上调用ggplot
ggplot()+
几何校正(数据=数据。帧(rectData),映射=aes(xmin=x1,xmax=x2,ymin=y1,ymax=y2,填充=因子(学期)),alpha=0.3+
geom_线(数据=GDAXI,aes(x=日期,y=结束))+
刻度填充手册(数值=c(“蓝色”、“红色”)、名称=“学期”)+
主题_bw()

如果您的日期值没有间隙,则此操作应该有效。。如果您正在处理其他有间隙的数据,则可以稍微调整它


我想你需要在geom rect中使用scale\u fill\u manual和alpha来获得正确的颜色:)

此外,我还找到了另一种解决方案:

GDAXI<-GDAXI%>%mutate(semester=lubridate::semester(date),
                      year=lubridate::year(date))

GDAXI<-GDAXI%>%group_by(year, semester)

table<-GDAXI%>%summarise(x1=head(date,1),
                         x2=tail(date,1),
                         y1=-Inf,
                         y2=Inf,
                         color=head(semester,1))%>%
               mutate(color=ifelse(color==1,"red","blue"))

GDAXI%>%
  ggplot()+
  geom_line(aes(x=date, y=close))+
  geom_rect(data=table, aes(xmin=x1, xmax=x2, ymin=y1, ymax=y2), fill=table$color, alpha=0.3)+
  theme_bw()
GDAXI%突变(学期=润滑::学期(日期),
年份=润滑油:年份(日期))
GDAXI%分组(年、学期)
表%汇总(x1=总目(日期1),
x2=尾部(日期,1),
y1=-Inf,
y2=Inf,
颜色=头(学期,1))%>%
变异(颜色=ifelse(颜色=1,“红色”,“蓝色”))
GDAXI%>%
ggplot()+
geom_线(aes(x=日期,y=结束))+
几何校正(数据=表格,aes(xmin=x1,xmax=x2,ymin=y1,ymax=y2),填充=表格$color,alpha=0.3)+
主题_bw()

如果您不想让灰色溢出,可以设置
panel.background=element\u blank()
然后使用
geom\u矩形
这是否回答了您的问题?没问题!另一个解决方案是什么?可能有一个软件包,我对数据时间数据lol不是很熟悉。我在下面添加了这个作为答案。是的,这是有效的,因为您按年+学期分组,这是学期的等价物(GDAXI$date,其中年=真)。那么你将不会有一个填补传奇(我想)。不要将“表”用作数据帧。。有时它会把事情搞砸,因为table()是一个函数
library(lubridate)

GDAXI<-GDAXI%>%mutate(semester=semester(date))
# define y limits
LIMS = range(GDAXI$close,na.rm=T)
# we need to define the semesters and year, i.e the boxes we want to plot
GDAXI$semester_grp = semester(GDAXI$date, with_year = TRUE)

# here we create the coordinates of the rectangle
# basically x1 is start of semester
# x2 is end of semester
rectData = GDAXI %>% group_by(semester_grp) %>% 
summarize(x1=min(date),x2=max(date),y1=LIMS[1],y2=LIMS[2],semester=unique(semester))    
# we call ggplot on the two separate data frames
ggplot()+
geom_rect(data=data.frame(rectData),mapping=aes(xmin=x1,xmax=x2,ymin=y1,ymax=y2,fill=factor(semester)),alpha=0.3) + 
geom_line(data=GDAXI,aes(x=date, y=close))+
scale_fill_manual(values=c("blue","red"),name="Semester")+
theme_bw()
GDAXI<-GDAXI%>%mutate(semester=lubridate::semester(date),
                      year=lubridate::year(date))

GDAXI<-GDAXI%>%group_by(year, semester)

table<-GDAXI%>%summarise(x1=head(date,1),
                         x2=tail(date,1),
                         y1=-Inf,
                         y2=Inf,
                         color=head(semester,1))%>%
               mutate(color=ifelse(color==1,"red","blue"))

GDAXI%>%
  ggplot()+
  geom_line(aes(x=date, y=close))+
  geom_rect(data=table, aes(xmin=x1, xmax=x2, ymin=y1, ymax=y2), fill=table$color, alpha=0.3)+
  theme_bw()