假设我在ggplot2中有以下情节:
p <- ggplot() +
geom_point(aes(x=15,y=50),shape=19,fill="gray0", size=5)+
geom_point(aes(x=28, y=75),shape=19, fill="gray0", size=5)+
geom_point(aes(x=13, y=100),shape=19, fill="gray0", size=5)+
geom_segment(aes(x = 15, y = 50, xend = 28, yend = 75), size=1)+
geom_segment(aes(x = 13, y = 100, xend = 28, yend = 75), size=1)+
geom_segment(aes(x = 15, y = 50, xend = 13, yend = 100), size=1)
这将绘制在公共点相交的三个线段,从而创建一个三角形。然后如何用颜色填充形成的三角形? (假设我需要使用geom_segment)
答案 0 :(得分:2)
您可以改用geom_polygon:
df <- data.frame(x = c(15, 28, 13), y = c(50,75,100))
ggplot() +
geom_point(df, mapping = aes(x = x, y = y), size = 5) +
geom_polygon(df, mapping=aes(x = x, y = y), fill="grey")
您可能希望将这些点放在三角形的顶部,如果需要,则将geom_polygon然后单击geom_point