data <- data.frame(expression=c(exprSet), sample=rep(colnames(exprSet),each=nrow(exprSet))) head(data) data$sample <- factor(data$sample, levels = c("A","B",“C”),使用ggplot画图,计算并标记箱线图的中位数,R语言代码
时间: 2024-03-16 09:40:54 浏览: 110
snakeyaml-1.28-API文档-中文版.zip
5星 · 资源好评率100%
使用ggplot2包绘制箱线图,并使用dplyr包计算数据框中的中位数并标记在图中。
首先,假设数据框的名称为data,需要绘制箱线图的变量为expression,需要按照sample变量分组。代码如下:
```
library(dplyr)
library(ggplot2)
# 计算中位数
median_data <- data %>%
group_by(sample) %>%
summarize(median_expression = median(expression))
# 绘制箱线图
ggplot(data, aes(x = sample, y = expression)) +
geom_boxplot() +
geom_text(data = median_data, aes(x = sample, y = median_expression, label = paste0("Median: ", round(median_expression, 2))),
vjust = -1, hjust = -0.5, size = 4, color = "red") +
labs(x = "Sample", y = "Expression", title = "Boxplot of Expression by Sample") +
scale_x_discrete(limits = c("A", "B", "C"))
```
解释一下代码:
- `library(dplyr)`和`library(ggplot2)`加载需要使用的包。
- `median_data <- data %>% group_by(sample) %>% summarize(median_expression = median(expression))`计算按照sample分组后的expression变量的中位数。
- `ggplot(data, aes(x = sample, y = expression))`指定数据框data和x、y变量作为绘图数据。
- `geom_boxplot()`绘制箱线图。
- `geom_text(data = median_data, aes(x = sample, y = median_expression, label = paste0("Median: ", round(median_expression, 2))), vjust = -1, hjust = -0.5, size = 4, color = "red")`在图中添加中位数文本标签。其中,`data = median_data`指定文本位置和显示内容的数据框,`aes(x = sample, y = median_expression, label = paste0("Median: ", round(median_expression, 2)))`指定文本位置和显示内容,`vjust = -1`和`hjust = -0.5`设置文本的垂直和水平位置,`size = 4`设置文本的大小,`color = "red"`设置文本的颜色。
- `labs(x = "Sample", y = "Expression", title = "Boxplot of Expression by Sample")`设置图形的坐标轴标签和标题。
- `scale_x_discrete(limits = c("A", "B", "C"))`设置x轴的离散值限制为"A"、"B"和"C"。
这段代码会生成一个带有中位数标志的箱线图,中位数标志为红色文本。
阅读全文