原标题:ggplot2绘制漂亮的直方图
欢迎关注天善智能 hellobi.com,我们是专注于商业智能BI,大数据,数据分析领域的垂直社区,学习、问答、求职,一站式搞定!
对商业智能BI、大数据分析挖掘、机器学习,python,R等数据领域感兴趣的同学加微信:tstoutiao,邀请你进入数据爱好者交流群,数据爱好者们都在这儿。
今天这篇博文为大家分享如何用ggplot2绘制漂亮的直方图(如封面所示)
数据准备set.seed(070511)
d
基本直方图
library(ggplot2)ggplot(d, aes(x)) + geom_histogram(bins = 50)
通过分组添加颜色
ggplot(d, aes(x, fill = cut(x, 100))) +
geom_histogram(bins = 50)
去掉图列
ggplot(d, aes(x, fill = cut(x, 100))) +
geom_histogram(bins = 50,show.legend = FALSE)
调整色调
ggplot(d, aes(x, fill = cut(x, 100))) +
geom_histogram(show.legend = FALSE) + scale_fill_discrete(h = c(250, 10))
调整饱和度和亮度
ggplot(d, aes(x, fill = cut(x, 100))) +
geom_histogram(show.legend = FALSE) + scale_fill_discrete(h = c(240, 10), c = 120, l = 70)
设置主题
ggplot(d, aes(x, fill = cut(x, 100))) +
geom_histogram(show.legend = FALSE) + scale_fill_discrete(h = c(240, 10), c = 120, l = 70) + theme_minimal() + labs(x = "Variable X", y = "n") + ggtitle("Histogram of X")
为封面绘图
p
geom_histogram(show.legend = FALSE,bins = 50) + theme_minimal() + labs(x = "Variable X", y = "n") + ggtitle("Histogram of X",subtitle = R.version.string)+ labs(caption = "zsrnog")p + scale_fill_discrete(h = c(180, 360), c = 150, l = 80)+ theme(panel.background = element_rect(fill='black'), #panel.border = element_rect(fill='black'), plot.background = element_rect(fill='black'),
plot.title =element_text(colour = "blue"), plot.subtitle=element_text(colour = "blue"), plot.caption=element_text(colour = "blue"), axis.line = element_line(colour = "grey80"), axis.text = element_text(colour = "blue"), axis.title = element_text(colour = "grey80"))
转载请保留以下内容:
本文来源自天善社区EasyCharts老师的博客(公众号)。
原文链接: https://ask.hellobi.com/blog/EasyCharts/8048返回搜狐,查看更多
责任编辑: