直方圖是一個簡單但功能強(qiáng)大的圖,相對于小提琴和雨云圖铜靶,直方圖可能是傳達(dá)數(shù)據(jù)的絕佳方式茧痕。下面讓我們開始繪制圖形,喜歡的小伙伴可以關(guān)注個人公眾號R語言數(shù)據(jù)分析指南將分享更多實(shí)用技能,先行拜謝了
自定義主題
library(tidyverse)
theme_niwot <- function(){
theme_bw() +
theme(text = element_text(family = "Helvetica Light"),
axis.line.x = element_line(color="black"),
axis.line.y = element_line(color="black"),
axis.text.x = element_text(family = "Times",size=12,face="plain"),
axis.text.y = element_text(family = "Times",size=12,face="plain"),
panel.border = element_blank(),
axis.title.x = element_text(margin = margin(t = 10),size=13,
family = "Times",color="black"),
axis.title.y = element_text(margin = margin(r = 10),size=13,
family = "Times",color="black"),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_blank(),
plot.margin = unit(c(1, 1, 1, 1), units = ,"cm"),
legend.text = element_text(size = 12,family ="Times"),
legend.title = element_blank(),
legend.key = element_blank(),
panel.background = element_rect(fill = "white"),
legend.background = element_rect(color = "black",
fill = "transparent",size = 2, linetype = "blank"))
}
自定義繪制直方圖
iris %>% ggplot(aes(Sepal.Length))+
geom_histogram(alpha=0.8,fill="palegreen4",color="black",)+
theme_niwot()+
scale_y_continuous(limits = c(0,20),expand = expansion(mult = c(0, 0.1)))+
geom_vline(xintercept = mean(iris$Sepal.Length),
linetype = "dotted",
colour = "palegreen4",size = 1) +
annotate("text", x = 7, y = 15,
label = "The mean number of\nSepal.Length observations was 5.843333") +
geom_curve(aes(x = 5.85, y = 16,yend = 16,xend = 7),
arrow = arrow(length = unit(0.07, "inch"),
ends="first"),size = 0.7,
color = "grey30", curvature = -0.3) +
labs(x = "Sepal.Length", y = "Count")
-
geom_vline 添加垂直線
geom_vline(xintercept, linetype, color, size)
-
annotate 在特定坐標(biāo)處添加文本注釋
-
geom_curve 畫一條曲線
此圖的亮點(diǎn)也許就在于添加曲線進(jìn)行注釋呐籽,下一節(jié)將詳細(xì)介紹如何在圖片中添加注釋曲線