示例数据请私信
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",]
网友评论