生信星球培训第四十四期

学习小组Day4笔记-菠萝

2020-03-16  本文已影响0人  菠萝_c93e

R语言语言基础和ggplot2作图

1R基础

1.1安装:R和Rstudio

1.2界面和设置

1.3getwd() 查看工作目录

1.4setwd()设置工作目录

1.5sum(1,2,3,4,5)求和

1.6赋值符号用 <-

1.7简单作图

a 散点图plot(runif(500,50,3)) 500(随机数个数) 50(均值)3(方差) 1.png

b 盒须图

boxplot(iris$Sepal.Length~iris$Species,col = c("orange","brown","cyan"))
#col: 绘图颜色#col.axis:坐标轴刻度颜色#col.lab:坐标轴名称颜色#col.main:图形标题颜色 #col.sub:副标题颜色#fg:图形前景色!#bf:图形背景色
111111.png

1.8.显示文件列表 dir()

1.9加减乘除和删除变量

x<- (10+2)*99
x
a<-3
b <- 1
c <- 4
u <- 5+6
rm(b)##删除某个变量
rm(u,c)  
rm(list = ls()) #删除所有变量

2 ggplot2 作图

2.1 散点图 、分组、分面

ggplot(data= mpg )+
geom_point(mapping=aes(x = displ, y = hwy),color = "blue",shape=9,size=40,alpha=1/4)+   #设置颜色、形状、大小、透明度
geom_smooth(mapping = aes(x = displ, y = hwy,group =trans,linetpye =trans, color =trans)) +  #根据trans分组
theme_bw()+#主题
xlab("abc") + ylab("efg") + #x轴y轴标签
ggtitle("trans")+#图标题
theme(legend.position ="left") +  #图例位置
facet_wrap(~ trans, nrow =4,ncol = 3)#依据trans分面,4行3列
Rplot04.png

2.2空白多圆圈图

ggplot(data = diamonds, mapping = aes(x = cut, colour = clarity)) + 
  geom_bar(fill = NA , position = "identity")+#直方图之对象直接显示-identity。颜色映射是由color和fill之分的,表示边框和填充。如果要设置无填充(也就是透明),则fill=NA
  coord_polar(theta = "y")+ #coord_polar 设置极坐标系
  theme_classic()
Rplot02.png

2.3饼图

ggplot(mpg, aes(x = factor(1), fill = drv)) +
  geom_bar(width = 1) +
  coord_polar(theta = "y")

2.4geom_abline添加线条 coord_fixed保证横纵坐标的标尺一致,线条45°角

2.5柱状图

并列

ggplot(data = diamonds, mapping = aes(x = cut, fill = clarity)) + 
  geom_bar( position = "dodge")
Rplot05.png

堆积柱状图 1312312.png

ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, fill = clarity), position = "fill")#position="fill"设置的每组堆叠条形具有相同的高度。
上一篇 下一篇

猜你喜欢

热点阅读