首页 > 解决方案 > 是否有用于创建带有尖椭圆或透镜的绘图的自定义几何图形或函数?

问题描述

我为我的大学项目测量了大约 2000 颗种子的长度和宽度。我想绘制它们的形状以便于解释视觉表示。

它们是椭圆的,但末端是尖的,所以椭圆函数不太有效。

有关种子形状的想法,请参见图片。

在此处输入图像描述

标签: rplotellipse

解决方案


编辑:添加coord_fixed()以确保纵横比正确,并添加一些虚拟点来拉伸画布。

这行得通吗?它是背靠背的geom_curves()

#Stackoverflow seed lens geom_curve
#question from:
#https://stackoverflow.com/questions/68433803/is-there-a-function-in-r-for-creating-an-ellipse-with-pointed-ends#comment120943747_68433803
#assistance from:
#https://stackoverflow.com/a/55627647/4927395

library(tidyverse)

df <- tribble(
  ~seed, ~h, ~w, ~row,
  #----|----|---|---
  "Oak", 5,  4,   1,
  "Soy", 2,  2,   2,
  "Rye", 4,  1,   3
)


df <- df %>% mutate(curvature = w/h)

ggplot() + 
  lapply(split(df, 1:nrow(df)), function(dat) {
    geom_curve(data = dat, aes(x = row, y = -h/2, xend = row, yend = h/2), curvature = dat["curvature"]) }
  ) +
  lapply(split(df, 1:nrow(df)), function(dat) {
    geom_curve(data = dat, aes(x = row, y = h/2, xend = row, yend = -h/2), curvature = dat["curvature"]) }
  )+
  geom_point(aes(x=-2, y=0), colour = NA)+
  geom_point(aes(x=5, y=0), colour = NA)+
  geom_label(data = df, aes(x=row, y = 0, label = seed))+
  coord_fixed(ratio = 1) +
  #coord_cartesian(xlim = c(-2,nrow(df)+2))
  ylab(NULL)+


ggsave("seeds.png")

结果: 种子图片


推荐阅读