1.提取数据框指定行与列
如提取df中Plot为FP的行
FP <- df[which(df$Plot == "FP"),]
FP <- subset(df,Plot=="FP")
提取df中几列,可select()输入列名称,或start_col:end_col
select(df,-conservation, -(sleep_total:awake)) #在列名称前面添加减号来取消列
select(df, name, starts_with("sleep")) #通过starts_with(),ends_with()或contains( )来选择相似名称的列
select_if(df, is.numeric)
select_if(df, is.character) #按照数据类型选择列
select(df, conservation, sleep_total, everything()) #用everything()函数将选择的列移至表格最前
select(df, animal = name, sleep_total) #选择同时更改列名
提取行与列
subset(f,f$origin=="JFK",select=c(1:10))
2.分面绘图
绘制VC-Flen的散点图,按Plot分面,scales="free"让每个图有独立的x、y轴(同理scales=‘free_y’ ‘free_x’),同时去掉子标题填充色
ggplot(df,aes(x=VC,y=Flen))+geom_point()+facet_wrap(~Plot,scales="free",nrow=5)+
theme(strip.text=element_text(size=14,face="bold"),
strip.background=element_rect(fill="white"))
3.添加线性回归线并标注相关系数、p值、回归方程与R2
添加回归线并标注回归方程、R2,size=3.5指定公式字号,coef.digits=3指定三位有效数字
ggplot(df,aes(x,y))+geom_point()+geom_smooth(method="lm",se=FALSE,formula = y ~ x)+
stat_poly_eq(aes(label=paste(..eq.label..,..rr.label..,sep='~~~')),
formula = y ~ x,parse= TRUE, size=3.5,label.x="right",coef.digits=3)
使用stat_cor() 自动添加相关系数和显著性水平(P值),通过label.x label.y指定文本对应的x、y轴坐标,也可通过label.x.npc=0~1指定位置
stat_cor(method = "pearson", label.x = 3, label.y = 30)
4.Patchwork拼图
| + 实现水平拼接,/实现垂直布局
(p1+p2+p3)/(p4+p5+p6)/(p7+p8+plot_spacer())
也可以通过plot_layout()来指定布局,定义网格的尺寸以及要分配给不同行和列的空间
bxp + dens + plot_layout(ncol = 1, heights = c(1, 3))
可以使用&或 * 向所有子图添加元素,而不必单独修改所有图
添加标记
p + plot_annotation(tag_levels = 'A')
plot_grid(p1, p2, p3, labels = c("A", "B", "C"), ncol = 2, nrow = 2)
合并相同图例
p + plot_layout(guides = 'collect')
也可以通过ggarrange共享图例并控制图例的位置
ggarrange(p1,p2,ncol = 1, nrow = 2,common.legend = TRUE,legend="right",align = "v")
5.数据处理
数据融合melt与重塑dcast
----df1包括5列:id变量Plot、Time、RainLevel,测量变量variable(df中其他列合并成的一列,包括var1 var2…),value。
----df2包括至少4列:id变量Plot、RainLevel,var1(按Time取平均),var2(按Time取平均),…
df1 <- melt(df,id=c("Plot","Time","RainLevel"))
df2 <- dcast(df1,Plot+RainLevel~variable,mean)
变量标记为日期或者因子并排序
RL$date <- as.Date(RL$date)
df$RainLevel <- factor(df$RainLevel,levels=c("小雨","中雨","大雨","暴雨"))
df <- arrange(df,RainLevel)
分组取平均
—ddply,如FL按date,ID分组,对多个变量同时取平均值和标准差
FLms<-ddply(FL,.(date,ID),summarise,mean1=mean(FL_M1),sd1=sd(FL_M1))
—aggregate,也能按多个变量分组,求多列的均值或标准差,似乎一次只能求一种函数
如下例对x按sex分组,对3-4列分别求平均值
x=data.frame(name=c("张三","李四","王五","赵六"),
sex=c("M","M","F","F"),age=c(20,40,22,30),height=c(166,170,150,155))
aggregate(x[,3:4], list(sex=x$sex), mean)
替换某一列中某个特定数值
把df中Gender列中的F替换成W
df$Gender[which(df$Gender =='F')] <- 'W'
6.ggplot绘图
带误差棒的柱状图
ggplot(df1,aes(x=ID,y=mean,ymin=mean-sd,ymax=mean+sd))+
geom_bar(aes(fill=ID), stat = "identity",alpha=0.8)+
geom_errorbar(aes(color=ID),width=0.1)+
scale_color_brewer(palette = 'Paired')+scale_fill_brewer(palette = 'Paired')
带误差棒的折线图
ggplot(df,aes(x=Plot,y=mean))+
geom_line(aes(group=""),color="#E8630A")+geom_point(color="#E8630A")+
geom_errorbar(color="#E8630A",aes(ymin=mean-sd,ymax=mean+sd),width=0.1)
分组柱状图
ggplot(df,aes(x,y))+geom_bar(stat = 'identity',aes(fill=Plot),
position=position_dodge(0.9))+mytheme+scale_fill_brewer(palette = 'Paired')+
labs(x=NULL, y="jl")+ylim(0,0.8)
7.ggplot绘图细节
去掉图例
+guides(fill="none",color="none")
修改图例标题名称name、标签的顺序breaks及内容labels
+scale_colour_discrete(name="tuli", breaks = c("6","4","8"),
labels = c("B","A","C")))