专栏名称: EasyCharts
EasyCharts,易图表,我们将定期推送各种数据可视化与分析教程,包括Excel(Power BI)、Origin、Sigmaplot、GraphPad、R、Python、Matlab、Tableau、D3.js等。
目录
相关文章推荐
前端之巅  ·  npm 够用吗?初创企业为何追捧这个 ... ·  昨天  
香港新港人  ·  3大媽突入男廁半分鐘偷笑離開!如廁大叔爆粗鬧 ... ·  23 小时前  
香港新港人  ·  3大媽突入男廁半分鐘偷笑離開!如廁大叔爆粗鬧 ... ·  23 小时前  
前端早读课  ·  【图书】零基础开发AI ... ·  2 天前  
前端早读课  ·  【第3462期】7 分钟深度理解柯里化 ·  2 天前  
前端早读课  ·  【第3461期】多种序列帧格式的最佳实践 ·  3 天前  
51好读  ›  专栏  ›  EasyCharts

ggplot2绘制漂亮的直方图

EasyCharts  · 公众号  · 前端  · 2017-05-11 19:24

正文

今天这篇博文为大家分享如何用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"))







请到「今天看啥」查看全文