美文网首页
2023-07-07 ggplot2画图总结

2023-07-07 ggplot2画图总结

作者: 汪大山 | 来源:发表于2023-07-06 18:49 被阅读0次

ggplot画图的模式就是把以下所有的小模块加到一起

1.引入ggplot包

library(ggplot2)

2.绘图

2.1 先导入数据统一设置,映射,颜色,尺寸都可以在这里设置

ggplot(data=data,aes(x=a,y=b))+  

2.2 选择图的类型,function自行选择

geom_function(aes(x=a,y=b))+ 

2.3 控制坐标轴内容的显示,(范围、具体数值、科学计数显示等),y同理,当然还包括各种数据类型都可以,这里的是连续的

scale_x_continous(limits = c(0,140000),breaks = c(0,50000,130000),label = scales::number_format(accuracy = 1))+ 

2.4 内置主题的选择和自定义的显示设置(框线、标签大小颜色角度,在图中偏移位置等)
注:先用默认主题,再下一步详细设置,不然会相互覆盖

theme_bw()+  
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) + #坐标轴显示宣传90°

2.5坐标轴标签显示设置

xlab("samples") + ylab("counts") +labs("")

2.6标题显示设置

ggtitle("")

2.7垂直线水平线

geom_vline(xintercept = 20,linetype = "dashed", color = "red")
geom_hline(yintercept = 20,linetype = "dashed", color = "red")

2.8自定义坐标轴的显示

scale_x_discrete(breaks=c("motif_1","motif_2","motif_3","motif_4","motif_5","motif_6"),
                   labels=c("1","2","3","4","5","6"))

2.9自定义图例显示
图例其实就是图中颜色的简练描述,背后使用的数据是一样的,所以用scale_fillscale_color可以同时修改图中颜色和旁边的图例

scale_fill_manual(name="Motif length",labels=c("1","2","3","4","5","6"),values=c() )  #前提是之前没有进行过scale_fill的设置

2.10 在图中注释

annotate("text", x = mean(range(heightweight$ageYear)), y = Inf,
label = "I'm a titile", vjust = 1.5, size = 4.5)

相关文章

网友评论

      本文标题:2023-07-07 ggplot2画图总结

      本文链接:https://www.haomeiwen.com/subject/ltxrudtx.html