gpt4 book ai didi

r - geom_curve - 绘制零以上的所有曲线

转载 作者:行者123 更新时间:2023-12-02 04:26:10 25 4
gpt4 key购买 nike

我正在尝试制作类似于 arcdiagram 的弧线图包,使用ggplot2的geom_curve:

library(ggplot2)

ggplot(myDat, aes(x = A, xend = B, y = 0, yend = 0)) +
geom_curve(alpha = 0.2, curvature = 1, ncp = 1000, lineend = 'butt')

enter image description here

有些曲线低于 0,我怎样才能使它们全部高于 Y = 0?

数据:

myDat <- 
structure(list(A = c(36047276L, 36074979L, 36074979L, 36074979L,
36074979L, 36074979L, 36074979L, 36074979L, 36074979L, 36074979L,
36074979L, 36077099L, 36077099L, 36077099L, 36077099L, 36077099L,
36077099L, 36077099L, 36077099L, 36098040L, 36098040L, 36098040L,
36098040L, 36098040L, 36098040L, 36098040L, 36098040L, 36098040L,
36098040L, 36098040L, 36098040L, 36098040L, 36098040L, 36098040L,
36098040L, 36098040L, 36099840L, 36099840L, 36099840L, 36099840L,
36099840L, 36099840L, 36099840L, 36099840L, 36099840L, 36099840L,
36099840L, 36099840L, 36099840L, 36099840L, 36099840L, 36099840L,
36099840L, 36101586L, 36101586L, 36101586L, 36101586L, 36101586L,
36101586L, 36101586L, 36101586L, 36101586L, 36101586L, 36101586L,
36101586L, 36101586L, 36101586L, 36101586L, 36101586L, 36101586L,
36103565L, 36103565L, 36103565L, 36103565L, 36103565L, 36103565L,
36103565L, 36103565L, 36103565L, 36103565L, 36103565L, 36103565L,
36103565L, 36103565L, 36103565L, 36103565L, 36103565L),
B = c(36047417L,
36077099L, 36077279L, 36077863L, 36078510L, 36079111L, 36079565L,
36080165L, 36080428L, 36082473L, 36082907L, 36074979L, 36077279L,
36077863L, 36078510L, 36079111L, 36079565L, 36080165L, 36082907L,
36096300L, 36097775L, 36098987L, 36099840L, 36099952L, 36100767L,
36101156L, 36101586L, 36101633L, 36101926L, 36102035L, 36102381L,
36102833L, 36103149L, 36103565L, 36103872L, 36104121L, 36096300L,
36097775L, 36098040L, 36098987L, 36099952L, 36100767L, 36101156L,
36101586L, 36101633L, 36101926L, 36102035L, 36102381L, 36102833L,
36103149L, 36103565L, 36103872L, 36104121L, 36096300L, 36097775L,
36098040L, 36098987L, 36099840L, 36099952L, 36100767L, 36101156L,
36101633L, 36101926L, 36102035L, 36102381L, 36102833L, 36103149L,
36103565L, 36103872L, 36104121L, 36096300L, 36097775L, 36098040L,
36098987L, 36099840L, 36099952L, 36100767L, 36101156L, 36101586L,
36101633L, 36101926L, 36102035L, 36102381L, 36102833L, 36103149L,
36103872L, 36104121L)), .Names = c("A", "B"),
class = "data.frame", row.names = c(NA, -87L))

最佳答案

根据曲率,曲线将向左或向右弯曲。这意味着在这种情况下,当曲线从 A 绘制到 B 且曲率为 1 时,它将向右弯曲。因此,如果 A 小于 B,则曲线将为“负”(位于 y 轴下方),如果 A 大于 B,则曲线将为正。

使所有曲线位于同一侧的一种方法是使用两个 geom_curve,每种情况一个:

ggplot(mapping = aes(x = A, xend = B, y = 0, yend = 0)) +
geom_curve(data = subset(myDat, A > B),
alpha = 0.2, curvature = 1, ncp = 1000, lineend = 'butt') +
geom_curve(data = subset(myDat, A < B),
alpha = 0.2, curvature = -1, ncp = 1000, lineend = 'butt')

或者,当它们以错误的方式对齐时,我们可以交换它们:

myDat2 <- myDat
myDat2$A <- pmax(myDat$A, myDat$B)
myDat2$B <- pmin(myDat$A, myDat$B)

ggplot(myDat2, aes(x = A, xend = B, y = 0, yend = 0)) +
geom_curve(alpha = 0.2, curvature = 1, ncp = 1000, lineend = 'butt')

两者都会导致:

enter image description here

关于r - geom_curve - 绘制零以上的所有曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40039955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com