ggplot2繪製漂亮的直方圖
今天這篇博文為大家分享如何用ggplot2繪製漂亮的直方圖(如封面所示)
數據準備
set.seed(070511)d <- data.frame(x = rnorm(2000))
基本直方圖
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")
EasyCharts團隊出品為封面繪圖
p <- ggplot(d, aes(x, fill = cut(x, 100))) + 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團隊^..^~
QQ交流群:553270834
微信公眾號:EasyCharts
更多信息敬請查看: http://easychart.github.io/post/Easycharts/
推薦閱讀:
※離散顏色標度連續化的最佳實踐
※R語言 RStudio快捷鍵總結
※R語言文本挖掘包安裝方法匯總
※北京歷史空氣質量數據可視化~
※[原]打造數據產品的快速原型:Shiny的Docker之旅
TAG:R编程语言 |