如何填充由直线和曲线创建的几何图形?

我正尝试用不同的颜色填充下图中的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 = 159.67,yend = -45) +
  geom_segment(x=128,xend = 96.33,yend = -45) +
  coord_fixed()

我该怎么做?

wokun688 回答:如何填充由直线和曲线创建的几何图形?

简短的答案:这是一个非常邪恶的hack。

现在让我们详细说明一下:如especially in this GitHub thread中所述,不可能访问geom_curve产生的坐标(它使用CurveGrob进行绘制,“这些值都是在绘制时计算的时间” [@ thomasp85])。其“绘制时行为的计算”的一种效果可以在下面看到-如果不添加coord_plot,它会有所不同。这与geom_spline不同:添加coord_fixed不会更改坐标。

请参见下面的图1和2:红色曲线是用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))

接下来,我演示“绘制时间(红色曲线)的计算”与“直接计算”之间的区别:

使用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()

没有coord_fixed 红色曲线不触及线段,但黑色曲线仍触及线段

ggplot(mydata) + 
  geom_curve(aes(x = 33,y )) 

# Final hack
# Get x/y coordinates from ggplot_build
p <- ggplot(mydata) + 
  geom_bspline(data = my_spline,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,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,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')

reprex package(v0.3.0)于2019-12-05创建

,

您可以访问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,start = -pi/9,end = pi/9))
p3 <- ggplot() + geom_arc(aes(x0 = 125,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,data.frame(x = ggplot_build(p2)$data[[1]]$x,y = ggplot_build(p2)$data[[1]]$y),-200))
df_poly3 <- rbind(c(125,data.frame(x = ggplot_build(p3)$data[[1]]$x,y = ggplot_build(p3)$data[[1]]$y),-200))

ggplot() + 
  geom_polygon(data = df_poly1,y),fill = 'red') + 
  geom_polygon(data = df_poly2,fill = 'blue') +
  geom_polygon(data = df_poly3,fill = 'green')

这将产生这样的图像。 enter image description here

本文链接:https://www.f2er.com/3004057.html

大家都在问