我正在尝试加载该ggplot,并且每当执行此操作时都会出现此错误:
Error in as.Date.numeric(value) : 'origin' must be supplied
这是as.Date函数所必需的唯一相关代码:
boxdates<-as.numeric(as.Date(c('1990-01-05','1992-01-05','1994-01-05','1996-01-05','1998-01-05',
'2000-01-05','2002-01-05','2004-01-05','2006-01-05','2008-01-05',
'2010-01-05','2012-01-05','2014-01-05','2016-01-05','2018-01-05')))
boxlegend<-format(as.Date(boxdates),'%Y')
这是在ggplot中使用这些变量的方式:
scale_fill_gradientn(colors=c("#66355A", "#9E0808", "#C45608", "#C4933D", "#3A8026", "#716FB3"),
breaks=boxdates, labels=boxlegend)+
scale_color_gradientn(colors=c("#66355A", "#9E0808", "#C45608", "#C4933D", "#3A8026", "#716FB3"),
breaks=boxdates, labels=boxlegend)
任何想法这些有什么问题吗?
我尝试从scale_fill_gradients中删除boxdates和boxlegend,但它仍然给出相同的错误,我也玩过变量,没有任何变化。并且boxdates和boxlegends可以很好地运行,并准确给出它们应该给的值,只是将它们放在ggplot中。
这是所有好奇的人的完整代码:
# Return Box Plots #######
# Calculate n-day rolling returns (discrete or continuous)
daily.ret <- ROC(Adj.Close, n=1, type='discrete')
three.ret <- ROC(Adj.Close, n=3, type='discrete')
five.ret <- ROC(Adj.Close, n=5, type='discrete')
ten.ret <- ROC(Adj.Close, n=10, type='discrete')
monthly.ret <- ROC(Adj.Close, n=21, type='discrete')
quarterly.ret <- ROC(Adj.Close, n=63, type='discrete')
semiannual.ret <- ROC(Adj.Close, n=126, type='discrete')
annual.ret <- ROC(Adj.Close, n=252, type='discrete')
# Merge short-term and longer-term returns xts objects and rename columns
# name the columns
short.rets <- merge(daily.ret,three.ret,five.ret,ten.ret)
names(short.rets) <- c('One','Three','Five','Ten')
long.rets <- merge(monthly.ret,quarterly.ret,semiannual.ret,annual.ret)
names(long.rets) <- c('Monthly','Quarterly','SixMonth','Annual')
# Convert to df and melt
short.rets.df <- data.frame(times=time(short.rets),short.rets)
melted.short.rets <- melt(short.rets.df, id='times')
long.rets.df <- data.frame(times=time(long.rets),long.rets)
melted.long.rets <- melt(long.rets.df, id='times')
# The following lines are for the purpose of automating y-axis labels
# Step 1 - Calculate the range
shortbox.y.range <- max(melted.short.rets$value,na.rm = T)-min(melted.short.rets$value, na.rm = T)
longbox.y.range <- max(melted.long.rets$value,na.rm = T)-min(melted.long.rets$value, na.rm = T)
# Step 2 - Use the range to determine what increment to use for y-axis breaks
shortbox.y.increment <- ifelse(shortbox.y.range>4,.5,
ifelse(shortbox.y.range>2,.2,
ifelse(shortbox.y.range>1,.1,.05)))
longbox.y.increment <- ifelse(longbox.y.range>4,.5,
ifelse(longbox.y.range>2,.2,
ifelse(longbox.y.range>1,.1,.05)))
# Step 3- Create a vector from the min rounded down, to the max rounded up,
# in increments of the calculated value (floor rounds down & ceiling rounds up)
shortbox.y.breaks <- seq(from=floor(min(melted.short.rets$value,na.rm = T)),
to=ceiling(max(melted.short.rets$value,na.rm = T)),
by=shortbox.y.increment)
longbox.y.breaks <- seq(from=floor(min(melted.long.rets$value,na.rm = T)),
to=ceiling(max(melted.long.rets$value,na.rm = T)),
by=longbox.y.increment)
# Step 4 - Multiply by 100 and add a '%' for labeling
shortbox.y.labels <- paste0(round(shortbox.y.breaks,2)*100,'%')
longbox.y.labels <- paste0(round(longbox.y.breaks,2)*100,'%')
# For the legend - first we have to specify breaks for the legend (boxdates), then we specify
# the labels (boxlegend), Jan 5 is used because of holidays and such
boxdates<-as.numeric(as.Date(c('1992-01-05','1994-01-05','1996-01-05','1998-01-05',
'2000-01-05','2002-01-05','2004-01-05','2006-01-05','2008-01-05',
'2010-01-05','2012-01-05','2014-01-05','2016-01-05','2018-01-05',"2019-01-05")))
boxlegend<-format(as.Date(boxdates),'%Y')
boxdates.annual<-as.numeric(as.Date(c('2004-01-05','2005-01-05','2006-01-05','2007-01-05',
'2008-01-05','2009-01-05','2010-01-05','2011-01-05','2012-01-05',
'2013-01-05','2014-01-05','2015-01-05','2016-01-05','2017-01-05',
'2018-01-05','2019-01-05')))
boxlegend.annual<-format(as.Date(boxdates.annual),'%Y')
# Subsetting to make top 2% and bottom 2% bigger
# these subsets work with the melted data
dailysub<- subset(melted.short.rets,subset=variable=='One')
threesub<- subset(melted.short.rets,subset=variable=='Three')
fivesub<- subset(melted.short.rets,subset=variable=='Five')
tensub<- subset(melted.short.rets,subset=variable=='Ten')
monthsub<- subset(melted.long.rets,subset=variable=='Monthly')
quartersub<- subset(melted.long.rets,subset=variable=='Quarterly')
sixmonthsub<- subset(melted.long.rets,subset=variable=='SixMonth')
annualsub<- subset(melted.long.rets,subset=variable=='Annual')
#This may be used to help with automation in the future
QB <-.99
QS <- 1-QB
QB2 <- .95
QS2 <- 1-QB2
Dot <- 7
SDot <- .3
dailysub$size<- ifelse(dailysub$value >= quantile(dailysub$value, .99,na.rm = T),3,
ifelse(dailysub$value <= quantile(dailysub$value, .01, na.rm = T),3,
ifelse(dailysub$value >= quantile(dailysub$value, .95,na.rm = T),2,
ifelse(dailysub$value <= quantile(dailysub$value, .05, na.rm = T),2,NA))))
threesub$size<- ifelse(threesub$value >= quantile(threesub$value, .99,na.rm = T),3,
ifelse(threesub$value <= quantile(threesub$value, .01, na.rm = T),3,
ifelse(threesub$value >= quantile(threesub$value, .95,na.rm = T),2,
ifelse(threesub$value <= quantile(threesub$value, .05, na.rm = T),2,NA))))
fivesub$size<- ifelse(fivesub$value >= quantile(fivesub$value, .99,na.rm = T),3,
ifelse(fivesub$value <= quantile(fivesub$value, .01, na.rm = T),3,
ifelse(fivesub$value >= quantile(fivesub$value, .95,na.rm = T),2,
ifelse(fivesub$value <= quantile(fivesub$value, .05, na.rm = T),2,NA))))
tensub$size<- ifelse(tensub$value >= quantile(tensub$value, .99,na.rm = T),3,
ifelse(tensub$value <= quantile(tensub$value, .01, na.rm = T),3,
ifelse(tensub$value >= quantile(tensub$value, .95,na.rm = T),2,
ifelse(tensub$value <= quantile(tensub$value, .05, na.rm = T),2,NA))))
monthsub$size<- ifelse(monthsub$value >= quantile(monthsub$value, .99,na.rm = T),3,
ifelse(monthsub$value <= quantile(monthsub$value, .01, na.rm = T),3,
ifelse(monthsub$value >= quantile(monthsub$value, .95,na.rm = T),2,
ifelse(monthsub$value <= quantile(monthsub$value, .05, na.rm = T),2,NA))))
quartersub$size<- ifelse(quartersub$value >= quantile(quartersub$value, .99,na.rm = T),3,
ifelse(quartersub$value <= quantile(quartersub$value, .01, na.rm = T),3,
ifelse(quartersub$value >= quantile(quartersub$value, .95,na.rm = T),2,
ifelse(quartersub$value <= quantile(quartersub$value, .05, na.rm = T),2,NA))))
sixmonthsub$size<- ifelse(sixmonthsub$value >= quantile(sixmonthsub$value, .99,na.rm = T),3,
ifelse(sixmonthsub$value <= quantile(sixmonthsub$value, .01, na.rm = T),3,
ifelse(sixmonthsub$value >= quantile(sixmonthsub$value, .95,na.rm = T),2,
ifelse(sixmonthsub$value <= quantile(sixmonthsub$value, .05, na.rm = T),2,NA))))
annualsub$size<- ifelse(annualsub$value >= quantile(annualsub$value, .99,na.rm = T),3,
ifelse(annualsub$value <= quantile(annualsub$value, .01, na.rm = T),3,
ifelse(annualsub$value >= quantile(annualsub$value, .95,na.rm = T),2,
ifelse(annualsub$value <= quantile(annualsub$value, .05, na.rm = T),2,NA))))
#we subsetted the original, and now we're putting the individual fragments back together
melted.short.rets2<-rbind(dailysub,threesub,fivesub,tensub)
melted.long.rets2<-rbind(monthsub,quartersub,sixmonthsub,annualsub)
shortbox <- ggplot(melted.short.rets2, aes(x=variable, y=value))+
geom_jitter(aes(fill=times,color=times),width=.37,alpha=.8, size=melted.short.rets2$size, shape=21)+
scale_y_continuous(breaks=shortbox.y.breaks,labels=shortbox.y.labels)+
scale_x_discrete(labels= c('1-day','3-day','5-day','10-day'))+
scale_fill_gradientn(colors=c("#66355A", "#9E0808", "#C45608", "#C4933D", "#3A8026", "#716FB3"),
breaks=boxdates, labels=boxlegend)+
scale_color_gradientn(colors=c("#66355A", "#9E0808", "#C45608", "#C4933D", "#3A8026", "#716FB3"),
breaks=boxdates, labels=boxlegend)+
stat_boxplot(geom = "errorbar", width=.1, size=1) +
geom_boxplot(outlier.shape=NA, fill='white', colour="black",size=.75)+
labs(title=paste0(asset," Return Distributions\n"),y='Return\n',x='')+
theme_bw()+
theme(plot.title=element_text(hjust=.5,size=44-max(0,nchar(asset)-6),face='bold'),
axis.text=element_text(size=22,face='bold',color='black'),
axis.title=element_text(size=32,face='bold'),
legend.text = element_text(size = 22, color = 'black', face='bold'),
legend.title = element_blank(),
panel.grid.major.y = element_line(colour = "grey40"),
panel.grid.minor.y = element_blank(),
panel.grid.major.x = element_blank(),
panel.border = element_rect(color = 'black',size = 3),
legend.key.height = unit(2.25,'cm'),
axis.ticks = element_line(size = 1),
axis.ticks.length = unit(2,'mm'),
plot.margin=unit(c(1,1,1,1),"cm"))
longbox <- ggplot(melted.long.rets2, aes(x=variable, y=value))+
geom_jitter(aes(fill=times,color=times),width=.37,alpha=.8, size=melted.long.rets2$size, shape=21)+
scale_y_continuous(breaks=longbox.y.breaks, labels=longbox.y.labels)+
scale_x_discrete(labels=c('Monthly','Quarterly','Semiannual','Annual'))+
scale_fill_gradientn(colors=c("#66355A", "#9E0808", "#C45608", "#C4933D", "#3A8026", "#716FB3"),
breaks=boxdates,labels=boxlegend)+
scale_color_gradientn(colors=c("#66355A", "#9E0808", "#C45608", "#C4933D", "#3A8026", "#716FB3"),
breaks=boxdates,labels=boxlegend)+
stat_boxplot(geom = "errorbar", width=0.1, size=1) +
geom_boxplot(outlier.colour=NA, fill='white', colour="black",size=.75)+
labs(title=paste0(asset," Return Distributions\n"),y='Return\n',x='')+
theme_bw()+
theme(plot.title=element_text(hjust=.5,size=44-max(0,nchar(asset)-6),face='bold'),
axis.text=element_text(size=22,face='bold',color='black'),
axis.title=element_text(size=32,face='bold'),
legend.text = element_text(size = 22, color = 'black', face='bold'),
legend.title = element_blank(),
panel.grid.major.y = element_line(colour = "grey40"),
panel.grid.minor.y = element_blank(),
panel.grid.major.x = element_blank(),
panel.border = element_rect(color = 'black',size = 3),
legend.key.height = unit(2.25,'cm'),
axis.ticks = element_line(size = 1),
axis.ticks.length = unit(2,'mm'),
plot.margin=unit(c(1,1,1,1),"cm"))
答案 0 :(得分:0)
更改:
boxlegend<-format(as.Date(boxdates),'%Y')
收件人:
boxlegend<-format(as.Date(boxdates, origin = "1970-01-01"),'%Y')
将解决您的来源错误。您还可以从库lubridate中引用origin
有关R和情节图的快速示例,请查看VisualisingValues