1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| obes <- read.csv("obes.csv") library(ggplot2) library(reshape2) library(viridis) library(dplyr) library(cowplot) obgrid <- melt(obes, id = c("state", "reg.bea", "reg.cens")) names(obgrid) <- c("state", "reg.bea", "reg.cens", "year", "ob.rate") obgrid$year <- as.character(obgrid$year) obgrid$year <- obgrid$year %>% gsub(pattern = "ob.", replacement = "20") obgrid$year <- as.numeric(obgrid$year)
obgrid$state <- reorder( subset(obgrid, year == 2015)$state, subset(obgrid, year == 2015)$ob.rate) (p1 <- ggplot(obgrid, aes(year, state)) + geom_tile(aes(fill = ob.rate)) + scale_x_continuous(breaks = 2003:2015) + scale_fill_viridis(option = "plasma", labels = scales::percent) + labs(title = "各州的成年人肥胖率\n", x = "年份", fill = "肥胖率") + facet_wrap(reg.bea ~ ., scales = "free_y", ncol = 2) + theme_bw(base_family = 'STSong', base_size = 12) + theme(plot.title = element_text(family = 'STSongti-SC-Bold', hjust = 0.1, size = 20)) + theme(plot.margin = grid::unit(c(0.8, 1.8, 1.8, 1.8), "cm")) + theme(axis.title.y = element_blank()) + theme(axis.text.x = element_text(angle = 45, vjust = -0.2)))
ggdraw(p1) + draw_label("制图:程振兴", x = 0.87, y = 0.05, fontfamily = 'STSong', size = 14) + draw_image("https://www.czxa.top/images/default28.png", x = 0.76, y = 0.02, width = 0.06, height = 0.06)
|