-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2025-08-15_ggplot2版本的银河系-并计算特定等高线的面积.qmd
More file actions
158 lines (121 loc) · 6.35 KB
/
2025-08-15_ggplot2版本的银河系-并计算特定等高线的面积.qmd
File metadata and controls
158 lines (121 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
---
title: ggplot2“银河系”等高线
subtitle: ggplot2版本的“银河系”,并计算特定等高线的面积
date: 2025-08-15
toc-depth: 4
toc-expand: true
lang: en
---
在2025-07-29日,我们展示过H&E起源版本的“银河系”([QuPath:“银河系”起源于H&E](2025-07-29_银河系起源于HE.qmd)),在2025-08-12,我们通过R的ggplot2来展示该“银河系”([R:ggplot2等高线图版本的“银河系”](2025-08-12_ggplot2等高线图版本的银河系.qmd))。
这次,我们进一步计算特定等高线的面积。
## 1. Load packages and read data/加载包和读取数据
加载包。
```{r}
#| message: false
#| warning: false
# load packages
library(tidyverse) # for data manipulation and visualization
library(RColorBrewer) # for color palettes
library(patchwork) # for combining plots
library(pracma) # for calculating the area of a polygon
```
读取数据。
```{r}
# read the table
txt_file <- "raw_data/2025-08-12_QuPath_InstanSeg.txt" # specify the path to the text file
df <- txt_file |> read_delim(delim = "\t", col_names = TRUE, show_col_types = FALSE) # read the text file as a data frame
txt_file |> rm() # remove the txt_file variable to free up memory
df <- df |>
dplyr::select(`Centroid X µm`, `Centroid Y µm`) # select the columns specifying the coordinates of nuclei
df |> head() # display the first few rows of the data frame
```
## 2. Plot the nuclei as points and add contour lines/绘制细胞核和添加等高线
Plot the nuclei as points./绘制细胞核。
```{r}
nuclei <- ggplot(df, aes(x = `Centroid X µm`, y = `Centroid Y µm`)) +
geom_point(color = "#0072B2") + # scatter plot
scale_x_continuous(limits = c(0, 1000)) + # set x limits
scale_y_continuous(limits = c(0, 900)) + # set y limits
theme_classic() # apply classic theme
nuclei
```
Plot the nuclei as points and add contour lines./绘制细胞核和等高线。
```{r}
nuclei_contour_plot <- ggplot(df, aes(x = `Centroid X µm`, y = `Centroid Y µm`)) +
geom_point(color = "#0072B2") + # scatter plot
geom_density_2d(aes(color = after_stat(level)), bins = 15) + # add contour lines
scale_color_viridis_c(option = "H") + # set color scale for contour lines
scale_x_continuous(limits = c(0, 1000)) + # set x limits
scale_y_continuous(limits = c(0, 900)) + # set y limits
theme_classic() # apply classic theme
nuclei_contour_plot
```
## 3. Claculate the area of a contour level/计算一个等高线的面积
Extract the data used to create the contour plot/提取用于创建等高线图的数据
```{r}
plot_data <- nuclei_contour_plot |>
ggplot_build() |>
pluck("data", 2) # extract the data used to create the contour plot
plot_data |> dim() # display the dimensions of the contour plot data
plot_data |> distinct(level) # display distinct levels in the contour plot
```
Select a level (e.g. 8th level)./选择一个等高线(比如第8个level)
```{r}
levels <- plot_data |> distinct(level) # select the unique levels
plot_data_8 <- plot_data |>
filter(level == levels$level[8]) # filter the data for the 8th level
```
Plot the 8th contour level./绘制第8个等高线
```{r}
contour_8 <- ggplot(plot_data_8, aes(x = x, y = y, group = group)) +
geom_polygon(color = "blue", fill = "lightblue", alpha = 0.5) + # draw the polygon with specified fill and border color
theme_classic() + # apply classic theme
scale_x_continuous(limits = c(0, 1000)) + # set x limits
scale_y_continuous(limits = c(0, 900)) # set y limits
contour_8
```
Calculate the area and center of the 8th contour level using the `pracma` package/计算第8个等高线的面积和中心坐标。
```{r}
# set a function to calculate the area and center of a polygon
calculate_polygon_area_pracma <- function(df) {
x <- df$x
y <- df$y
n <- length(x)
area <- polyarea(x, y) # calculate the area
center <- poly_center(x, y) # calculate the center
return(c(area, center))
}
# use the function to calculate the area and center of each polygon of 8th contour level
area_each_polygon <- plot_data_8 |>
group_by(group) |>
group_map(~ calculate_polygon_area_pracma(.x))
area_each_polygon
```
Plot the area of the 8th contour level with the area annotated./绘制面积图,并标注面积
```{r}
contour_8_area <- ggplot(plot_data_8, aes(x = x, y = y, group = group)) +
geom_polygon(color = "blue", fill = "lightblue", alpha = 0.5) + # draw the polygon with specified fill and border color
theme_classic() + # apply classic theme
scale_x_continuous(limits = c(0, 1000)) + # set x limits
scale_y_continuous(limits = c(0, 900)) + # set y limits
annotate("text", x = area_each_polygon[[1]][2], y = area_each_polygon[[1]][3], label = round(area_each_polygon[[1]][1], 2), size = 5, color = "red") + # add the area and center label
annotate("text", x = area_each_polygon[[2]][2], y = area_each_polygon[[2]][3], label = round(area_each_polygon[[2]][1], 2), size = 5, color = "red") + # add the area and center label
annotate("text", x = area_each_polygon[[3]][2], y = area_each_polygon[[3]][3], label = round(area_each_polygon[[3]][1], 2), size = 5, color = "red") # add the area and center label
contour_8_area
```
Plot the 8th contour level with the arear annotated on the original image./绘制面积图到原图,并标注面积
```{r}
ggplot(df, aes(x = `Centroid X µm`, y = `Centroid Y µm`)) +
geom_polygon(data = plot_data_8, aes(x = x, y = y, group = group), fill = "#cccccc") +
geom_point(color = "#969696") + # scatter plot
geom_density_2d(bins = 15, color = "#cbc9e2") + # add contour lines
scale_x_continuous(limits = c(0, 1000)) + # set x limits
scale_y_continuous(limits = c(0, 900)) + # set y limits
theme_classic() + # apply classic theme
annotate("text", x = area_each_polygon[[1]][2], y = area_each_polygon[[1]][3], label = round(area_each_polygon[[1]][1], 2), size = 6, color = "#f03b20") + # add the area and center label
annotate("text", x = area_each_polygon[[2]][2], y = area_each_polygon[[2]][3], label = round(area_each_polygon[[2]][1], 2), size = 6, color = "#f03b20") + # add the area and center label
annotate("text", x = area_each_polygon[[3]][2], y = area_each_polygon[[3]][3], label = round(area_each_polygon[[3]][1], 2), size = 6, color = "#f03b20") # add the area and center label
```
## 4. “银行系”的起源
<img src="images/he_low.png" style="width: 100%; height: auto;">
[给我买杯茶🍵](给我买杯茶.qmd)