标签:颜色 ppi 对象 ggplot2 ons color 最大的 tle 大小
点图,也可以叫做散点图,通过绘制散点来呈现数据的分布,使用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)
常用的参数注释:
使用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)
常用参数注释:
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
标签:颜色 ppi 对象 ggplot2 ons color 最大的 tle 大小
原文地址:https://www.cnblogs.com/ljhdo/p/4886067.html