見出し画像

ggplot 分布図 theme_dark()

概要

vix変化率、dxy変化率の分布図。色でspxの日次収益を表現している。
theme_dark()を使用。

サンプルコード

DXY <- DXY[!is.na(DXY[,4]),] # remove blank entries in DXY
m <- merge(dailyReturn(GSPC)["2011::"], dailyReturn(VIX)["2010::"][index(dailyReturn(GSPC)["2011::"])], VIX[,4][index(dailyReturn(GSPC)["2011::"])],dailyReturn(DXY)["2011::"][index(dailyReturn(GSPC)["2011::"])])
colnames(m) <- c('spx','vixdiff','vix','dxy')
df <- m
p <- ggplot(df, aes(x=vixdiff,y=dxy,color=spx))
p <- p + geom_point(alpha=1,size=1)
p <- p + scale_color_gradient(low = "red", high = "green",name = "spx")
p <- p + theme_dark()                                                    
p <- p + annotate("text", x=last(df$vixdiff),y=last(df$dxy), label = "◇",family = "HiraKakuProN-W3",alpha=1,color='red')
# p <- p + stat_smooth(aes(x=vix,y=spx),method = loess, formula = y ~ x,se=T)
plot(p)


出力例

スクリーンショット 2022-06-28 8.45.22

サンプルその2

注意事項。color に離散値を代入するときはscale_color_discrete()を使うこと。

DXY <- DXY[!is.na(DXY[,4]),] # remove blank entries in DXY
m <- merge(dailyReturn(GSPC)["2021::"], dailyReturn(VIX)["2021::"][index(dailyReturn(GSPC)["2021::"])], VIX[,4][index(dailyReturn(GSPC)["2021::"])],dailyReturn(DXY)["2021::"][index(dailyReturn(GSPC)["2021::"])])
colnames(m) <- c('spx','vixdiff','vix','dxy')
m <- data.frame(m,s=cut(m$spx,breaks=c(min(df$spx),-0.01,0,0.01,max(df$spx)),labels=c('a','b','c','d'),include.lowest = T))
df <- m
p <- ggplot(df, aes(x=vixdiff,y=dxy,color=s))
p <- p + geom_point(alpha=1,size=1)
p <- p + scale_color_discrete(name='spx daily return',label=c('less than -0.01','between -0.01 and 0','between 0 and 0.01','more than 0.01'))
p <- p + theme_dark()   
p <- p + xlab("vix daily change") + ylab("dxy daily change")                                                 
p <- p + annotate("text", x=last(df$vixdiff),y=last(df$dxy), label = "◇",family = "HiraKakuProN-W3",alpha=1,color='red')
# p <- p + stat_smooth(aes(x=vix,y=spx),method = loess, formula = y ~ x,se=T)
plot(p)
png("~/Dropbox/R-script/covid/w.png", width = 1200, height = 1000)
plot(p)
dev.off()

出力例その2


この記事が気に入ったらサポートをしてみませんか?