【问题标题】:x axis is too short in R plot.defaultR plot.default中的x轴太短
【发布时间】:2015-10-11 04:09:23
【问题描述】:

我的 x 轴太短了。

d <- data.frame(x = c(120,200,300), y = rep(1,3))

 plot(d$x, 
      d$y,
      xlim = c(min(d$x), max(d$x)),
      axes = FALSE,
      xlab = "",
      ylab = "")

axis(1, lwd = 2)

显然这并不理想。我知道单个绘图实例有很多解决方案。但是,我正在以编程方式生成许多具有不同 x 值的此类图表。因此,我需要一个通用且直接适用于不同 x 值的解决方案。

下面是一些头脑风暴代码:

 #This works but the tick marks are ugly.  I also can't control lwd of axis
 plot(d$x, 
      d$y,
      yaxt="n",
      frame.plot = FALSE,
      xlab = "",
      ylab = "",
      xaxp = c(120, 300, 50)) 

 #this is the solution for this particular case, however, it would not work in general
 plot(d$x, 
      d$y,
      xlim = c(min(d$x)-20, max(d$x)),
      axes = FALSE,
      xlab = "",
      ylab = "")

axis(1, lwd = 2)    

有什么想法吗?

【问题讨论】:

  • I also can't control lwd of axis 你确定吗?这是一个相当笼统的声明。你在axis 中尝试过lwd 吗?因为你只举了一个例子,我只能给你这个with(d, plot(x, y, axes = FALSE, ann = FALSE)); axis(1, at = pretty(range(d$x)), lwd = 0, lwd.ticks = 1) 不知道这对你有什么作用

标签: r plot axis


【解决方案1】:

这是一种使用基本 R 图形的方法。逻辑类似于ggplot2 的答案。下面的代码将 x 范围设置为数据范围上下最接近的 50 倍数,并在每个 50 的倍数处放置刻度线。如果您想根据不同地块的数据范围。

xfun 计算最小和最大 x 限制。 tick.dist 设置图表中刻度线之间的距离。默认设置为 50。因此,默认情况下,which="min" 时,xfun 返回小于 value 的 50 的最大倍数。当 which="max" 时,xfun 返回大于 value 的 50 的大倍数。

xfun = function(value, which, tick.dist=50) {

  # Calculate minimum x-limit
  if(which=="min") {
    return(value - value %% tick.dist)
  }

  # Calculate maximum x-limit
  if(which=="max") {
    return(value + (tick.dist - value %% tick.dist))
  }
}

现在我们创建四个示例图。

par(mfrow=c(2,2))

# Try out various ranges for the x-values
x_vals = c(-23, 56, 80, 123)

# Set distance between tick marks
tick.dist=50

for (i in 1:length(x_vals)) {
  # Create fake data
  d <- data.frame(x=runif(10, x_vals[i], x_vals[i] + 220), y = rep(3,10))  

  # Set x limits and number of tick marks
  xmin = xfun(min(d$x), "min", tick.dist) 
  xmax = xfun(max(d$x), "max", tick.dist) 
  nticks = as.integer((xmax - xmin)/tick.dist)

  # Plot
  plot(d$x, d$y, xaxt="n", yaxt="n", frame.plot = FALSE,
       xlab = "", ylab = "", xlim=c(xmin, xmax)) 

  # xaxp controls location of min and max x-axis tick marks
  #  as well as the total number of tick marks. 
  #  See ?par("xaxp") for more info.
  axis(1, lwd = 2, xaxp = c(xmin, xmax, nticks))
}

【讨论】:

    【解决方案2】:

    这是一个使用ggplot2 进行绘图的选项。下面的代码将 x 范围设置为数据范围上下最接近的 50 倍数,并在每个 50 的倍数处放置刻度线。如果您想根据不同地块的数据范围。

    library(ggplot2)
    library(gridExtra)  # For grid.arrange function
    

    xfun 计算最小和最大 x 限制。 tick.dist 设置图表中刻度线之间的距离。默认设置为 50。因此,默认情况下,当 which="min" 时,xfun 返回小于 value 的 50 的最大倍数。当which="max" 时,xfun 返回大于value 的 50 的大倍数。

    xfun = function(value, which, tick.dist=50) {
    
      # Calculate lower x-limit
      if(which=="min") {
        return(value - value %% tick.dist)
      }
    
      # Calculate upper x-limit
      if(which=="max") {
        return(value + (tick.dist - value %% tick.dist))
      }
    }
    

    现在我们创建一个只绘制点标记和 x 轴的自定义主题:

    my_theme = list(theme_bw(),
                    theme(panel.border=element_blank(), 
                          axis.line=element_line(), 
                          axis.line.y=element_blank(),
                          panel.grid.major=element_blank(),
                          panel.grid.minor=element_blank(),
                          axis.ticks.y=element_blank(),
                          axis.text.y=element_blank()),
                    labs(y="",x=""))
    

    现在我们为四组不同的 x 值创建图,使用 xfun 设置 x 限制。

    # Try out various ranges for the x-values
    x_vals = c(-23, 56, 80, 123)
    
    # Set distance between ticks and x-axis padding
    tick.dist=50
    pad = 5
    
    # List to store the plots
    p.list=list()
    
    # Create a plot for each element of x_vals
    for (i in 1:length(x_vals)) {
    
      # Create fake data
      d <- data.frame(x=runif(10, x_vals[i], x_vals[i] + 220), y = rep(3,10))  
    
      # Set x limits
      xmin = xfun(min(d$x), "min", tick.dist) 
      xmax = xfun(max(d$x), "max", tick.dist) 
    
      # Plot
      p.list[[i]] = ggplot(d, aes(x,y)) +
        geom_point() +
        # Set the exact x-axis range
        coord_cartesian(xlim=c(xmin - pad, xmax + pad)) + 
        # Set major tick values
        scale_x_continuous(breaks=seq(-1000,1000, tick.dist)) +
        my_theme
    }
    
    # Plot all the graphs on a single "page"
    do.call(grid.arrange, p.list)
    

    【讨论】:

      猜你喜欢
      • 2013-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多