缩放散点图的 X 和 Y 轴以填充整个轴
Posted
技术标签:
【中文标题】缩放散点图的 X 和 Y 轴以填充整个轴【英文标题】:Scale the X and Y axis of a scatterplot to fill the whole axis 【发布时间】:2021-07-11 15:30:27 【问题描述】:我在之前的帖子中问过这个问题。我明白有些事情并没有我想的那么清楚。因此,我创建了一个 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轴需要延长到20000
如主题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】:这是一个使用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)
【讨论】:
谢谢,但我需要将这些图表彼此分开。我知道它不需要那么复杂,但我不知道在创建 28 个不同的图表时创建比这更容易的东西。除此之外,还有一些图表的 x 值(或标签)超过 700 万。这真的会与其他图表混淆,因为它正在按固定值进行缩放,对吧?以上是关于缩放散点图的 X 和 Y 轴以填充整个轴的主要内容,如果未能解决你的问题,请参考以下文章