我试图用不同的颜色填充下图中的 3 个三角形。

data = data.frame(x=c(125), y=c(220)) #this data is just to be able to use gplot to draw figures
ggplot(data, aes(x = x, y = y)) +
xlim(0,250) +
ylim(-250, 0) +
geom_curve(x = 33, xend = 223, y = -100, yend = -100, curvature = -.65) +
geom_segment(x=128, xend = 33, y=-208, yend = -100) +
geom_segment(x=128, xend = 223, y=-208, yend = -100) +
geom_segment(x=128, xend = 159.67, y=-208, yend = -45) +
geom_segment(x=128, xend = 96.33, y=-208, yend = -45) +
coord_fixed()
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
简短的回答:这是一个非常邪恶的黑客。
现在让我们详细说明:正如在这个 GitHub 线程中特别讨论的那样,无法访问由此产生的坐标geom_curve(它CurveGrob用于绘图和“这些值都是在绘制时计算的”[@thomasp85])。其“绘制时间行为计算”的一种效果如下所示 - 如果添加coord_plot与否,它会有所不同。这与 不同geom_spline:添加coord_fixed不会改变坐标。
请参见下面的图一和图二:红色曲线是用geom_curve它创建的- 它与 geom_segment 线失去联系......
@thomasp85 在 GitHub 线程中建议可以改用他的包ggforce。现在,要真正控制曲率,需要使用 geom_bspline 并调整曲率。
一旦找到曲率,就可以使用 ggplot_build 对象中的坐标。我们可以根据这些坐标计算多边形(这也不是很简单,因为需要创建切割并为正确的“边缘”添加点)。见下文。
library(tidyverse)
library(ggforce)
mydata = data.frame(x = 128, xend = c(33, 223, 159.67, 96.33), y = -208, yend = c(-100,-100,-45,-45))
#for spline control points.
my_spline <- data.frame(x = c(33, 128, 223), y = c(-100, 24,-100))
Run Code Online (Sandbox Code Playgroud)
接下来我将演示“绘制时计算(红色曲线)”和“直接计算”之间的区别:
随着coord_fixed 红色和黑色曲线触摸段
ggplot(mydata) +
geom_curve(aes(x = 33, xend = 223, y = -100, yend = -100), curvature = -.65, color = 'red') +
geom_segment(aes(x = x, xend = xend, y = y, yend = yend)) +
geom_bspline(data = my_spline, aes(x, y )) +
coord_fixed()
Run Code Online (Sandbox Code Playgroud)

没有coord_fixed 红色曲线不接触线段,但黑色曲线仍然接触
ggplot(mydata) +
geom_curve(aes(x = 33, xend = 223, y = -100, yend = -100), curvature = -.65, color = 'red') +
geom_segment(aes(x = x, xend = xend, y = y, yend = yend)) +
geom_bspline(data = my_spline, aes(x, y ))
Run Code Online (Sandbox Code Playgroud)

# Final hack
# Get x/y coordinates from ggplot_build
p <- ggplot(mydata) +
geom_bspline(data = my_spline, aes(x, y ))
pb <- ggplot_build(p)$data[[1]]
#create groups for fill
data_polygon <- data.frame(x = pb[['x']], y = pb[['y']]) %>%
mutate(cut_poly = cut(x, c(-Inf, 96.33, 159.67, Inf), labels = letters[1:3]))
#add corner points - repeat extremes from b, otherwise there will be a gap
data_add <- data_polygon %>%
filter(cut_poly == 'b') %>%
slice(which.min(x), which.max(x)) %>%
mutate(cut_poly = letters[c(1,3)]) %>%
bind_rows(data.frame(x = 128, y = -208, cut_poly = letters[1:3], stringsAsFactors = FALSE)) %>%
arrange(x) #important to arrange, otherwise you get irregular polygons
data_plot <- rbind(data_polygon,data_add)
ggplot(data_plot) +
geom_polygon(aes(x, y, fill = cut_poly), color = 'black')
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v0.3.0)于 2019 年 12 月 5 日创建
小智 5
您可以访问 ggforce 包中生成的几何图形的曲线数据,这使得从曲线创建多边形的工作变得更加容易。
然后您可以使用 geom_polygon 绘制单个多边形并用不同的颜色填充它们
library(ggforce)
p1 <- ggplot() + geom_arc(aes(x0 = 125, y0 = -200, r = 100, start = -pi/3, end = -pi/9))
p2 <- ggplot() + geom_arc(aes(x0 = 125, y0 = -200, r = 100, start = -pi/9, end = pi/9))
p3 <- ggplot() + geom_arc(aes(x0 = 125, y0 = -200, r = 100, start = pi/9, end = pi/3))
df_poly1 <- rbind(c(125,-200),data.frame(x = ggplot_build(p1)$data[[1]]$x,y = ggplot_build(p1)$data[[1]]$y),c(125,-200))
df_poly2 <- rbind(c(125,-200),data.frame(x = ggplot_build(p2)$data[[1]]$x,y = ggplot_build(p2)$data[[1]]$y),c(125,-200))
df_poly3 <- rbind(c(125,-200),data.frame(x = ggplot_build(p3)$data[[1]]$x,y = ggplot_build(p3)$data[[1]]$y),c(125,-200))
ggplot() +
geom_polygon(data = df_poly1, aes(x,y), fill = 'red') +
geom_polygon(data = df_poly2, aes(x,y), fill = 'blue') +
geom_polygon(data = df_poly3, aes(x,y), fill = 'green')
Run Code Online (Sandbox Code Playgroud)