ggplot2-基础部分

作者: Juan_NF | 来源:发表于2019-05-20 13:01 被阅读0次
同样是一无所有,有的人觉得有朝一日可以拥有全世界;
希望你是有的人,可以没脸没皮地积极向上;
####diamonds数据集的格式,color、cut、carat、price分别是该数据的列
 > head(diamonds)
# A tibble: 6 x 10
  carat cut       color clarity depth table price     x     y     z
  <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
1 0.23  Ideal     E     SI2      61.5    55   326  3.95  3.98  2.43
2 0.21  Premium   E     SI1      59.8    61   326  3.89  3.84  2.31
3 0.23  Good      E     VS1      56.9    65   327  4.05  4.07  2.31
4 0.290 Premium   I     VS2      62.4    58   334  4.2   4.23  2.63
5 0.31  Good      J     SI2      63.3    58   335  4.34  4.35  2.75
6 0.24  Very Good J     VVS2     62.8    57   336  3.94  3.96  2.48
准备画布
  • 数据需要是数据框格式
  • diamonds是ggplot2自带数据集
library(ggplot2)
####just set up, no point
####格式:ggplot(df, aes(x, y, other aesthetics))
ggplot(diamonds, aes(x=carat, y=price,color=cut))
空白和xy轴.png
加点,加趋势线

ggplot(diamonds, aes(x=carat, y=price,color=cut)) + 
geom_point(alpha=0.1,size=1.0,shape=21,fill='white',stroke=2)+
geom_smooth(method='glm')
点_线.png
  • 依据cut分组,geom_point自动分配颜色
  • alpha设置点的透明度(避免点太大重叠在一起)
  • stroke的第一反应是中风,在这里是指,对于有边界的形状(可以用vignette("ggplot2-specs")查看相应形状),可以为内部和外部分别设置颜色,stroke用于设置边界的宽度;fill即内部颜色,这里color是依据分组的颜色;
  • method设定geom_smooth的曲线拟合方式
  • vignette("ggplot2-specs")调出参数的详细说明
设置特定颜色
ggplot(diamonds, aes(x=carat, y=price, color=cut)) + 
geom_point()+scale_color_brewer(type='qual',palette=8)
  • 可通过type选择‘div’,‘qual’,‘seq’来设置,palette设置数字,选择颜色
    Diverging
    BrBG, PiYG, PRGn, PuOr, RdBu, RdGy, RdYlBu, RdYlGn, Spectral
    Qualitative
    Accent, Dark2, Paired, Pastel1, Pastel2, Set1, Set2, Set3
    Sequential
    Blues, BuGn, BuPu, GnBu, Greens, Greys, Oranges, OrRd, PuBu, PuBuGn, PuRd, Purples, RdPu, Reds, YlGn, YlGnBu, YlOrBr, YlOrRd
  • 还有其他参数,?scale_color_brewer查找即可
对点的形状不满意,可用scale_shape_manual()设置
x、y、figure的title设置
gg <- ggplot(diamonds, aes(x=carat, y=price, color=cut)) + 
geom_point()+scale_color_brewer(type='qual',palette=8)+labs(title="Scatterplot", x="Carat", y="Price") 
print(gg)
  • 这里将整个设置存为图片,通过print打印出来进行展示


    Rplot_各种标题.png
标题字体设置
gg1 <- gg + theme(plot.title=element_text(size=30, face="bold"), 
                  axis.text.x=element_text(size=15), 
                  axis.text.y=element_text(size=15),
                  axis.title.x=element_text(size=25),
                  axis.title.y=element_text(size=25)) 
print(gg1)
Rplot_标题字体.png
标题字体方向-改为垂直

适用于标题太长的时候,可以修改字体的方向

gg2 <- gg + theme(axis.text.x = element_text(angle=90, hjust=1),
                  axis.text.y = element_text(angle=90, hjust=1))
print(gg2)
image.png
展示不同的分类下的分面
##cut和color是其另外的列
gg1 + facet_wrap( ~ cut)
gg_grid<-gg1 + facet_grid(color ~ cut) 
ggsave(gg_grid,filename = 'grid.png',width=20,height=15)
capitalize <- function(string) {
   substr(string, 1, 1) <- toupper(substr(string, 1, 1))
   string
}
conservation_status <- c(
   cd = "Conservation Dependent",
   en = "Endangered",
   lc = "Least concern",
   nt = "Near Threatened",
   vu = "Vulnerable",
   domesticated = "Domesticated"
)
global_labeller <- labeller(
       vore = capitalize,
       conservation = conservation_status,
       conservation2 = label_wrap_gen(10),
       .default = label_both
   )
gg_q<-gg1 + facet_wrap(color ~ cut, scales="free",labeller = global_labeller) 
ggsave(gg_q,filename = 'test.png',width = 20,height = 15)
  • facet_wrap 和facet_grid将一个图展示成不同的panel
查资料的个人理解如下:
  • 单个变量的展示,用facet_wrap比较方便,也好看一点;
  • 两个变量的展示,用facet_grid方便一点;
  • 均展示两个变量的情况,facet_grid会把所有组合均进行展示,而facet_wrap只会展示有点的组合;
  • 可以通过设定labeller的方式改变label的展示情况
  • 图太大就存成图片的格式
Rplot_facet_wrap.png
grid.png facet_wrap_labeller_set.png

[参考内容]
http://r-statistics.co/ggplot2-Tutorial-With-R.html
http://www.rpubs.com/Logos/252923


课程分享
生信技能树全球公益巡讲
https://mp.weixin.qq.com/s/E9ykuIbc-2Ja9HOY0bn_6g
B站公益74小时生信工程师教学视频合辑
https://mp.weixin.qq.com/s/IyFK7l_WBAiUgqQi8O7Hxw
招学徒:
https://mp.weixin.qq.com/s/KgbilzXnFjbKKunuw7NVfw

相关文章

  • ggplot2-基础部分

    同样是一无所有,有的人觉得有朝一日可以拥有全世界;希望你是有的人,可以没脸没皮地积极向上; 准备画布 数据需要是数...

  • 基础部分

    Swift 是一门开发 iOS, macOS, watchOS 和 tvOS 应用的新语言。然而,如果你有 C 或...

  • 基础部分

    坐标系 D3通过SVG画图,SVG的坐标系同HTML中的坐标系类似,都是以左上角作为坐标原点,向下向右为正延伸。 ...

  • 基础部分

    1、基础的数据类型 集合类型:Array、Set 和 Dictionary数据类型:Int(整型值)、Double...

  • 基础部分

    本章内容学习自官方文档 https://swiftgg.gitbook.io/swift/swift-jiao-c...

  • ggplot2绘制带有标准差图

    ggplot2-为折线图和条形图添加误差线 以ToothGrowth数据为例,进行处理 tg <- ToothGr...

  • COVID-19 in africa

    ggplot2-绘制非洲地区新冠肺炎(COVID19) 最近接触COVID的项目,目前COVID已经在世界上普遍大...

  • python基础部分

    C、C++、C#、Java、php、python语言的内在特性及区别 参考:https://blog.csdn.n...

  • 基础部分(1)

    1.Java基本数据类型 四种整数类型(byte、short、int、long) 两种浮点数类型(float、do...

  • JSON基础部分

    1.格式 格式: "key":value 2.两个方法 2.1 parse:把字符串转换成JSON ps:" ' ...

网友评论

    本文标题:ggplot2-基础部分

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