R 让ggplot facet_栅格填充颜色基于变量/列获取多个值

R 让ggplot facet_栅格填充颜色基于变量/列获取多个值,r,ggplot2,R,Ggplot2,正在寻找调整背景颜色的方法facet\u grid。在下面ggplot.MnWE的panel.background=element_rect(fill=选项)中找不到使用变量/列的示例,这表明了我要追求的理念,其中7月和8月网格后面应该没有灰色面板,但6月(以及所有其他月份)应该。不寻找使用facet\u grid中的drop=TRUE选项的解决方案 library(tidyquant) library(plyr) library(plotly) amznStock = as.data.fr

正在寻找调整背景颜色的方法
facet\u grid
。在下面ggplot.MnWE的
panel.background=element_rect(fill=
选项)中找不到使用变量/列的示例,这表明了我要追求的理念,其中7月和8月网格后面应该没有灰色面板,但6月(以及所有其他月份)应该。不寻找使用
facet\u grid
中的
drop=TRUE
选项的解决方案

library(tidyquant) 
library(plyr)
library(plotly)

amznStock = as.data.frame(tidyquant::tq_get(c("AMZN"),get="stock.prices")) # get data using tidyquant 
amznStock = amznStock[year(amznStock$date) > 2012, ] # Using data only after 2012Using ggplot2
amznStock$weekday = as.POSIXlt(amznStock$date)$wday #finding the day no. of the week
amznStock$weekdayf<-factor(amznStock$weekday,levels=rev(1:7),labels=rev(c("Mon","Tue","Wed","Thu","Fri","Sat","Sun")),ordered=TRUE) #converting the day no. to factor 
amznStock$monthf<-factor(month(amznStock$date),levels=as.character(1:12),labels=c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"),ordered=TRUE) # finding the month 
amznStock$yearmonth<- factor(as.yearmon(amznStock$date)) #finding the year and the month from the date. Eg: Nov 2018 
amznStock$week <- as.numeric(format(amznStock$date,"%W")) #finding the week of the year for each date 
amznStock<-ddply(amznStock,.(yearmonth),transform,monthweek=1+week-min(week)) #normalizing the week to start at 1 for every month 

#pretend we are missing data for june
amznStock<-amznStock[amznStock$monthf!="Jun",]

#pretend there are no data for jul or aug
amznStock<-amznStock[amznStock$monthf!="Jul",]
amznStock<-amznStock[amznStock$monthf!="Aug",]


p <- ggplot(amznStock, aes(monthweek, weekdayf, fill = amznStock$adjusted)) + geom_tile(colour = "white") + facet_grid(year(amznStock$date)~monthf, drop=FALSE) + scale_fill_gradient(low="red", high="green") + xlab("Week of Month") + ylab("") + ggtitle("Time-Series Calendar Heatmap: AMZN Stock Prices") + labs(fill = "Price") 
p

#set intended grid coloring
x2$background<-"light grey"
#turn off facet grid color when (pretending) there are no data for july or aug
x2$background[x2$monthf=="Jul"]<-"white"
x2$background[x2$monthf=="Aug"]<-"white"

p2<- p + 
  theme(
    panel.background = element_rect(fill = x2$background),
  )
p2
库(tidyquant)
图书馆(plyr)
图书馆(绘本)
amznStock=as.data.frame(tidyquant::tq_get(c(“AMZN”),get=“stock.prices”)#使用tidyquant获取数据
amznStock=amznStock[年份(amznStock$日期)>2012年]#仅在2012年之后使用数据使用ggplot2
amznStock$weekday=as.POSIXlt(amznStock$date)$wday#查找一周中的天数

amznStock$weekdayfA几件事:在您的代码中,您加载了这里似乎没有使用的库(没有看到
plotly
).
plyr
已经被弃用了一段时间,取而代之的是其他tidyverse软件包,主要是
dplyr
。请看下面的一小部分:这里只有几行内容涉及到您的实际问题,这是关于绘图的。如果您只给我们这些数据的一个示例,我们不需要下载并执行您在这里得到的所有操作更容易调试您可以在这个类似的问题中尝试这种方法:这里使用的策略类似,都在每个方面的背景中使用几何体,而不是方面面板格式本身。感谢所有编写了一些有用内容的人!有几件事:在您的代码中,您加载了这里似乎没有使用的库(没有看到任何使用
的情节
).
plyr
已经被弃用了一段时间,取而代之的是其他tidyverse软件包,主要是
dplyr
。请看下面的一小部分:这里只有几行内容涉及到您的实际问题,这是关于绘图的。如果您只给我们这些数据的一个示例,我们不需要下载并执行您在这里得到的所有操作更容易调试您可以在这个类似的问题中尝试这种方法:这里使用的类似策略,都在每个方面的背景中使用几何体,而不是方面面板格式本身。感谢所有编写了一些有用内容的人!