gpt4 book ai didi

r - ggplot2:轴上的花括号?

转载 作者:行者123 更新时间:2023-12-03 20:54:14 25 4
gpt4 key购买 nike

answering a recent visualization问题我真的需要大括号来显示轴上的跨度,但我不知道如何在 ggplot2 中做到这一点。这是情节:

example plot

而不是刻度线,我真的希望 y 轴标签“双字母名称的第二个字母”有一个从 1 到 10(红色和蓝色第二个字母的垂直跨度)延伸的大括号。但我不确定如何做到这一点。 x 轴可以从类似的处理中受益。

代码在链接的 CrossValidated 问题中可用(并且对于这个示例来说不必要地复杂,所以我不会展示它)。相反,这是一个最小的例子:

library(ggplot2)
x <- c(runif(10),runif(10)+2)
y <- c(runif(10),runif(10)+2)
qplot(x=x,y=y) +
scale_x_continuous("",breaks=c(.5,2.5),labels=c("Low types","High types") )

minimal example

在这种情况下,从 (0,1) 为低类型和从 (2,3) 为高类型的大括号将是理想的,而不是刻度线。

我宁愿不使用 geom_rect因为:
  • 刻度线将保留
  • 我更喜欢牙套
  • 它将在情节内部而不是外部

  • 我将如何做到这一点?完美的答案应该是:
  • 漂亮、光滑、纤细的花括号
  • 绘制在绘图区之外
  • 通过高级参数指定(理想情况下是传递给 breaks 中的 scale_x_continuous 选项的范围类型对象)
  • 最佳答案

    使用绘制大括号的函数的另一种解决方案。

    谢谢古尔!

    curly <- function(N = 100, Tilt = 1, Long = 2, scale = 0.1, xcent = 0.5,
    ycent = 0.5, theta = 0, col = 1, lwd = 1, grid = FALSE){

    # N determines how many points in each curve
    # Tilt is the ratio between the axis in the ellipse
    # defining the curliness of each curve
    # Long is the length of the straight line in the curly brackets
    # in units of the projection of the curly brackets in this dimension
    # 2*scale is the absolute size of the projection of the curly brackets
    # in the y dimension (when theta=0)
    # xcent is the location center of the x axis of the curly brackets
    # ycent is the location center of the y axis of the curly brackets
    # theta is the angle (in radians) of the curly brackets orientation
    # col and lwd are passed to points/grid.lines

    ymin <- scale / Tilt
    y2 <- ymin * Long
    i <- seq(0, pi/2, length.out = N)

    x <- c(ymin * Tilt * (sin(i)-1),
    seq(0,0, length.out = 2),
    ymin * (Tilt * (1 - sin(rev(i)))),
    ymin * (Tilt * (1 - sin(i))),
    seq(0,0, length.out = 2),
    ymin * Tilt * (sin(rev(i)) - 1))

    y <- c(-cos(i) * ymin,
    c(0,y2),
    y2 + (cos(rev(i))) * ymin,
    y2 + (2 - cos(i)) * ymin,
    c(y2 + 2 * ymin, 2 * y2 + 2 * ymin),
    2 * y2 + 2 * ymin + cos(rev(i)) * ymin)

    x <- x + xcent
    y <- y + ycent - ymin - y2

    x1 <- cos(theta) * (x - xcent) - sin(theta) * (y - ycent) + xcent
    y1 <- cos(theta) * (y - ycent) + sin(theta) * (x - xcent) + ycent

    ##For grid library:
    if(grid){
    grid.lines(unit(x1,"npc"), unit(y1,"npc"),gp=gpar(col=col,lwd=lwd))
    }

    ##Uncomment for base graphics
    else{
    par(xpd=TRUE)
    points(x1,y1,type='l',col=col,lwd=lwd)
    par(xpd=FALSE)
    }

    }


    library(ggplot2)
    x <- c(runif(10),runif(10)+2)
    y <- c(runif(10),runif(10)+2)
    qplot(x=x,y=y) +
    scale_x_continuous("",breaks=c(.5,2.5),labels=c("Low types","High types") )

    curly(N=100,Tilt=0.4,Long=0.3,scale=0.025,xcent=0.2525,
    ycent=par()$usr[3]+0.1,theta=-pi/2,col="red",lwd=2,grid=TRUE)
    curly(N=100,Tilt=0.4,Long=0.3,scale=0.025,xcent=0.8,
    ycent=par()$usr[3]+0.1,theta=-pi/2,col="red",lwd=2,grid=TRUE)

    result plot

    关于r - ggplot2:轴上的花括号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61425825/

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