• 函数中传递列名

      1. plot_fmonth_col <- function(ref, col, title){
      2. col <- rlang::sym(col)
      3. p <- ggplot(ref_tmp,
      4. aes(x = date_diff, col = is_top, legend = is_top)) +
      5. geom_line(aes(y = !!col)) +
      6. labs(title = paste(title, as.character(col))) +
      7. theme_bw() +
      8. theme(plot.title = element_text(hjust = 0.5))
      9. return(p)
      10. }
    • 修改图例标题

      1. p <- ggplot(ur_tag, aes(x = factor(score), fill = factor(is_top,
      2. levels = c(0, 1),
      3. labels = c("非高产", "高产")))) +
      4. geom_histogram(stat = 'count', position = 'fill', alpha = 0.8) +
      5. theme_minimal() +
      6. labs(title = '不同有利标签个数的高产占比',
      7. x = '有利标签条数',
      8. y = '比例') +
      9. theme(plot.title = element_text(hjust = 0.5)) +
      10. geom_hline(yintercept = 0.04197243) +
      11. guides(fill = guide_legend(title = "用户类别"))
      12. ggplotly(p)
    • 分栏绘图

      1. ggplot(user[user$sex != ""], aes(log(like_tea_num+1)), siz) +
      2. geom_histogram(bins = 20, aes(fill = sex)) +
      3. facet_grid(rows = vars(city_level),
      4. cols = vars(sex)) +
      5. labs(title = "收藏老师数目") +
      6. theme(plot.title = element_text(hjust = 0.5))