如何用R语言绘制散点图(数据分组展示)并同时添加全数据的线性和指数两条拟合线?

Python0143

如何用R语言绘制散点图(数据分组展示)并同时添加全数据的线性和指数两条拟合线?,第1张

用R作图,比用EXCEL要灵活的多。

散点图,直接用plot()即可

多类别,在R中就是多变量,用pionts() 加类别

拟合曲线用 fit<- lm()

lines(fit)

添加文字用 text()

给你一些代码,你慢慢研究:

install.packages('ggplot2')

library(ggplot2)

ggplot(a)+geom_bar(aes(x1,y,fill/col=x1/x2),position='dodge',stat='summary',fun='sum'/'mean')条形图+theme(text = element_text(family='Kai'))

ggplot(a)+geom_boxplot(aes(x1,y,col=x1/x2))箱线图

ggplot(a)+geom_point(aes(x1,y,col=x1/x2),position=position_jitter(width=0.04))散点图

1+geom_point(aes(x1,y,col=x1/x2),stat='summary',fun='sum'/'mean')+散点

2+geom_line(aes(x1,y,group=1/x2,col=x1/x2),stat='summary',fun='sum'/'mean')+折线

3+geom_errorbar(aes(x=x1,ymin=y-se,ymax=y+se,col=x1/x2),position=position_dodge(0.9),width=0.2)+误差

4+geom_text(aes(x1,y,label=marker,col=x1/x2),position=position_dodge(0.9)vjust=2或y+2)+显著字母

ggplot(a,aes(x1,y,fill/col=x1/x2))+geom_bar(position='dodge',stat='summary',fun='sum'/'mean')+geom_errorbar(aes(ymin=y-se,ymax=y+se),position=position_dodge(0.9),width=0.2)+geom_text(aes(label=marker),position=position_dodge(0.9),vjust=-2)条形图+误差棒+显著字母(坐标写一次即可)

ggplot(a,aes(x1,y,col=x1/x2))+geom_point(position=position_jitter(width=0.04),stat='summary',fun='sum'/'mean')+geom_line(aes(group=1/x2),stat='summary',fun='sum'/'mean')+geom_errorbar(aes(ymin=y-se,ymax=y+se),position=position_dodge(0.9),width=0.2)+geom_text(aes(label=marker),position=position_dodge(0.9),vjust=-2)散点图+折线+误差棒+显著字母(坐标写一次即可)

+geom_density(aes(y=liqi))密度图(1个数值型)

+geom_area(aes(x=tan,y=liqi))区域图(2个数值型)

+geom_smooth(aes(x=tan,y=liqi,group/col=chong),formula=y~x,method='lm',se=F)拟合图,分组/线条颜色(2个数值型)

+facet_wrap(~riqi,ncol/nrow=2,labeller='label_both/value')分面图,每行或每列分面数,分面标题

+xlab('自变量1(单位)')+ylab('因变量(单位)')+scale_fill_discrete(name='自变量2')更改轴和图例名称+coord_cartesian(ylim= c(0,80))限定轴范围

(fill=x1/x2,有此即可变色)+scale_fill_manual(values = c('grey70', 'grey50', 'grey30'))改变条形填充颜色(颜色数量=分组数量)

(col=x1/x2,有此即可变色)+scale_color_manual(values = c('red', 'orange', 'yellow'))改变颜色(颜色数量=分组数量)