餅圖加環(huán)形標(biāo)注-R

幫朋友復(fù)現(xiàn)的圖,這里代碼做個(gè)留存記錄

目標(biāo)圖:
07ccf9ba01fd0eff123fe454e82f12c.png

R代碼

library(ggplot2)
library(ggnewscale)
library(dplyr)

# 創(chuàng)建示例數(shù)據(jù)(按大致占比)
data <- data.frame(
  group = c("PD1+", "LAG3+", "TIM3+", "PD1+LAG3+", "PD1+TIM3+", "LAG3+TIM3+", "PD1+LAG3+TIM3+", "陰性"),
  value = c(15, 10, 5, 10, 7, 6, 8, 40),
  color = c("blue", "red", "yellow", "purple", "lightblue", "orange", "brown", "gray80")
)

# 計(jì)算起始和結(jié)束角度
data <- data %>%
  arrange(desc(value)) %>%
  mutate(end = cumsum(value),
         start = lag(end, default = 0))

# 繪制餅圖
P <- ggplot(data, aes(ymin = 0, ymax = 1, xmin = start, xmax = end, fill = group)) +
  geom_rect() +
  scale_fill_manual(values = setNames(data$color, data$group)) +
  coord_polar(theta = "x") +
  theme_void() +
  theme(legend.position = "none") +
  ggtitle("SFV-Luc 免疫分布") +
  new_scale_fill() +  # 開啟第二個(gè)填充層
  
  # 疊加外圈標(biāo)注
  geom_rect(data = data %>% filter(group %in% c("PD1+", "PD1+LAG3+", "PD1+TIM3+", "PD1+LAG3+TIM3+")),
            aes(ymin = 1, ymax = 1.1, xmin = start, xmax = end, fill = "PD1+"), inherit.aes = FALSE) +

  
  geom_rect(data = data %>% filter(group %in% c("LAG3+", "PD1+LAG3+", "LAG3+TIM3+", "PD1+LAG3+TIM3+")),
            aes(ymin = 1.1, ymax = 1.2, xmin = start, xmax = end, fill = "LAG3+"), inherit.aes = FALSE) +

  
  geom_rect(data = data %>% filter(group %in% c("TIM3+", "PD1+TIM3+", "LAG3+TIM3+", "PD1+LAG3+TIM3+")),
            aes(ymin = 1.2, ymax = 1.3, xmin = start, xmax = end, fill = "TIM3+"), inherit.aes = FALSE) +
  scale_fill_manual(values = c("PD1+" = "blue", "LAG3+" = "red", "TIM3+" = "yellow"), na.value = NA)
P

結(jié)果圖:


2fa17643fcdccf7b3ffc2ed67a2f057.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容