2019年8月28日水曜日

ggplot() data.frame() geom="bar" ggplot2を使用した棒グラフ



library(ggplot2)
w <- c();for(i in seq(1,12,1)){w <- append(w,apply.monthly(SP5[,4],sd)[seq(i,length(index(apply.monthly(SP5[,4],sd))),12)])}
# when > month(index(last(apply.monthly(SP5[,4],sd)))) = 8. numeric sequence for months is 
# c(5,6,7,8,9,10,11,12,1,2,3,4)
ggplot(data.frame(data=apply(matrix(last(w,floor(length(w)/12)*12),nrow=12),1,mean)[c(5,6,7,8,9,10,11,12,1,2,3,4)],mon=c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec")),aes(x=mon,y=data)) +
 stat_summary(fun.y=NULL,geom="bar")


STILL WORK IN PROGRESS



  w <- c();for(i in seq(1,12,1)){w <- append(w,apply.monthly(SP5[,4],sd)[seq(i,length(index(apply.monthly(SP5[,4],sd))),12)])}
  # when > month(index(last(apply.monthly(SP5[,4],sd)))) = 8. numeric sequence for months is 
  # c(5,6,7,8,9,10,11,12,1,2,3,4)

still unable to fix x axis label order

ggplot(data.frame(data=apply(matrix(last(w,floor(length(w)/12)*12),nrow=12),1,mean)[c(5,6,7,8,9,10,11,12,1,2,3,4)],mon=c("01jan","02feb","03mar","04apr","05may","06jun","07jul","08aug","09sep","10oct","11nov","12dec")),aes(x=mon,y=data,fill =mon))+
# levels = c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"))) +
    # order = c(5,6,7,8,9,10,11,12,1,2,3,4))) +
   stat_summary(fun.y=NULL,geom="bar")



library(ggplot2)
# ignore a next line complete nonsense.
# w <- c();for(i in seq(1,12,1)){w <-append(w,apply.monthly(SP5[,4],sd)[seq(i,length(index(apply.monthly(SP5[,4],sd))),12)])}
w <- apply.monthly(SP5[,4],sd)
  # when > month(index(last(apply.monthly(SP5[,4],sd)))) = 8. numeric sequence for months is   
  # c(5,6,7,8,9,10,11,12,1,2,3,4)
  # or execute c(last(seq(1,12,1),month(index(last(apply.monthly(SP5[,4],sd)))) ),head(seq(1,12,1),12-month(index(last(apply.monthly(SP5[,4],sd)))) ))

ggplot(data.frame(data=apply(matrix(last(w,floor(length(w)/12)*12),nrow=12),1,mean)[c(5,6,7,8,9,10,11,12,1,2,3,4)],mon=c("01jan","02feb","03mar","04apr","05may","06jun","07jul","08aug","09sep","10oct","11nov","12dec")),aes(x=mon,y=data,fill =mon))+
# levels = c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"))) +
    # order = c(5,6,7,8,9,10,11,12,1,2,3,4))) +
scale_x_discrete(label=c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"))+
stat_summary(fun.y=NULL,geom="bar")+
theme(legend.position = 'none')  # erase legend


# the final version
w <- apply.monthly(SP5[,4],sd)
# when > month(index(last(apply.monthly(SP5[,4],sd)))) = 8. numeric sequence for months is   
# c(5,6,7,8,9,10,11,12,1,2,3,4)
# or execute c(last(seq(1,12,1),month(index(last(apply.monthly(SP5[,4],sd)))) ),head(seq(1,12,1),12-month(index(last(apply.monthly(SP5[,4],sd)))) ))
#
df <- data.frame(data=apply(matrix(last(w,floor(length(w)/12)*12),nrow=12),1,mean)[     c(last(seq(1,12,1),month(index(last(apply.monthly(SP5[,4],sd)))) ),head(seq(1,12,1),12-month(index(last(apply.monthly(SP5[,4],sd)))) ))],mon=factor(seq(1,12,1)))
p <- ggplot(df, aes(x=mon,y=data,fill =mon)) 
p <- p + scale_x_discrete(label=c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"))  #x-axis label
p <- p + geom_bar(stat = "identity") # need identity to draw value itself. 
p <- p + theme(legend.position = 'none')  # erase legend

plot(p)



なお、

>month(index(last(apply.monthly(SP5[,4],sd))))
[1] 8

c(last(seq(1,12,1),month(index(last(apply.monthly(SP5[,4],sd)))) ),head(seq(1,12,1),12-month(index(last(apply.monthly(SP5[,4],sd)))) ))

で、「5  6  7  8  9 10 11 12  1  2  3  4」を生成できる。

0 件のコメント: