【问题标题】:Remove plot axis values删除绘图轴值
【发布时间】:2010-11-12 08:37:22
【问题描述】:

我只是想知道是否有办法在 r-plot 图中摆脱轴值,分别是 x 轴或 y 轴。

我知道axes = false 会去掉整个轴,但我只想去掉编号。

【问题讨论】:

    标签: r plot axis-labels


    【解决方案1】:

    移除 x 轴或 y 轴上的编号:

    plot(1:10, xaxt='n')
    plot(1:10, yaxt='n')
    

    如果您也想删除标签:

    plot(1:10, xaxt='n', ann=FALSE)
    plot(1:10, yaxt='n', ann=FALSE)
    

    【讨论】:

    • 但请记住,这些会删除整个轴...除非您使用 bty 设置将线放在轴所在的位置,否则那里将一无所有。默认值为 bty = 'o' ,因此通常在坐标轴所在的绘图周围会有一个框。但是使用 bty = 'n' 时只会有一些点漂浮在空间中。
    • @RichieCotton 的下一个答案更好
    【解决方案2】:

    使用基本图形,执行此操作的标准方法是使用 axes=FALSE,然后使用 Axis(或轴)创建您自己的轴。例如,

    x <- 1:20
    y <- runif(20)
    plot(x, y, axes=FALSE, frame.plot=TRUE)
    Axis(side=1, labels=FALSE)
    Axis(side=2, labels=FALSE)
    

    格的等价物是

    library(lattice)
    xyplot(y ~ x, scales=list(alternating=0))
    

    【讨论】:

    • 加一个格子解释!
    【解决方案3】:

    @Richie Cotton 上面有一个很好的答案。我只能补充一点,page 提供了一些示例。请尝试以下操作:

    x <- 1:20
    y <- runif(20)
    plot(x,y,xaxt = "n")
    axis(side = 1, at = x, labels = FALSE, tck = -0.01)
    

    【讨论】:

      【解决方案4】:

      你也可以在情节中放置标签:

      plot(spline(sub$day, sub$counts), type ='l', labels = FALSE)
      

      您会收到警告。我认为这是因为标签实际上是一个参数,它被传递给绘图运行的子程序(轴?)。警告会弹出,因为它不是绘图函数的直接参数。

      【讨论】:

        【解决方案5】:

        更改轴颜色以匹配背景,如果您正在动态修改背景,则需要同时更新轴颜色。 * 共享图为使用mock数据的graph/plot示例()

        ### Main Plotting Function ###
        plotXY <- function(time, value){
        
            ### Plot Style Settings ###
        
            ### default bg is white, set it the same as the axis-colour 
            background <- "white"
        
            ### default col.axis is black, set it the same as the background to match
            axis_colour <- "white"
        
            plot_title <- "Graph it!"
            xlabel <- "Time"
            ylabel <- "Value"
            label_colour <- "black"
            label_scale <- 2
            axis_scale <- 2
            symbol_scale <- 2
            title_scale <- 2
            subtitle_scale <- 2
            # point style 16 is a black dot
            point <- 16 
            # p - points, l - line, b - both
            plot_type <- "b"
        
            plot(time, value, main=plot_title, cex=symbol_scale, cex.lab=label_scale, cex.axis=axis_scale, cex.main=title_scale, cex.sub=subtitle_scale, xlab=xlabel, ylab=ylabel, col.lab=label_colour, col.axis=axis_colour, bg=background, pch=point, type=plot_type)
        }
        
        plotXY(time, value)
        

        【讨论】: