2019年9月1日日曜日

ggplot() ggplot2を使用した棒グラフ その3

!!CAUTION!!

Codes listed in this article are intended to calculate Standar Deviation or Cor. of Variation of August of each year. They work only in August as fixed length parameter such as "325" is used in many places until the day codes are updated to automatically caluculate the length of data array.

## codes begin.

ggplot 
 data.frame(data=matrix(as.vector(last(apply.monthly(SP5[,4],sd),325)[seq(1,325,12)]/last(apply.monthly(SP5[,4],mean),325)[seq(1,325,12)]),nrow  = 28),mon=as.character(seq(1992,2019,1))),aes(x=mon,y=data,fill =mon)) +
 scale_x_discrete(label=seq(1992,2019,1)) +
 stat_summary(fun.y=NULL,geom="bar") + # speciy "bar" graph
 theme(legend.position = 'none')

## codes end.

Below is updated to handle flexible number of months and years.

## codes begin

yearlen <- length(seq(1992,year(Sys.Date()),1))
monthlen <- length(index(apply.monthly(SP5[,4],sd)["1992::"]))
# put 1 to 12, which means Jan. to Dec.
startmon <- 8
# check start month is already in the past or not. if not the expected number of years
# to calculate is smaller by 1.
if(startmon <= month(last(index(apply.monthly(SP5[,4],sd)["1992::"])))){p <- 0}else{p <- 1}
df <- data.frame(
  data=as.vector(apply.monthly(SP5[,4],sd)["1992::"][seq(startmon,monthlen,12)]/apply.monthly(SP5[,4],mean)["1992::"][seq(startmon,monthlen,12)]),
  mon=as.character(seq(1992,year(Sys.Date())-p,1)))
p <- ggplot(df,aes(x=mon,y=data,fill=mon))
p <- p+geom_bar(stat = "identity")
p <- p+scale_x_discrete(label=seq(1992,year(Sys.Date()),1)) 
# p <- stat_summary(geom="bar") 
# p <- p+theme(legend.position = 'none')
plot(p)

## codes end


sample #1

## codes begin.

x <- data.frame(
   year  = 1992:2019,
   CoefficientofVariation = as.vector(last(apply.monthly(SP5[,4],sd),325)[seq(1,325,12)]/last(apply.monthly(SP5[,4],mean),325)[seq(1,325,12)])
)
g <- ggplot(x, aes(x = year, y = CoefficientofVariation,fill=year))
# stat = "identity" を忘れるとエラー: stat_count() must not be used with a y aesthetic."が出る
g <- g+geom_bar(stat = "identity")
# scale_x_discrete() doesn't work when x lables are numeric.
g <- g+scale_x_discrete(label=as.character(seq(1992,2019,1)))
# g <- g+theme(legend.position = 'none')
plot(g)

## codes end.

sample #2



## codes begin

x <- data.frame(
   year  = as.character(seq(1992,2019,1)),
   CoefficientofVariation = as.vector(last(apply.monthly(SP5[,4],sd),325)[seq(1,325,12)]/last(apply.monthly(SP5[,4],mean),325)[seq(1,325,12)])
)
g <- ggplot(x, aes(x = year, y = CoefficientofVariation,fill=year))
# stat = "identity" を忘れるとエラー: stat_count() must not be used with a y aesthetic."が出る
g <- g+geom_bar(stat = "identity")
g <- g+scale_x_discrete(label=seq(1992,2019,1)) 
g <- g+theme(legend.position = 'none')
plot(g)

## codes end

this code draw the exactly same graph as the sample#1

for start ="identify", please refer below.

By default, geom_bar uses stat="count" which makes the height of the bar proportion to the number of cases in each group (or if the weight aethetic is supplied, the sum of the weights). If you want the heights of the bars to represent values in the data, use stat="identity" and map a variable to the y aesthetic.


adding factor() to fill parameter to use distinctive colors. otherwise, graduation will be used when y parameter is numelic like "1951:2019". see sample #2.

## codes begin

x <- data.frame(
   year  = 1951:2019,
   CoefficientofVariation = as.vector(last(apply.monthly(SP5[,4],sd),817)[seq(1,817,12)]/last(apply.monthly(SP5[,4],mean),817)[seq(1,817,12)])
)
g <- ggplot(x, aes(x = year, y = CoefficientofVariation,fill=factor(year)))
# stat = "identity" を忘れるとエラー: stat_count() must not be used with a y aesthetic."が出る
g <- g+scale_x_discrete(label=seq(1950,2019,1))
g <- g+geom_bar(stat = "identity")
g <- g+theme(legend.position = 'none')
plot(g)


## codes end

sample #3




0 件のコメント: