library(ggplot2)
df <- read.csv("C:/~略~/newly_confirmed_cases_detail_weekly_old.csv",
# 1 行目は無視し、値が "*" のデータは NA 扱いとする
skip=1, na.strings="*")
male <- c()
# 2020年代の男性だけ抜き出す
for (i in 1:18) {
male <- rbind(male,
c(df$Week[i], "M.Under.10", df$Male.Under.10[i]),
c(df$Week[i], "M.10s", df$Male.10s[i]),
c(df$Week[i], "M.20s", df$Male.20s[i]),
c(df$Week[i], "M.30s", df$Male.30s[i]),
c(df$Week[i], "M.40s", df$Male.40s[i]),
c(df$Week[i], "M.50s", df$Male.50s[i]),
c(df$Week[i], "M.60s", df$Male.60s[i]),
c(df$Week[i], "M.70s", df$Male.70s[i]),
c(df$Week[i], "M.80s", df$Male.80s[i]),
c(df$Week[i], "M.Over.90", df$Male.Over.90[i]))
}
df_male <- data.frame(Week=factor(male[,1], levels=unique(male[,1])), # levels で並び順を指定
Age=factor(male[,2], levels=unique(male[,2])), # levels で並び順を指定
Count=as.numeric(male[,3]))
# NA は 0 で置き換える
df_male[is.na(df_male)] <- 0
g <- ggplot(df_male, aes(Week, Count, fill=Age)) +
geom_bar(stat="identity") +
theme(axis.text.x = element_text(angle = 280, hjust = 1, size = 8), legend.position = "bottom")
g
# 男女で横並びの場合
d2020 <- c()
for (i in 1:18) {
d2020 <- rbind(d2020,
c(df$Week[i], "Male", df$Male.Under.10[i] +
df$Male.10s[i] +
df$Male.20s[i] +
df$Male.30s[i] +
df$Male.40s[i] +
df$Male.50s[i] +
df$Male.60s[i] +
df$Male.70s[i] +
df$Male.80s[i] +
df$Male.Over.90[i]),
c(df$Week[i], "Female", df$Female.Under.10[i] +
df$Female.10s[i] +
df$Female.20s[i] +
df$Female.30s[i] +
df$Female.40s[i] +
df$Female.50s[i] +
df$Female.60s[i] +
df$Female.70s[i] +
df$Female.80s[i] +
df$Female.Over.90[i]))
}
df_2020 <- data.frame(Week=factor(d2020[,1], levels=unique(d2020[,1])), # levels で並び順を指定
Gender=factor(d2020[,2], levels=unique(d2020[,2])), # levels で並び順を指定
Count=as.numeric(d2020[,3]))
# NA は 0 で置き換える
df_2020[is.na(df_2020)] <- 0
g2 <- ggplot(df_2020, aes(Week, Count, fill=Gender)) +
geom_bar(stat="identity", position="dodge") +
theme(axis.text.x = element_text(angle = 280, hjust = 1, size = 8), legend.position = "bottom")
g2