## 7.3 Barplot

set.seed(10)
x <- sample(c("Tokyo", "Osaka", "Hokkaido", "Fukuoka"), replace = T, size = 30)
print(x)
##  [1] "Hokkaido" "Tokyo"    "Osaka"    "Fukuoka"  "Fukuoka"
##  [6] "Hokkaido" "Fukuoka"  "Osaka"    "Hokkaido" "Hokkaido"
## [11] "Hokkaido" "Fukuoka"  "Hokkaido" "Hokkaido" "Osaka"
## [16] "Hokkaido" "Osaka"    "Osaka"    "Fukuoka"  "Tokyo"
## [21] "Hokkaido" "Fukuoka"  "Osaka"    "Hokkaido" "Osaka"
## [26] "Hokkaido" "Hokkaido" "Osaka"    "Osaka"    "Tokyo"

smry <- table(x)
print(smry)
## x
##  Fukuoka Hokkaido    Osaka    Tokyo
##        6       12        9        3

barplot(smry)

barplot(smry, horiz = TRUE, las=1, cex.names=0.8)

1. smryをデータフレームに変換
2. xとなっている列名をそれぞれcity_nameに変更
df <- as.data.frame(x) # データフレームに変換
df <- df %>% rename(city_name = x)
df %>% head()
##   city_name
## 1  Hokkaido
## 2     Tokyo
## 3     Osaka
## 4   Fukuoka
## 5   Fukuoka
## 6  Hokkaido

ggplot(df) +
geom_bar(aes(x=city_name), position = "identity")

ggplot(df) +
geom_bar(aes(x=city_name), position="identity") +
coord_flip()

Exercise 7.1 (棒グラフの作成) 以下のコードを実行して，仮想的なアイスの好きな味のアンケート結果のベクトルquiestionaireを作成しなさい．

set.seed(10)
choices <- c("vanilla", "strowberry", "chocolate", "cheese", "maccha", "milk")
questionaire <- sample(choices, replace=TRUE, size=40)