如何在ggtern中绘制具有不同大小点的三元图形?

时间:2019-10-24 13:48:57

标签: r ggplot2 ggtern

我想制作一个可以改变每个点的大小的图表。我的数据有些患者只有3种可能的成分中的1种。结果,在一个顶点中,我有1个以上的患者信息重叠,并且我不想抖动。

到目前为止我所拥有的:

library(compositions)
library(ggtern)
ds <- structure(list(`GC+` = c(1, 0, 9, 21, 2, 0, 0, 0, 4, 0, 0, 24, 
                               0, 0, 1, 0, 0, 3, 3, 0, 5, 0, 0, 3, 0, 0, 0, 2, 11, 0, 0, 18, 
                               13, 0, 6, 8, 0, 1, 0, 1, 23, 0, 1, 4, 5), `PC+` = c(5, 2, 8, 
                                                                                   0, 6, 0, 0, 0, 10, 0, 0, 20, 0, 0, 2, 0, 0, 3, 3, 0, 0, 0, 10, 
                                                                                   2, 0, 0, 0, 0, 10, 1, 0, 4, 8, 0, 1, 16, 1, 2, 0, 0, 18, 0, 0, 
                                                                                   0, 1), `OT+` = c(0, 2, 7, 0, 0, 0, 0, 0, 2, 0, 1, 0, 0, 0, 0, 
                                                                                                    0, 2, 5, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 12, 0, 0, 6, 3, 1, 0, 
                                                                                                    6, 0, 0, 0, 0, 3, 0, 0, 3, 0), size = c(1, 1, 1, 4, 1, 0, 0, 
                                                                                                                                            0, 1, 0, 3, 1, 0, 0, 1, 0, 3, 1, 1, 0, 4, 0, 1, 1, 0, 0, 0, 1, 
                                                                                                                                            1, 2, 0, 1, 1, 3, 1, 1, 2, 1, 0, 4, 1, 0, 4, 1, 1)), row.names = c(NA, 
                                                                                                                                                                                                               45L), class = "data.frame")
d.tern <- as.data.frame(acomp(ds))
size <- apply(d.tern, 2, function(x) {
  sum(x==1)
})
ds$size <- ifelse(d.tern$`GC+` == 1, 4,
                  ifelse(d.tern$`PC+` == 1, 2,
                         ifelse(d.tern$`OT+` == 1, 3, 1)))
ds$size[is.na(ds$size)] <- 0
ggtern(data = ds, aes(`GC+`, `PC+`, `OT+`)) + 
  geom_mask() +
  geom_point(fill="red", shape=21, size = 3) + 
  theme_bw() +
  theme_showarrows() +
  theme_clockwise() +
  labs(x = "GC+", y = "PC+", z = "OT+",
       title = "Composição dos Linfonodos Positivos")

我想将ds的大小传递给geom_point。但这不起作用。

enter image description here

1 个答案:

答案 0 :(得分:1)

因此,这是一种计算每个唯一值样本的方法:

tab <- as.data.frame(table(ds[,1:3]))

# Keep only observed samples
tab <- tab[tab$Freq > 0,]

# Fix colnames to contain plus
colnames(tab) <- gsub("\\.", "+", colnames(tab))

# For reasons I don't understand the columns were converted to factors
# so we'll fix them again as numeric
tab[, 1:3] <- lapply(tab[, 1:3], as.numeric)

然后绘制如下:

ggtern(data = tab, aes(`GC+`, `PC+`, `OT+`)) + 
  geom_mask() +
  geom_point(aes(size = Freq), fill="red", shape=21) +
  scale_size_continuous(range = c(3, 5), breaks = sort(unique(tab$Freq))) +
  theme_bw() +
  theme_showarrows() +
  theme_clockwise() +
  labs(x = "GC+", y = "PC+", z = "OT+",
       title = "Composição dos Linfonodos Positivos")

enter image description here

您可以使用scale_size_continuous()功能,直到您满意的尺寸为止。