向样条曲线添加点

时间:2018-04-12 17:54:12

标签: r ggplot2 ggforce

我希望直接在曲线样条顶部添加点。 这里的代码不起作用,因为geom_point将点放置为直线。见第2点,第3点。我尝试过使用stat_bspline2和geom ="指向"没有成功。

非常感谢帮助。

library(tidyverse)
library(ggforce)

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(1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 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), size = 7)

0 个答案:

没有答案