美文网首页
【R】箱线图

【R】箱线图

作者: 鸦言 | 来源:发表于2020-07-15 19:31 被阅读0次

示例数据请私信

setwd("D:/R语言学习")
library(ggplot2)
library(reshape2)
library(scales)
data = read.csv("pros_with_name.csv", header = T, row.names = 1)
data = data[2:100, 2:22]
data2 = melt(data)
head(data2)
summary(data2)
p = ggplot(data2, aes(x = variable, y = value, color = variable)) +
  geom_boxplot() +
  theme(axis.title = element_text(size = 23, family = "serif")) +
  theme(axis.text.x = element_text(hjust = 0.5, vjust = 0.5, angle= 0, family = "serif", size = 18)) +
  theme(axis.text.y = element_text(hjust = 0.5, vjust = 0.5, angle= 0, family = "serif", size = 18)) +
  theme(legend.position = "right")
p
image.png

实心

# 实心
p = ggplot(data2, aes(x = variable, y = value)) +
  geom_boxplot(aes(fill=factor(variable))) +
  theme(axis.title = element_text(size = 23, family = "serif")) +
  theme(axis.text.x = element_text(hjust = 0.5, vjust = 0.5, angle= 0, family = "serif", size = 18)) +
  theme(axis.text.y = element_text(hjust = 0.5, vjust = 0.5, angle= 0, family = "serif", size = 18)) +
  theme(legend.position = "right")
p
image.png

小提琴图

# violin plot
data = read.csv("pros_with_name.csv", header = T, row.names = 1)
data = data[2:1000, 2:7]
data2 = melt(data)
p = ggplot(data2, aes(x = variable, y = value)) +
  geom_violin(aes(fill=factor(variable))) +
  theme(axis.title = element_text(size = 23, family = "serif")) +
  theme(axis.text.x = element_text(hjust = 0.5, vjust = 0.5, angle= 0, family = "serif", size = 18)) +
  theme(axis.text.y = element_text(hjust = 0.5, vjust = 0.5, angle= 0, family = "serif", size = 18)) +
  theme(legend.position = "none")
p
image.png

jitter plot

# jitter plot
p = ggplot(data2, aes(x = variable, y = value)) +
  geom_jitter(aes(color = factor(variable))) +
  theme(axis.title = element_text(size = 23, family = "serif")) +
  theme(axis.text.x = element_text(hjust = 0.5, vjust = 0.5, angle= 0, family = "serif", size = 18)) +
  theme(axis.text.y = element_text(hjust = 0.5, vjust = 0.5, angle= 0, family = "serif", size = 18)) +
  theme(legend.position = "none")
p
image.png
# jitter plot (ggbeeswarm包)

install.packages("ggbeeswarm")
library(ggbeeswarm)

p = ggplot(data2, aes(x = variable, y = value)) +
  geom_quasirandom(aes(colour = factor(variable)), groupOnX = TRUE) +
  theme_bw() +
  theme(panel.grid.major = element_blank(),  panel.grid.minor = element_blank(), legend.key = element_blank()) +
  theme(legend.position = "none")
p
image.png

分组

# 增加分组信息

data = read.csv("pros_with_name.csv", header = T, row.names = 1)
data2 = data[1:1000, 2:8]
group = unlist(lapply(substring(rownames(data2), 1, 2), function(x) x[1]))
data2$group = group
data3 = melt(data2, id.vars = "group")
data4 = data3[data3$variable == "A",]
unique(data4$group)
data5_1 = data4[data4$group == "YP",]
data5_2 = data4[data4$group == "QG",]
data5_3 = data4[data4$group == "QE",]
data5 = rbind(data5_1, data5_2, data5_3)


# 调整顺序
data5$group = factor(data5$group, levels = c("YP", "QG", "QE"),)


# 画图
p = ggplot(data5, aes(x = group, y = value)) +
  geom_violin(aes(fill = factor(group))) +
  theme(axis.title = element_text(family = "serif", size = 23)) +
  theme(axis.text = element_text(family = "serif", size = 18)) +
  theme(legend.position = "none")
p
image.png

按分组,多种变量

# 按分组
data = read.csv("pros_with_name.csv", header = T, row.names = 1)
data2 = data[1:1000, 2:8]
group = unlist(lapply(substring(rownames(data2), 1, 2), function(x) x[1]))
data2$group = group
data3 = melt(data2, id.vars = "group")
data4 = data3
unique(data4$group)
data5_1 = data4[data4$group == "YP",]
data5_2 = data4[data4$group == "QG",]
data5_3 = data4[data4$group == "QE",]
data5 = rbind(data5_1, data5_2, data5_3)


# 调整顺序
data5$group = factor(data5$group, levels = c("YP", "QG", "QE"),)


# 画图
p = ggplot(data5, aes(x = variable, y = value)) +
  geom_boxplot(aes(fill = factor(group))) +
  theme(axis.title = element_text(family = "serif", size = 23)) +
  theme(axis.text = element_text(family = "serif", size = 18)) +
  theme(legend.position = "none")
p
image.png

注:

获取数据时

data5_1 = data4[data4$group == "YP",]
data5_2 = data4[data4$group == "QG",]
data5_3 = data4[data4$group == "QE",]
data5 = rbind(data5_1, data5_2, data5_3)

可以替换为

data5 = data4[data4$group == "YP"|data4$group == "QG"|data4$group == "QE",]

相关文章

  • 【R】箱线图

    示例数据请私信 实心 小提琴图 jitter plot 分组 按分组,多种变量 注: 获取数据时 可以替换为

  • R语言 箱线图

    箱线图是数据集中的数据分布良好的度量。 它将数据集分成三个四分位数。 此图表表示数据集中的最小值,最大值,中值,第...

  • r 画箱线图

    http://r-graph-gallery.com/[http://r-graph-gallery.com/]h...

  • 2020-10-26学习小组Day4笔记--文惠

    因为我会用R和R studio,所以笔记比较少 R作图:plot散点图boxplot箱线图清除所有数据rm(lis...

  • ggpubr包系列学习教程(十一)

    使用ggboxplot函数绘制箱线图 加载所需R包 基本用法: Usage 常用参数 Arguments 使用示例...

  • 如果绘制箱线图-Excel2013

    文章简介 继续学习,这次学习了箱线图的概念、四分位数计算以及箱线图的绘制,分享给大家。 箱线图简介 箱线图(Box...

  • R中的箱线图进阶

    箱线图能够显示出离群点(outlier),离群点也叫做异常值,通过箱线图能够很容易识别出数据中的异常值。 geom...

  • [R语言]boxplot绘图经验总结

    箱线图是统计分析里面最为重要且基础的图形,利用R语言绘制箱线图是数据分析里必不可少的一环。这里我总结了面对不同情形...

  • R绘图

    heatmap pheatmap 实例 其他1 其他2 其他3 线图1 线图2 箱线图1 箱线图2 火山图 韦恩...

  • seaborn实例-boxplot-箱线图

    关于箱线图的理论,参考:箱线图(Box Plot)理论篇 这一篇看看seaborn中绘制箱线图 seaborn.b...

网友评论

      本文标题:【R】箱线图

      本文链接:https://www.haomeiwen.com/subject/hpodhktx.html