函数中传递列名
plot_fmonth_col <- function(ref, col, title){col <- rlang::sym(col)p <- ggplot(ref_tmp,aes(x = date_diff, col = is_top, legend = is_top)) +geom_line(aes(y = !!col)) +labs(title = paste(title, as.character(col))) +theme_bw() +theme(plot.title = element_text(hjust = 0.5))return(p)}
修改图例标题
p <- ggplot(ur_tag, aes(x = factor(score), fill = factor(is_top,levels = c(0, 1),labels = c("非高产", "高产")))) +geom_histogram(stat = 'count', position = 'fill', alpha = 0.8) +theme_minimal() +labs(title = '不同有利标签个数的高产占比',x = '有利标签条数',y = '比例') +theme(plot.title = element_text(hjust = 0.5)) +geom_hline(yintercept = 0.04197243) +guides(fill = guide_legend(title = "用户类别"))ggplotly(p)
分栏绘图
ggplot(user[user$sex != ""], aes(log(like_tea_num+1)), siz) +geom_histogram(bins = 20, aes(fill = sex)) +facet_grid(rows = vars(city_level),cols = vars(sex)) +labs(title = "收藏老师数目") +theme(plot.title = element_text(hjust = 0.5))
