缩放散点图的 X 和 Y 轴以填充整个轴

时间:2021-04-16 15:28:43

标签: r ggplot2 scatter-plot

我之前在上一篇文章中问过这个问题。我明白有些事情并没有我想象的那么清楚。因此,我创建了一个 100% 功能性复制代码,它应该可以说明为什么 Function{} 中的数据不能被更改(如上一篇文章中所建议的那样)。

复制代码(只需复制、粘贴和执行,您就会得到三个图形):

df <- data.frame(
                            ID = c(1L,1L,
                                   1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,
                                   1L,1L,1L,1L,1L,1L,1L,1L,2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,2L,3L,3L,3L,
                                   3L,3L,3L,3L,3L,3L,3L),
                             x = as.integer(c(14400L,
                                   13400L,12600L,11600L,10500L,9520L,8540L,
                                   7710L,6580L,4540L,3710L,2880L,1440L,
                                   0L,-10000L,-20900L,-31800L,-42700L,
                                   -53600L,-64500L,-75400L,165000L,74300L,62800L,52600L,35000L,
                                   22200L,6090L,0L,-10000L,-16500L,-23000L,-29500L,-36000L,
                                   -42500L,-49000L,97900L,
                                   51100L,22900L,4410L,0L,-5000L,
                                   -7280L,-9560L,-11840L,
                                   -14120L)),
                          U.x. = c(1,
                                   0.923076923,0.846153846,0.769230769,
                                   0.692307692,0.615,0.538,0.462,0.385,0.308,0.231,
                                   0.154,0.077,0,-2.723,-3.134,-9.393,
                                   -34.664,-58.576,-188.332,-307.888,1,0.857,0.714,0.571,0.429,0.286,0.143,
                                   0,-0.091,-0.238,-0.265,-0.447,-0.472,-0.507,-0.574,1,0.75,
                                   0.5,0.25,0,-0.142,-0.193,
                                   -0.322,-0.56,-0.755))

ID = c(0) #Needs to be set before starting the function, otherwise the first plot wont be generated

plot_utility <- function(ID) {
  x = df$x[df$ID==ID]
  y = df$U.x.[df$ID==ID]
  z = df$ID[df$ID==ID]
  df1 = data.frame(df$ID[df$ID==ID], df$U.x.[df$ID==ID],
                   df$x[df$ID==ID])
  # Set boundaries and stepsize for x-axis
  lowboundx <- df[df$ID==ID,][nrow(df[df$ID==ID,]), 2]
  upboundx <- df[df$ID==ID,][1, 2]
  
  # Determine sequence for x-axis with respectively lower bound (seq1) and upper bound (seq2) 
  seq1x <- floor(lowboundx / 5000) * 5000
  seq2x <- ceiling(upboundx / 5000) * 5000
  
  # Set boundaries and stepsize for y-axis
  lowboundy <- df[df$ID==ID,][nrow(df[df$ID==ID,]), 3]
  upboundy <- df[df$ID==ID,][1, 3]
  
  # Determine sequence for x-axis with respectively lower bound (seq1) and upper bound (seq2) 
  if(lowboundy <= -55) {
    seq1y <- floor(lowboundy / 50) * 50
    seq2y <- upboundy # This is always 1
  } else {
    seq1y <- round(lowboundy, digits = 3)#ceiling(lowboundy / 50) * 50
    seq2y <- upboundy # This is always 1
  }
  
ggplot(df1, aes(x = x, y = y)) + 
  geom_point(shape=15) +
  geom_hline(yintercept = 0, linetype="dashed", color = "red") +
  geom_vline(xintercept = 0, linetype="dashed", color = "red") +
  scale_x_continuous(name="Euro", limits = c(seq1x, seq2x), labels = comma) +
  scale_y_continuous(name="U(x)", limits = c(seq1y, seq2y)) +
  labs(title = paste("Subject", ID))
  
}

repeat {
  ID = ID + 1
  print(plot_utility(ID))
  print(ID)
  if (ID == 3){
    break
  }
}

我的问题是什么:

如主题 1 所示:

在图表的右侧,x 轴以 0 结尾,但在此 0 之后仍有点。x 轴需要扩展到 20,000

如主题 2 所示

这是一个比其他例子更难的例子,但原理是一样的。在 x 轴上的标签外有一个点。需要有一个额外的标签,上面写着 200,000。这听起来有点极端,但我更喜欢当前图表

如主题 3 所示

在左下角(y 轴和 x 轴在一起的地方),轴上的标签外有一些点(-0.5 和 0),我需要将其解析为额外标签​​(-1.0 和 - 25,000)这将是一个额外的断点(我想这就是所谓的) y 轴应该看起来像这样(从下到上): -1.0, -0.5, 0.0, 0.5, 1.0 x 轴应如下所示(从左到右): -25,000 | 0 | 25,000 | 50,000 | 75,000 | 100,000

值得注意的是,无法通过 limits = 处的 scale_x_continuous 更改此设置,因为这也会使所有其他图形发生变异,而这并不是真正需要的。

我的问题的任何解决方案将不胜感激! :) (请注意:data.frame 被逆向工程以创建此示例,请不要注意代码中不必要的值命名:))

1 个答案:

答案 0 :(得分:0)

这是一个使用 facet_grid() 进行分面的案例。如果您需要单独的图,那么这不是适合您的解决方案。当您对情节进行分面时,您可以设置 scales = "free" 来完成您想要的结局。另请注意,此方法不需要如此复杂的代码。

ggplot(df, aes(x = x, y = U.x.)) +
  geom_point(shape=15) +
  geom_hline(yintercept = 0, linetype="dashed", color = "red") +
  geom_vline(xintercept = 0, linetype="dashed", color = "red") +
  scale_x_continuous(name="Euro") +
  scale_y_continuous(name="U(x)") +
  facet_grid(rows = vars(ID), scales = "free", labeller = label_both)

enter image description here

相关问题