缩放散点图的X和Y轴以填充整个轴 [英] Scale the X and Y axis of a scatterplot to fill the whole axis

查看:52
本文介绍了缩放散点图的X和Y轴以填充整个轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在上一篇文章中曾问过这个问题.我知道有些事情并没有我想的那么清楚.因此,我创建了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,3公升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)#需要在启动功能之前进行设置,否则将不会生成第一张图plot_utility<-函数(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])#设置x轴的边界和步长lowboundx<-df [df $ ID == ID,] [nrow(df [df $ ID == ID,]),2]upboundx<-df [df $ ID == ID,] [1、2]#确定x轴的顺序,分别具有下限(seq1)和上限(seq2)seq1x<-下限(lowboundx/5000)* 5000seq2x<-上限(upboundx/5000)* 5000#设置y轴的边界和步长低边界<-df [df $ ID == ID,] [nrow(df [df $ ID == ID,]),3]上界<-df [df $ ID == ID,] [1,3]#确定x轴的顺序,分别具有下限(seq1)和上限(seq2)if(lowboundy< = -55){seq1y<-地板(低边界/50)* 50seq2y<-upboundy#始终为1} 别的 {seq1y<-回合(lowboundy,digits = 3)#ceiling(lowboundy/50)* 50seq2y<-upboundy#始终为1}ggplot(df1,aes(x = x,y = y))+geom_point(形状= 15)+geom_hline(yintercept = 0,线型=虚线",颜色=红色")+geom_vline(xintercept = 0,线型=虚线",颜色=红色")+scale_x_continuous(name ="Euro",限制= c(seq1x,seq2x),标签=逗号)+scale_y_continuous(name ="U(x)",限制= c(seq1y,seq2y))+实验室(标题=粘贴(主题",ID))}重复 {ID = ID + 1打印(plot_utility(ID))打印(ID)如果(ID == 3){休息}} 

我的问题是什么

如主题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.0x轴应看起来像(从左到右):-25,000 |0 |25,000 |50,000 |75,000 |100,000

值得注意的是,不可能通过 scale_x_continuous limits = 来更改此设置,因为这也会使所有其他图形发生变异,这并不是真正需要的./p>

任何解决我的问题的方法将不胜感激!:)(请注意:为创建此示例,对data.frame进行了反向工程,请不要在代码中进一步注意不必要的值命名:))

解决方案

以下是使用 facet_grid()进行构面的情况.如果您需要单独的图,那么这不是您的解决方案.当刻面图时,您可以设置 scales ="free" 以完成所需的目标.还要注意,这种方法不需要如此复杂的代码.

  ggplot(df,aes(x = x,y = U.x.))+geom_point(形状= 15)+geom_hline(yintercept = 0,线型=虚线",颜色=红色")+geom_vline(xintercept = 0,线型=虚线",颜色=红色")+scale_x_continuous(name ="Euro")+scale_y_continuous(name =``U(x)'')+facet_grid(行= vars(ID),比例=免费",贴标签=贴标签) 

I asked this question before in a previous post. I understood that some things were not as clearly mentioned as I thought. Therefore I have created an 100% functional reproductional code which should show why the data within Function{} cannot be changed (as suggested in prevrious post).

Reproductional code (just copy, paste and execute and you'll get three graphs):

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
  }
}

What is my issue:

As shown in Subject 1:

On the right hand side of the graph the x axis ends with 0, but there are still dots after this 0. The x axis needs to be extended to 20,000

As shown in Subject 2

This is a more difficult example as the others but the principle is the same. There is a dot outside of an label on the x axis. There needs to be an extra label which says 200,000. This sounds a bit extreme but I prefer that over the current graph

As shown in Subject 3

In the left down corner (where y and x axis come together), there are dots outside of the labels on the axis (-0.5 and 0) I need this to get resolved into something like extra labels (-1.0 and -25,000) These would be an additional break point (i guess thats what it is called) The y axis should look something like this (from below to up): -1.0, -0.5, 0.0, 0.5, 1.0 The x axis should look like (from left to right): -25,000 | 0 | 25,000 | 50,000 | 75,000 | 100,000

As noticable, it is not possible to change this through limits = at scale_x_continuous as this would mutate all of the other graphs as well which is not realy desired.

Any solution to my problem would be appreciated a lot! :) (Please note: the data.frame was reverse engineerd in order to create this example, please don't pay attention to the unnecessary naming of values further up in the code :))

解决方案

Here is a case for faceting using facet_grid(). If you need individual plots, then this is not the solution for you. When you facet a plot, you can set scales = "free" to accomplish your desired end. Note also that this approach does not need such complex code.

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)

这篇关于缩放散点图的X和Y轴以填充整个轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆