過去に作成したlolipopグラフのスクリプトを保管するメモ。
library(tidyverse)
# ベクトル作成(値は適当)
value1 <- c(1.57,3.47,6.05,11.84,1.82,3.35,5.49,9.76,1.52,3.51,6.29,14.46)
value2 <- c(1.72,4.61,9.02,13.09,1.95,4.00,7.29,8.93,1.67,4.79,9.82,20.73)
# カテゴリ列定義
sex <- c("all","all","all","all","Male","Male","Male","Male","Female","Female","Female","Female")
x <- c("65-69","70-74","75-79","80+","65-69,Male","70-74,Male","75-79,Male","80+,Male","65-69,Female","70-74,Female","75-79,Female","80+,Female")
# dataframe作成
dat <- data.frame(
x, #年齢階級
value1,
value2,
sex #性別グループ
) %>%
rowwise() %>%
mutate( mymean = mean(c(value1,value2) )) %>%
mutate(x=factor(x, x))
ggplotを使用してロリポップグラフを作成
ggplot(dat) +
geom_segment( aes(x=x, xend=x, y=value1, yend=value2) ) +
geom_point( aes(x=x, y=value1), color="tomato", alpha=0.8,size=3 ) +
geom_point( aes(x=x, y=value2), color="darkgreen", alpha=0.8, size=3 ) +
labs(color = "value1")+
coord_flip()+
theme(
legend.position = "none",
panel.border = element_blank(),
text=element_text(size=15)
) +
xlab("Group") +
ylab("Incident rate")+
facet_wrap(~group, ncol=0, scale="free_y")