4  ggplot2“银河系”加权有无

ggplot2版本的“银河系”:无细胞核面积的加权 vs. 细胞核面积的加权

Published

August 23, 2025

在2025-07-29,我们展示过H&E起源版本的“银河系”(QuPath:“银河系”起源于H&E)。在2025-08-12,我们通过R的ggplot2来展示该“银河系”(R:ggplot2等高线图版本的“银河系”)。在2025-08-15,我们进一步计算特定等高线的面积R:ggplot2版本的“银河系”,并计算特定等高线的面积)。在2025-08-21,我们将等高线展示在原始H&E图片上(R:ggplot2版本的“银河系”,并将等高线展示在H&E图上)。

这次,我们通过ggplot2展示该“银河系”,且比较有无细胞核面积的加权。

1. Load packages and read data/加载包和读取数据

# load packages
library(tidyverse) # for data manipulation and visualization
library(RColorBrewer) # for color palettes
# 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`, `Area µm^2`) # select the columns specifying the coordinates and area of nuclei
df |> head() # display the first few rows of the data frame
# A tibble: 6 × 3
  `Centroid X µm` `Centroid Y µm` `Area µm^2`
            <dbl>           <dbl>       <dbl>
1            131.            88.4        29.2
2            118.            90.2        55.5
3            169.            90.9        55.5
4            179.            95.0        61  
5            230.            96.5        69.8
6            119.            96.8        53.8

2. 增加一个“random_color”列,用于每个细胞核颜色的随机分配

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
# A tibble: 6 × 4
  `Centroid X µm` `Centroid Y µm` `Area µm^2` Random_color
            <dbl>           <dbl>       <dbl> <chr>       
1            131.            88.4        29.2 #A6D854     
2            118.            90.2        55.5 #8DA0CB     
3            169.            90.9        55.5 #B3B3B3     
4            179.            95.0        61   #B3B3B3     
5            230.            96.5        69.8 #8DA0CB     
6            119.            96.8        53.8 #E78AC3     

3. Plot the nuclei as points and add contour lines/绘制无细胞核面积加权的分布图和有细胞核面积加权的分布图

3.1 Without weighted area/无细胞核面积加权

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/有细胞核面积加权

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. “银河系”的起源

给我买杯茶🍵