R可视化和ggplot2

ggplot2-基础部分

2019-05-20  本文已影响0人  Juan_NF
同样是一无所有,有的人觉得有朝一日可以拥有全世界;
希望你是有的人,可以没脸没皮地积极向上;
####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
准备画布
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
设置特定颜色
ggplot(diamonds, aes(x=carat, y=price, color=cut)) + 
geom_point()+scale_color_brewer(type='qual',palette=8)
对点的形状不满意,可用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)
标题字体设置
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方便一点;
  • 均展示两个变量的情况,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

上一篇 下一篇

猜你喜欢

热点阅读