R plot.default中的x轴太短

时间:2015-10-11 04:09:23

标签: r plot axis

我的x轴太短了。

My Plot

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)    

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

这是使用基础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))
}

enter image description here

答案 1 :(得分:1)

这是一个使用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=""))

现在我们使用xfun创建四个不同x值集的图,以设置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)

enter image description here