-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2025-08-23_ggplot2版本的银河系-无细胞核面积的加权-vs-细胞核面积的加权.qmd
More file actions
81 lines (60 loc) · 3.33 KB
/
2025-08-23_ggplot2版本的银河系-无细胞核面积的加权-vs-细胞核面积的加权.qmd
File metadata and controls
81 lines (60 loc) · 3.33 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
---
title: ggplot2“银河系”加权有无
subtitle: ggplot2版本的“银河系”:无细胞核面积的加权 vs. 细胞核面积的加权
date: 2025-08-23
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))。在2025-08-15,我们进一步计算特定等高线的面积[R:ggplot2版本的“银河系”,并计算特定等高线的面积](2025-08-15_ggplot2版本的银河系-并计算特定等高线的面积.qmd))。在2025-08-21,我们将等高线展示在原始H&E图片上([R:ggplot2版本的“银河系”,并将等高线展示在H&E图上](2025-08-21_ggplot2版本的银河系-并将等高线展示在HE图上.qmd))。
这次,我们通过ggplot2展示该“银河系”,且比较有无细胞核面积的加权。
## 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
```
```{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
```
```{r}
df <- df |>
dplyr::select(`Centroid X µm`, `Centroid Y µm`, `Area µm^2`) # select the columns specifying the coordinates and area of nuclei
df |> head() # display the first few rows of the data frame
```
## 2. 增加一个“random_color”列,用于每个细胞核颜色的随机分配
```{r}
palette <- brewer.pal(8, "Set2") # get a color palette
df <- df |>
mutate(Random_color = sample(palette, nrow(df), replace = TRUE)) # add a random color column
df |> head() # Display the first six rows of the updated data frame
```
## 3. Plot the nuclei as points and add contour lines/绘制无细胞核面积加权的分布图和有细胞核面积加权的分布图
### 3.1 Without weighted area/无细胞核面积加权
```{r}
nuclei_unweighted <- ggplot(df, aes(x = `Centroid X µm`, y = `Centroid Y µm`)) +
geom_point(aes(color = Random_color)) + # 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_unweighted
ggsave("images/2025-08-23_without-weighted-area.png", width = 8, height = 6, dpi = 300)
```
### 3.2 With weighted area/有细胞核面积加权
```{r}
nuclei_weighted <- ggplot(df, aes(x = `Centroid X µm`, y = `Centroid Y µm`)) +
geom_point(aes(color = Random_color, size = `Area µm^2`), alpha = 1/2) + # scatter plot
scale_size_area("Area\n(µm^2)", breaks = c(5, 10, 20, 40, 80, 120, 160)) + # set size scale
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_weighted
```
## 4. “银河系”的起源
<img src="images/he_low.png" style="width: 100%; height: auto">
[给我买杯茶🍵](给我买杯茶.qmd)