1

继续我之前的 bspline问题

如果这是我的曲线:

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)
)
4

1 回答 1

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))
于 2018-04-10T17:38:41.580 回答