r语言 - 将选择点添加到彩色 bspline



继续我之前的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)
)

它并不完美,但它有效。添加一些带有所需点位置的列(我假设如果 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))

相关内容

  • 没有找到相关文章

最新更新