如何在ggtern图中添加特定的标记点?

时间:2018-05-21 04:03:55

标签: r ggplot2 ggtern

使用ggplot2我通常希望能够像这样添加数据点,

ggtern(df, aes(X, Y, Z, value = VALUE), aes(x, y, z)) +
        geom_point(aes(fill = VALUE), size = 2, stroke = 0, shape = 21) + 
        scale_fill_gradient(low = "red",high = "yellow", guide = F) + 
        scale_color_gradient(low = "red",high = "yellow", guide = F) +
    geom_point(aes(x = 10, y = 10, z = 50), shape = 21)

但是,当使用ggtern包生成三元图时,它们会被插入错误的位置(参见示例图像),并显示以下警告:

Warning: Ignoring unknown aesthetics: z

这意味着ggplot2可能会尝试渲染点而不是ggtern。如何在ggtern图中添加特定的标记点?

Ternery plot with data point in wrong location

2 个答案:

答案 0 :(得分:0)

这似乎有两个要点。第一个是创建一个annotation,虽然这可能并不理想,因为它不像一个点那么精确。一个例子是,

ggtern() + 
annotate(geom  = 'text',
              x     = c(0.5,1/3,0.0),
              y     = c(0.5,1/3,0.0),
              z     = c(0.0,1/3,1.0),
              angle = c(0,30,60),
              vjust = c(1.5,0.5,-0.5),
              label = paste("Point",c("A","B","C")),
              color = c("green","red",'blue')) +
  theme_dark() + 
  theme_nomask()

enter image description here

第二种选择是创建一个新的data frame and add that to the plot.虽然这样做的好处是可以更好地控制这一点,但缺点是标签需要额外的工作。

答案 1 :(得分:0)

一种可能性是建立一个标识您想要标记的点的列,在本例中为“lab”列,并说我想标记第一和第三点:

df <- data.frame(x=c(10,20,30), y=c(15,25,35), z=c(75,55,35), VALUE=c(1,2,3), lab=c("One", "", "Three"))

然后可以使用geom_text或geom_label标记这些特定点,例如:

ggtern(df, aes(x, y, z, value = VALUE)) +
  geom_point(aes(fill = VALUE), size = 2, stroke = 0, shape = 21) + 
  scale_fill_gradient(low = "red",high = "yellow", guide = F) + 
  scale_color_gradient(low = "red",high = "yellow", guide = F) +
  geom_text(aes(label = lab), vjust=1)