R绘图:ggeconodist,基于ggplot2的另类箱图
R绘图系列回顾
今天我们介绍一个基因ggplot2的另类箱图制作的包:ggeconodist
这个包是根据“经济学人”杂志独特的箱图风格所开发的R包
首先安装包
install.packages("ggeconodist", repos = "https://cinc.rud.is")先用ggplot2画一个简单的箱图
p <- ggplot(mpg, aes(class, hwy)) + theme_ipsum_rc()p+ geom_boxplot()
用ggeconodist包改变风格,我们只需要将geom_boxplot()换成geom_econodist()即可
p + geom_econodist(width = 0.25)
放在同一张图中,将图旋转,对比两种箱图的效果
(p + geom_boxplot() + coord_flip()) + (p + geom_econodist(tenth_col = ft_cols$blue, ninetieth_col = ft_cols$red) + coord_flip()) + plot_layout(ncol = 1)
配对作图对比
(p + geom_boxplot(aes(fill = factor(drv)))) + (p + geom_econodist(aes(fill = factor(drv)))) + plot_layout(ncol = 1)
用自己的数据测试一下
load(file = 'drawdata1.Rdata')ggplot(drawdata1, aes(Type, LDHA,color=Type))+ geom_boxplot()+ stat_compare_means(comparisons = my_comparisons)
把geom_boxplot()换成geom_econodist()
ggplot(drawdata1, aes(Type, LDHA,color=Type),color = "Type" )+ geom_econodist(width = 0.5) + stat_compare_means(comparisons = my_comparisons)
与ggplot2::geom_boxplot()一样,可以传入“ymin”、“median”和“ymax”的预计算值,也可以传入一个y列,然后使用stat_econodist()计算所需的统计数据。
ggplot(mammogram_costs, aes(x = city)) + geom_econodist( aes(ymin = tenth, median = median, ymax = ninetieth), stat = "identity", show.legend = TRUE ) + scale_y_continuous(expand = c(0,0), position = "right", limits = range(0, 800)) + coord_flip() + labs( x = NULL, y = NULL, title = "Mammoscams", subtitle = "United States, prices for a mammogram*\nBy metro area, 2016, $", caption = "*For three large insurance companies\nSource: Health Care Cost Institute" ) + theme_econodist() -> gg
grid.newpage()left_align(gg, c("subtitle", "title", "caption")) %>% add_econodist_legend(econodist_legend_grob(), below = "subtitle") %>% grid.draw()
参考:https://github.com/hrbrmstr/ggeconodist
赞 (0)
