关键词:
点图,也可以叫做散点图,通过绘制散点来呈现数据的分布,使用geom_dotplot()函数来绘制点图:
geom_dotplot(mapping = NULL, data = NULL, position = "identity", ..., binwidth = NULL, binaxis = "x", method = "dotdensity", binpositions = "bygroup", stackdir = "up", stackratio = 1, dotsize = 1, stackgroups = FALSE, origin = NULL, right = TRUE, width = 0.9, drop = FALSE, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
常用的参数注释:
- mapping:使用aes()来设置点图美学特征,参数x是因子,参数y是数值
- data:数据框对象
- method:默认值是dotdensity(点密度封箱),或者histodot,固定的封箱宽度,比如 stat_bin
- binwidth:bin是封箱的意思,该参数用于调整组距,该参数受到method参数的影响,如果method是dotdensity,那么binwidth指定最大的bin宽度;如果method是histodot,那么binwidth指定bin的宽度,默认值是数据范围(range of the data)的1/30。
- binaxis:沿着那个轴进行封箱,默认值是x
- stackdir:设置堆叠点的方向,默认值是up,有效值是down、center、centerwhole和up。
- stackratio:点堆叠的密集程度,默认值是1,值越小,堆集越密集;
- dotsize:点的大小,相对于binwidth的直径,默认值是1。
使用ToothGrowth数据来绘制点图:
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
一,绘制散点图
绘制基本的点图
library(ggplot2) p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_dotplot(binaxis=‘y‘, stackdir=‘center‘, stackratio=1.5, dotsize=1.2)
二,添加汇总数据
向点图中添加汇总数据,使用ggplot2包中的函数
stat_summary(mapping = NULL, data = NULL, geom = "pointrange", position = "identity", ..., fun.data = NULL, fun.y = NULL, fun.ymax = NULL, fun.ymin = NULL, fun.args = list(), na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
常用参数注释:
- fun.data:指定一个函数(function),返回带有变量ymin,y和ymax的数据框 或者,指定三个单独的函数,分别向每个函数传递一个向量,分别返回一个数字,用于表示ymin、y和ymax:
- fun.y:
- fun.ymax:
- fun.ymin:
- fun.args=list():可选的参数,用于指定传递给fun.xxx函数的参数
1,向点图中增加均值和中位数
# dot plot with mean points p + stat_summary(fun.y=mean, geom="point", shape=18, size=3, color="red") # dot plot with median points p + stat_summary(fun.y=median, geom="point", shape=18, size=3, color="red")
2,向点图中增加点范围
fun.low.mean <- function(x)mean(x)-sd(x) fun.up.mean <- function(x)mean(x)+sd(x) ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_dotplot(binaxis=‘y‘, stackdir=‘center‘, stackratio=1.5, dotsize=1.2)+ stat_summary(fun.y = mean, fun.ymin = fun.low.mean, fun.ymax = fun.up.mean, colour = "red", size = 0.7)
3,使用fun.data向点图中增加点范围
data_summary <- function(x) m <- mean(x) ymin <- m-sd(x) ymax <- m+sd(x) return(c(y=m,ymin=ymin,ymax=ymax)) ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_dotplot(binaxis=‘y‘, stackdir=‘center‘, stackratio=1.5, dotsize=1.2)+ stat_summary(fun.data = data_summary, colour = "red", size = 0.7)
三,按照分组改变点图的颜色
首先要对点图分组,这通过aes(fill=dose)来实现,然后,使用scale_fill_manual()来设置每个分组的颜色:
ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) + geom_dotplot(binaxis=‘y‘, stackdir=‘center‘)+ scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
四,添加点图的说明
通过theme函数向点图中增加说明,通过legend.position来控制说明的位置
ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) + geom_dotplot(binaxis=‘y‘, stackdir=‘center‘)+ scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))+ theme(legend.position="top")
五,向点图中增加标题和轴的标签
通过labs()来添加标题和坐标轴的标签
ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) + geom_dotplot(binaxis=‘y‘, stackdir=‘center‘)+ scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))+ labs(title="Plot of length by dose",x="Dose (mg)", y = "Length")+ theme_classic()
参考文档:
ggplot2 dot plot : Quick start guide - R software and data visualization
r绘制病理进展点图(代码片段)
R绘制点图,用于形象表示:病人用药后,有哪几个病理正向,哪几个负向。借助ggbeeswarm和ggplot2可快速绘制此类图形。代码如下#install.packages("ggbeeswarm")library("ggbeeswarm")library("ggplot2")filename<-"/Users/xxx.csv"dt<-read.csv(filename,header=T 查看详情
ggplot2高效实用指南
...更好。本文内容基本是来源于STHDA,这是一份十分详细的ggplot2使用指南,因此我将其翻译成中文,一是有助于我自己学习理解,另外其他R语言爱好者或者可视化爱好者可以用来学习。翻译过程肯定不能十全十美,各位读者有建... 查看详情
r语言使用ggplot2绘制带有边缘直方图的散点图实战
R语言使用ggplot2绘制带有边缘直方图的散点图实战 目录R语言使用ggplot2绘制带有边缘直方图的散点图实战 查看详情
r语言使用ggplot2包使用geom_dotplot函数绘制分组点图(水平点图垂直点图点的大小堆叠比例)实战(dotplot)
R语言使用ggplot2包使用geom_dotplot函数绘制分组点图(水平点图、垂直点图、点的大小、堆叠比例)实战(dotplot)目录R语言使用ggplot2包使用geom_dotplot函数绘制分组点图(水平点图、垂直点图、点的大小、堆叠比例)实战(dotplot) 查看详情
r语言使用ggplot2包使用geom_dotplot函数绘制分组点图(改变图例位置)实战(dotplot)
R语言使用ggplot2包使用geom_dotplot函数绘制分组点图(改变图例位置)实战(dotplot)目录R语言使用ggplot2包使用geom_dotplot函数绘制分组点图(改变图例位置)实战(dotplot) 查看详情
r语言绘图:ggplot2绘制roc
使用ggplot2包绘制ROC曲线rocplot<-function(pred,truth,...){predob<-prediction(pred,truth)#打印AUcperf.auc<-performance(predob,measure='auc',x.measure='cutoff')#perf<-performance(pred 查看详情
r语言使用ggplot2包使用geom_dotplot函数绘制分组点图(改变分组的次序)实战(dotplot)
R语言使用ggplot2包使用geom_dotplot函数绘制分组点图(改变分组的次序)实战(dotplot)目录 R语言使用ggplot2包使用geom_dotplot函数绘制分组点图(改变分组的次序)实战(dotplot) 查看详情
r语言使用ggplot2包使用geom_dotplot函数绘制分组点图(配置显示的分组)实战(dotplot)
R语言使用ggplot2包使用geom_dotplot函数绘制分组点图(配置显示的分组)实战(dotplot)目录 R语言使用ggplot2包使用geom_dotplot函数绘制分组点图(配置显示的分组)实战(dotplot) 查看详情
r语言使用ggplot2包使用geom_dotplot函数绘制分组点图(自定义填充色)实战(dotplot)
R语言使用ggplot2包使用geom_dotplot函数绘制分组点图(自定义填充色)实战(dotplot)目录 R语言使用ggplot2包使用geom_dotplot函数绘制分组点图(自定义填充色)实战(dotplot) 查看详情
r语言使用ggplot2包使用geom_dotplot函数绘制分组点图(单色填充分组颜色填充)实战(dotplot)
R语言使用ggplot2包使用geom_dotplot函数绘制分组点图(单色填充、分组颜色填充)实战(dotplot)目录R语言使用ggplot2包使用geom_dotplot函数绘制分组点图(单色填充、分组颜色填充)实战(dotplot) 查看详情
r语言使用ggplot2包的快速可视化函数qplot绘制散点图(设置数据点的形状大小)实战
R语言使用ggplot2包的快速可视化函数qplot绘制散点图(设置数据点的形状、大小)实战目录 R语言使用ggplot2包的快速可视化函数qplot绘制散点图(设置点的形状、大小)实战 查看详情
r语言使用ggplot2包使用geom_dotplot函数绘制分组点图(自定义分组颜色主题)实战(dotplot)
R语言使用ggplot2包使用geom_dotplot函数绘制分组点图(自定义分组颜色、主题)实战(dotplot)目录 R语言使用ggplot2包使用geom_dotplot函数绘制分组点图(自定义分组颜色、主题)实战(dotplot) 查看详情
r语言使用ggplot2包geom_jitter()函数绘制分组(stripplot,一维散点图)带状图(改变分组次序)实战
R语言使用ggplot2包geom_jitter()函数绘制分组(stripplot,一维散点图)带状图(改变分组次序)实战目录R语言使用ggplot2包geom_jitter()函数绘制分组(stripplot,一维散点图 查看详情
r语言使用ggplot2包使用geom_dotplot函数绘制分组点图(手动编码添加均值标准偏差)实战(dotplot)
R语言使用ggplot2包使用geom_dotplot函数绘制分组点图(手动编码添加均值、标准偏差)实战(dotplot)目录R语言使用ggplot2包使用geom_dotplot函数绘制分组点图(手动编码添加均值、标准偏差)实战(dotplot) 查看详情
r语言使用ggplot2包的快速可视化函数qplot绘制散点图(分类变量分组配色连续值程度配色)实战
R语言使用ggplot2包的快速可视化函数qplot绘制散点图(分类变量分组配色、连续值程度配色)实战目录R语言使用ggplot2包的快速可视化函数qplot绘制散点图(分类变量分组配色、连续值程度配色)实战 查看详情
r语言使用ggplot2包的快速可视化函数qplot绘制散点图(设置每个数据点的文本标签信息)实战
R语言使用ggplot2包的快速可视化函数qplot绘制散点图(设置每个数据点的文本标签信息)实战目录R语言使用ggplot2包的快速可视化函数qplot绘制散点图(设置每个数据点的文本标签信息)实战#仿真数据 查看详情
r语言使用ggplot2包使用geom_dotplot函数绘制分组点图(双分类变量分组可视化)实战(dotplot)
R语言使用ggplot2包使用geom_dotplot函数绘制分组点图(双分类变量分组可视化)实战(dotplot)目录R语言使用ggplot2包使用geom_dotplot函数绘制分组点图(双分类变量分组可视化)实战(dotplot) 查看详情
r语言使用ggplot2包geom_jitter()函数绘制分组(stripplot,一维散点图)带状图(添加均值标准偏差)实战
R语言使用ggplot2包geom_jitter()函数绘制分组(stripplot,一维散点图)带状图(添加均值、标准偏差)实战目录R语言使用ggplot2包geom_jitter()函数绘制分组(stripplot,一维散点图)带状图(添加均值、标准偏差)实战 查看详情