将选择点添加到彩色bspline

时间:2018-04-10 17:19:32

标签: r ggplot2 ggforce

继续我之前的bspline question

如果这是我的曲线:

data <- tibble (
  x = c(10, 15, 17, 17, 20, 22, 22, 23, 25, 25, 27, 29),
  y = c(5, 7, 4, 4, 0, 5, 5, 6, 5, 5, 4, 5.5),
  g = c("A", "A", "A", "B", "B", "B", "C", "C", "C", "D","D","D"),
  pt = c(0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1)
)

ggplot(data) + 
  stat_bspline2(aes(x=x, y=y, color = ..group.., group = g), size = 4, n = 300, geom = "bspline0") +
  scale_color_gradientn(colours = c("red", "pink", "green", "white"), guide = F) 

如何在曲线上的选定点上添加点?

这是不怎么做的:

ggplot(data) + 
  stat_bspline2(aes(x=x, y=y, color = ..group.., group = g), size = 4, n = 300, geom = "bspline0") +
  scale_color_gradientn(colours = c("red", "pink", "green", "white"), guide = F) +
  stat_bspline2(data = pt, aes(x = x, y = x, color = ..group.., group = pt), n = 12, geom = "point", size = 9)
)

1 个答案:

答案 0 :(得分:1)

它并不完美,但它确实有效。添加一些具有所需点位置的列(我假设如果pt = 1,则需要绘制点)

data <- data %>%
    mutate(pt_x = ifelse(pt == 1, x, NA),
           pt_y = ifelse(pt == 1, y, NA))

ggplot(data) + 
    stat_bspline2(aes(x=x, y=y, color = ..group.., group = g), size = 4, n = 300, geom = "bspline0") +
    scale_color_gradientn(colours = c("red", "pink", "green", "white"), guide = F) +
    geom_point(aes(pt_x, pt_y))