Can you easily plot rugs/axes on the top/right in ggplot2?

前端 未结 2 1878
深忆病人
深忆病人 2021-02-10 05:27

The following example has no inherent meaning... it\'s just meant to demonstrate particular placement of labels, rugs, etc. and is representative of [edited] (a) a significan

2条回答
  •  攒了一身酷
    2021-02-10 06:17

    I'll echo @Gavin's question, but for the sake of fiddling, this should get you pretty close:

    qplot(x,y) + 
        geom_segment(data = data.frame(x), aes(x = x, y = max(x) - .05, xend = x, yend = max(x))) +         #x-rug
        geom_segment(data = data.frame(x), aes(x = min(x), y = max(x), xend = max(x), yend = max(x))) +     #x-rug
        geom_segment(data = data.frame(y), aes(x = max(x) + .05, y = y, xend = max(x), yend = y)) +         #y-rug
        geom_segment(data = data.frame(y), aes(x = max(x) + .05, y = min(y), xend = max(x) + .05, yend = max(y) )) + #y-rug
        scale_x_continuous(breaks = NA) +   
        scale_y_continuous(breaks = NA) +
        xlab(NULL) +
        ylab(NULL) +
        geom_text(aes(label = round(mean(x),2), x = mean(x), y = min(y) - .2), size = 4) +
        geom_text(aes(label = round(mean(y),2), x = min(x) - .2, y = mean(y)), size = 4) + 
        geom_text(aes(label = round(max(x),2), x = max(x) + .2, y = max(y) + .2), size = 4)
        #...add other text labels to your heart's desire.
    

    If you don't need to put the rugs on the top and on the right, you can take advantage of geom_rug(). I don't know of an easy way to "move" the x or y axis away from their predefined locations. Something like this may be easier to digest / work with:

    df <- data.frame(x,y)
    qplot(x,y, data = df, geom = c("point", "rug")) # + ...any additional geom's here
    

提交回复
热议问题