2021年5月15日土曜日

HiraKakuPro-W3 scale_fill_hue tidyr  gather 日本語 行列 matrix axis.ticks.x axis.text.x How to erase x-axis lable X軸のラベルを消すには 横並び 棒グラフ

 

  1. 都道府県別データから指定された期間の新規陽性者データを抜き出す。
  2. 抜き出したデータをベクターに変換し、都道府県の人口で割り人口あたりの新規陽性者データを算出する。
  3. 算出したデータを行列、さらにdata frame に変換する。
  4. 3. をtidyr::gather を使用してggplot 用に変換する。
  5. X軸テキスト消去の参考にしたのはこちら


len <- 90
target <- c(1,13,27,40,47)
data <- mdf
# wdf <- ( ((mdf[,-48] %>% last(.,len) %>% t() %>% as.vector()) /rep(as.vector(pref_pop[,1]) ,len) %>% matrix(.,nrow=47) %>% t() %>% data.frame() )%>% cbind(.,t=last(mdf$t,len)) ) <- BUG
# wdf <-  ((mdf[,-48] %>% last(.,len) %>% t() %>% as.vector()) /rep(as.vector(pref_pop[,1]) ,len) %>% matrix(.,nrow=47)) %>% t() %>% data.frame() %>% cbind(.,t=last(mdf$t,len))
wdf <- ((data[,-48] %>% last(.,len) %>% t() %>% as.vector()) /rep(as.vector(pref_pop[,1]) ,len) %>% matrix(.,nrow=47)) %>% t() %>% data.frame() %>% cbind(.,t=last(data$t,len))
colnames(wdf)[-48] <- pref_en
# df <- (wdf %>% tidyr::gather(reg_name,value,-t))
df <- (wdf %>% tidyr::gather(reg_name,value,target))
p <- ggplot(df,aes(x=t, y=value,fill=reg_name)) 
# p <- p +scale_fill_brewer(palette="Accent")
# p <- p+geom_bar(stat="identity",width=1,position = "fill")
p <- p+geom_bar(stat="identity",width=1,position = "dodge")
# p <- p + geom_histogram(bins=len,position = "fill", alpha = 0.9)

p <- p + theme_gray (base_family = "HiraKakuPro-W3")
p <- p + scale_fill_hue(name="都道府県",labels=pref_jp[target])
p <- p + theme(panel.background = element_rect(fill = "grey90",
                                               colour = "lightblue"))
plot(p)



wdf[,-48] %>% apply(.,2,sum)
df <- wdf[,-48] %>% apply(.,2,sum)
df <- data.frame(df)
df <- cbind(df,region=rownames(df))

p <- ggplot(df, aes(x = region, y = df, fill = region))
p <- p + geom_bar(stat = "identity")
p <- p + theme_gray (base_family = "HiraKakuPro-W3")
# p <- p + theme(axis.ticks.x=element_line(size=8))
p <- p + theme(axis.ticks.x=element_blank())
# p <- p + theme(axis.text.x=element_blank())
# p <- p + theme(axis.text.x=element_line(size=8)) # NG
p <- p + ylab("人口千人あたり") + xlab("")
p <- p + scale_fill_hue(name="都道府県",labels=pref_jp)
p <- p + scale_x_discrete(label=substr(pref_jp,1,1))
p <- p+theme(legend.position = 'none')    # legend terminate 凡例 消去
p <- p + theme(axis.text.x = element_text(angle = 90, hjust = 1))  # x軸 縦書き
plot(p)

"p <- p + theme(axis.text.x = element_text(angle = 90, hjust = 1))"でラベルを横倒し縦書きにできる。



0 件のコメント: