Wrapping / bending a text around a circle in plot (R)

前端 未结 2 1366
没有蜡笔的小新
没有蜡笔的小新 2021-02-13 19:25

Is there any chance to write text which is \"wrapped\" around the circle? I mean something like this:\"enter

2条回答
  •  借酒劲吻你
    2021-02-13 19:48

    Yes, and here is the code, free of charge :-) . I wrote this a while back but I don't think ever published it in any CRAN package.

    # Plot symbols oriented to local slope.
    # Interesting problem: if underlying plot has some arbitrary aspect ratio,
    # retrieve by doing: Josh O'B via SO:  
    # myasp <- with(par(),(pin[2]/pin[1])/(diff(usr[3:4])/diff(usr[1:2])))
    # so make that the default value of argument 'asp'
    # Default is 'plotx' is vector of indices at which to 
    # plot symbols.  If is_indices=FALSE, only then turn to splinefun to 
    # calculate y-values and slopes; and user beware.
    #
    # 6 Feb 2014: added default col arg so can stick in a color vector if desired
    # TODO
    #
    slopetext<-function(x,y,plotx, mytext, is_indices=TRUE, asp=with(par(), (pin[1]/pin[2])*(diff(usr[3:4])/diff(usr[1:2]))),offsetit= 0, col='black', ...) {
    if (length(x) != length(y)) stop('data length mismatch')
    if (!is.numeric(c(x,y,plotx) ) ) stop('data not numeric')
    if(is_indices) {
        # plotting at existing points.
        if(any(plotx<=1) | any(plotx>= length(x))) {
            warning("can't plot endpoint; will remove")
            plotx<-plotx[(plotx>1 & plotx x[highs] )
        intcpts <- y[highs]-slopes*x[highs]   
        ploty <- intcpts + x[plotx]*slopes
        # change name, so to speak, to simplify common plotting code
        plotx<-x[plotx]
        }else{
        #interpolating at plotx values
            if  (any(plotxmax(x)) ) {
                warning("can't plot extrapolated point; will remove")
                plotx<-plotx[(plotx>min(x) & plotx

    Edit: per David's excellent suggestion, a sample case:

    x <- 1:100
    y <- x/20 + sin(x/10)
    plot(x,y,t='l')
    slopetext(x=x,y=y,plotx=seq(10,70,by=10),mytext=letters[1:8])
    

    The third argument in this example selects every tenth value of (x,y) for placement of the text. I should warn that I haven't idiot-proofed the is_indices=FALSE case and the spline fit may in extreme cases place your text in funny ways.

    enter image description here

提交回复
热议问题