今天这篇推文小编写一些基础的内容:如何绘制在散点图上显示其线性模型线性模型的拟合结果及其置信区间。这里小编使用R和Python分别绘制,主要内容如下:
小编这里将结合R-ggpubr包进行必要图表元素的的添加,首先,我们使用ggplot2进行基本的绘制,如下:
「样例一」:单一类别
library(tidyverse)
library(ggtext)
library(hrbrthemes)
library(wesanderson)
library(LaCroixColoR)
library(RColorBrewer)
library(ggsci)
# 读取数据
library(readxl)
test_df<-read_excel("test_data.xlsx")
# 可视化绘制
ggplot(data = test_df,aes(x = total_bill,y = tip)) +
geom_point(shape=21,fill="#0073C2",colour="black",size=3,stroke=.5) +
geom_smooth(fill="#868686",colour="#CD534C") +
labs(
title = "Example of <span style='color:#D20F26'>ggplot2::geom_smooth function</span>",
subtitle = "processed charts with <span style='color:#1A73E8'>geom_smooth(method = 'loess')</span>",
caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
theme(
plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
size = 20, margin = margin(t = 1, b = 12)),
plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
plot.caption = element_markdown(face = 'bold',size = 12))
Example of ggplot2::geom_smooth(method = 'loess')
注意:这里使用的是method = 'loess' 参数设置,还可以设置method = 'l' ,结果(这里我们同时设置使用ggpubr包添加了部分绘图元素):
ggplot(data = test_df,aes(x = total_bill,y = tip)) +
geom_point(shape=21,fill="#0073C2",colour="black",size=3,stroke=.5) +
geom_smooth(method = lm,fill="#868686",colour="#CD534C") +
ggpubr::stat_regline_equation(label.x = .1,label.y = 10,size=6,fontface='bold') +
ggpubr::stat_cor(aes(label = paste(..r.label..,..p.label.., sep = "~`,`~")),
label.x = .1, label.y = 9,size=6,fontface='bold') +
labs(x="",y="",
title = "Example of <span style='color:#D20F26'>ggplot2::geom_smooth function</span>",
subtitle = "processed charts with <span style='color:#1A73E8'>geom_smooth(method = 'lm')</span>",
caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
theme(
plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
size = 20, margin = margin(t = 1, b = 12)),
plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
plot.caption = element_markdown(face = 'bold',size = 12))
Example of ggplot2::geom_smooth(method = 'lm')
可以看出:使用ggpubr::stat_regline_equation() 和ggpubr::stat_cor() 分别添加了拟合公式和R和P等指标的添加。接下来,小编再介绍多个元素的绘制方法。
「样例二」:多个类别
绘制多个类别的图表只需将映射进行设置即可,如下:
ggplot(data = test_df,aes(x = total_bill,y = tip)) +
geom_point(aes(fill=sex),shape=21,size=3,stroke=.5) +
geom_smooth(aes(colour=sex,fill=sex),method = lm) +
ggsci::scale_fill_nejm()+
ggsci::scale_colour_nejm()+
ggpubr::stat_regline_equation(aes(color=sex),label.x = .1,size=6,fontface='bold') +
ggpubr::stat_cor(aes(label = paste(..r.label..,..p.label.., sep = "~`,`~"),color=sex),
label.x = 20,size=6,fontface='bold') +
labs(x="",y="",
title = "Example of <span style='color:#D20F26'>ggplot2::geom_smooth function</span>",
subtitle = "processed charts with <span style='color:#1A73E8'>geom_smooth(method = 'lm')</span>",
caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
theme(
plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
size = 20, margin = margin(t = 1, b = 12)),
plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
plot.caption = element_markdown(face = 'bold',size = 12))
Example03 of ggplot2::geom_smooth(method = 'lm'
当然,设置geom_smooth(method = 'loess') 即可获得如下可视化结果:
Example04 of ggplot2::geom_smooth(method = 'loess'
上述就简单介绍完R绘制的方法,接下来我们介绍使用Python绘制此类图。
这里小编使用了Python-seaborn库中的lmplot()函数进行绘制,详细如下:
「样例一」:单一类别
import seaborn as sns
import matplotlib.pyplot as plt
# 可视化全局设置
plt.style.use('fivethirtyeight')
plt.rcParams['font.family'] = "Times New Roman"
plt.rcParams["axes.titlesize"] = 18
#可视化绘制
sns.lmplot(x="total_bill", y="tip", data=tips,height=5, aspect=1.2,scatter_kws={"color":"#0073C2","edgecolor":"black"},
line_kws={"color":"#BC3C28","linewidth":2})
# 由于不支持ax参数,我们可以这样操作
ax = plt.gca()
# 定制化操作
ax.set_facecolor("white")
for spine in ['top','bottom','left','right']:
ax.spines[spine].set_color("white")
ax.tick_params(labelsize = 15,direction = 'in')
#ax.grid(which='major',axis='y',ls='--',c='k',alpha=.7)
ax.grid(which='major',ls='--',c='k',alpha=.7)
ax.set_xlim(left=0,right=55)
ax.set_ylim(bottom=0,top=10.5)
titlefontdict = {"size":23,"color":"k",'family':'Times New Roman'}
ax.set_title('Example01 Of Linear Regression Scatter Plot ',titlefontdict,pad=15)
text_font = {'family':'Times New Roman','size':'22','weight':'bold','color':'black'}
ax.text(.9,.9,"(a)",transform = ax.transAxes,fontdict=text_font,zorder=4)
ax.text(.8,.056,'\nVisualization by DataCharm',transform = ax.transAxes,
ha='center', va='center',fontsize = 11,color='black')
Example01 Of seaborn.lmplot()
注意:由于seaborn.lmplot()不支持ax属性设置,我们想要对其定制化操作,则可以通过如下语句进行:
ax = plt.gca()
通过设置该语句,我们就可以使用一些常用的matplotlib定制化操作语句啦~
「样例二」:多个类别 seaborn.lmplot()函数对多个类别的图表绘制也是非常简单的,通过设置hue参数即可,详细如下:
sns.lmplot(x="total_bill", y="tip", hue="sex",hue_order=['Female','Male'],data=tips,height=5, aspect=1.2,
palette=["#BC3C28","#0072B5"],scatter_kws={"edgecolor":"black"},line_kws={"linewidth":2})
# 由于不支持ax参数,我们可以这样操作
ax = plt.gca()
# 定制化操作
ax.set_facecolor("white")
for spine in ['top','bottom','left','right']:
ax.spines[spine].set_color("white")
ax.tick_params(labelsize = 15,direction = 'in')
#ax.grid(which='major',axis='y',ls='--',c='k',alpha=.7)
ax.grid(which='major',ls='--',c='k',alpha=.7)
ax.set_xlim(left=0,right=55)
ax.set_ylim(bottom=0,top=10.5)
titlefontdict = {"size":23,"color":"k",'family':'Times New Roman'}
ax.set_title('Example02 Of Linear Regression Scatter Plot ',titlefontdict,pad=15)
text_font = {'family':'Times New Roman','size':'22','weight':'bold','color':'black'}
ax.text(.9,.9,"(a)",transform = ax.transAxes,fontdict=text_font,zorder=4)
ax.text(.8,.056,'\nVisualization by DataCharm',transform = ax.transAxes,
ha='center', va='center',fontsize = 11,color='black')
Example02 Of seaborn.lmplot()
从这里可以看出,Python-seaborn和ggplot2绘图语法较为相近,对一些统计绘图也更加友好,而需要绘制出定制化的图表,则需熟悉matplotlib的各个属性函数含义。
以上就是简单的介绍如何使用R和Python绘制带有拟合区间的散点图,更多详细资料可参考:ggplot2::geom_smooth()[1]seaborn.lmplot()[2]
本期推文小编简单介绍了如何绘制在散点图上显示其线性模型线性模型的拟合结果及其置信区间,同时也比较了R-ggplot2和Python-seaborn绘制图表的不同,希望小伙伴们可选择适合自己的工具进行可视化图表的绘制。
[1]
ggplot2::geom_smooth()资料: https://ggplot2.tidyverse.org/reference/geom_smooth.html。
[2]
seaborn.lmplot()资料: https://seaborn.pydata.org/generated/seaborn.lmplot.html#seaborn.lmplot。