个人常用的R画图统计代码
文章目录
不管干啥先加载
library(ggplot2);library(reshape2);library(dplyr);library(scales)
mytheme = theme_bw()+theme(
#panel.grid.major = element_blank(),
#panel.grid.minor = element_blank(),
axis.title = element_text(family = 'Times New Roman',face = 'bold'),
axis.text = element_text(family = 'Times New Roman',face = 'bold'),
legend.title = element_text(family = 'Times New Roman',face = 'plain'),
legend.text = element_text(family = 'Times New Roman',face = 'plain'),
legend.position = 'right',
text = element_text(family = 'Times New Roman',face = 'plain'),
strip.background = element_rect(fill = "white")
)
library(ggplot2);library(reshape2);library(dplyr);library(scales);library(ggpubr);library(RColorBrewer);library(lmtest);library(hydroGOF)
mytheme = theme_bw()+theme(
#panel.grid.major = element_blank(),
#panel.grid.minor = element_blank(),
axis.title = element_text(face = 'bold'),
axis.text = element_text(face = 'bold'),
legend.title = element_text(face = 'bold'),
legend.text = element_text(face = 'bold'),
legend.position = 'right',
text = element_text( face = 'bold'),
strip.background = element_rect(fill = "white")
)
drawpdf=function(fig,figname,widthl,heightl){
fignamepdf = paste(figname,'.pdf',sep="")
pdf(fignamepdf,width =widthl,height = heightl)
print(fig)
dev.off()
ppi=300
fignamepng = paste(figname,'.png',sep="")
png(fignamepng,width =widthl*ppi,height = heightl*ppi,res = ppi)
print(fig)
dev.off()
}
result =ggarrange(fig1,fig2,fig3,fig4,fig5,fig6,ncol = 3,nrow=2,
labels =c("(a)","(b)","(c)","(d)","(e)","(f)"),
common.legend = T,legend = "right")
画图
柱状图
geom_bar(position='dodge',stat='identity')
纵向合并
total<-rbind(student1,student2)
分面
facet_grid(纵向~横向)
重命名dataframe列名
names(df) <- c("male", "female", "unknown")
按列名合并dataframe
library(dplyr)
df = left_join(df,rice_type,by = "field")
统计dataframe的平均值和标准差
tapply(field_13_rice_zn$颖壳锌含量, INDEX=field_13_rice_zn$田块, FUN=mean,na.rm=T)(按田块统计颖壳锌含量)
tapply(field_13_rice_zn$颖壳锌含量, INDEX=field_13_rice_zn$田块, FUN=mean,na.rm=T)
myvars = c('籽粒','颖壳')
df = aggregate(df[myvars],by=list(field = df$field), mean,na.rm=T)
1:1线
geom_abline(intercept = 0,slope=1,color='red',linetype="dotdash")
ggplot线形
geom_line(mapping = NULL, data = NULL, stat = "identity",
position = "identity", na.rm = FALSE, show.legend = NA,
inherit.aes = TRUE, ...)
统计函数
使用pastecs包中的stat.desc
library(pastecs)
round(stat.desc(df),4)
or
library(psych)
describe(df)
反转坐标系
+ scale_y_reverse()
coord_flip())
用自己的颜色
library(RColorBrewer)
peise = brewer.pal(9,'OrRd')
mycolor1 = peise[2:7]
+scale_color_manual(values=mycolor1)
显示颜色
library(scales)
show_col(colorsinr)
给数据排序
data$buwei <- factor(data$buwei,levels = c('Milled rice','glume'))
给dataframe排序
la_mean_df=la_mean_df[order(la_mean_df$field),]
根据条件修改数据
field_13_rice_zn$fer_type[field_13_rice_zn$田块=="13.1"] <- "no"
根据日期筛选数据
startday = as.Date("2020-4-21")
endday = as.Date("2020-10-23")
used_wea_data = wea_data[which(wea_data$date>=startday &
wea_data$date<=endday),]
select 显示指定列
selectresult=subset(df1,name=="aa" & sex=="f",select=c(age,sex))
负号排除指定列
selectresult=subset(df1,name=="aa" & sex=="f",select=c(-age,-sex))
数据融合
mydata<-melt(data,id.vars=c("Name","Company"),variable.name="Year",value.name="Sale")
修改列的顺序
df<-df[, c("Grade","Class","Score")]
坐标轴中加入公式或单位
# 全公式
labels = expression(-pi, -pi/2, 0, pi/2, pi)
文本与符号组合
xlab = expression(paste("Phase Angle ", phi))
参考链接
https://www.jb51.net/article/227455.htm
https://www.jb51.net/article/227456.htm
https://www.jb51.net/article/207347.htm
x轴坐标轴角度
待填入
计算R2
library(lmtest)
library(hydroGOF)
R2=round(cor(seldf$DVS,seldf$obsDVS)^2,3)
rmse1=round(rmse(seldf$DVS,seldf$obsDVS),3)
nse1 = round(NSE(seldf$DVS,seldf$obsDVS),3)
颜色
library(RColorBrewer)
display.brewer.all() #显示所有可用颜色
brewer.pal(4,"Set2")
图片拼接
ggarrange(p1,p2,p3,ncol = 3,common.legend = T,legend=)
导出数据无序号
write.table(data[, c("a", "c")], file = "data.txt", row.names = FALSE)
导出pdf
pdf("fig.pdf",width = 6,height = 4)
plot()
dev.off()
drawpdf=function(fig,figname,widthl,heightl){
fignamepdf = paste(figname,'.pdf',sep="")
pdf(fignamepdf,width =widthl,height = heightl)
print(fig)
dev.off()
ppi=300
fignamepng = paste(figname,'.png',sep="")
png(fignamepng,width =widthl*ppi,height = heightl*ppi,res = ppi)
print(fig)
dev.off()
}
画日历热力图
data = left_join(jiancerq,wrjdf,by='date')
data[8,'jiance'] = 3
data[14,'jiance'] = 5
data[23,'jiance'] = 5
data[28,'jiance'] = 5
data[33,'jiance'] = 5
data[38,'jiance'] = 5
data[45,'jiance'] = 5
data[52,'jiance'] = 5
data[65,'jiance'] = 5
data[77,'jiance'] = 5
data[88,'jiance'] = 5
data[111,'jiance'] = 7
data[147,'jiance'] = 7
write.csv(data,'data.csv')
# 在Excel中处理数据
Sys.setlocale(category = "LC_ALL", locale = "C")
data = read.csv('data_process.csv')
data$date <- as.POSIXct(strptime(data$date, format = "%Y/%m/%d %H:%M", tz = "GMT"))
ppi = 600
png("myplot.png",width = 8*ppi,height = 8*ppi,res=ppi)
calendarPlot(data,
pollutant = "jiance",
breaks = c(0, 2, 4,6,8),
# 为了展示图片效果,分段是随意分的,没有科学依据
labels = c("UAV监测",
'UAV监测
+田间水层',
"UAV监测
+水层
+干物质
+叶面积",
'UAV监测
+水层
+干物质'),
cols = c("lightblue", "skyblue", "brown", "gold2"),
statistic="sum",
year = 2020)
dev.off()
误差棒图
ggplot(stadf, aes(x=date,y=leaf_dry_weight,fill=rice_type))+
geom_bar(position = 'dodge',stat='identity',width = 0.7)+
geom_errorbar(aes(ymin = leaf_dry_weight-leaf_dry_weight_sd,
ymax = leaf_dry_weight+leaf_dry_weight_sd),
width = 0.2,size =0.5,position = position_dodge(0.7))+
scale_fill_brewer(palette = 'Set2')+
mytheme
并列柱状图
ggplot(data14, aes(x=handle,y=zn,fill=zn_type))+
geom_bar(position = 'dodge',stat='identity',width = 0.7)+
geom_errorbar(aes(ymin = zn-std,ymax = zn+std),width = 0.2,size =0.5,position = position_dodge(0.7))+
scale_fill_brewer(palette = 'Set2')+
mytheme+
ylim(0,100)+
xlab('Experimental treatment')+
ylab('Zinc content (mg/kg)')+
labs(fill = 'Zn position')+
theme(legend.position = c(0.15,0.8))
组合图片
ggarrange
result =ggarrange(fig1,fig2,fig3,fig4,fig5,fig6,ncol = 3,nrow=2,
labels =c("(a)","(b)","(c)","(d)","(e)","(f)"),
common.legend = T,legend = "right")
patchwork
正常的无背景主题
mytheme = theme_bw()+theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title = element_text(family = 'Times New Roman',face = 'bold'),
axis.text = element_text(family = 'Times New Roman',face = 'bold'),
legend.title = element_text(family = 'Times New Roman',face = 'plain'),
legend.text = element_text(family = 'Times New Roman',face = 'plain'),
legend.position = 'right',
text = element_text(family = 'Times New Roman',face = 'plain'),
)