直方图是一个简单但功能强大的图,相对于小提琴和雨云图,直方图可能是传达数据的绝佳方式。下面让我们开始绘制图形,喜欢的小伙伴可以关注个人公众号R语言数据分析指南将分享更多实用技能,先行拜谢了
自定义主题
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 在特定坐标处添加文本注释
-
geom_curve 画一条曲线

此图的亮点也许就在于添加曲线进行注释,下一节将详细介绍如何在图片中添加注释曲线
网友评论