【问题标题】:I can't add more space between the y values我无法在 y 值之间添加更多空间
【发布时间】:2019-10-11 20:43:34
【问题描述】:

现在我正在使用这个数据库:

structure(list(year = c(1990, 1991, 1992, 1993, 1994, 1995, 1996, 
1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 
2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018
), saldo = c(8275867, 3702514, -2636805, -3665755, -5751042, 
841426, 48908, -4019329, -4943662, -2199522, 1060542, 6223146, 
16661053, 16087980, 12130455, 11699873, 12392519, 11272844, 12556386, 
16885829, 11381863, 9020415, 12008167, 1521176, 2668277.78157, 
-3419083.03045001, 2057032, -8308931, -3881619)), row.names = c(NA, 
-29L), class = c("tbl_df", "tbl", "data.frame"))

这是我绘制阿根廷贸易平衡时间序列的代码:

install.packages("ggplot2")
library(ggplot2)    

    rx <- do.call("rbind",
              sapply(1:(nrow(TB_since_1990)-1), function(i){
                f <- lm(year~saldo, TB_since_1990[i:(i+1),])
                if (f$qr$rank < 2) return(NULL)
                r <- predict(f, newdata=data.frame(saldo=0))
                if(TB_since_1990[i,]$year < r & r < TB_since_1990[i+1,]$year)
                  return(data.frame(year=r,saldo=0))
                else return(NULL)
              }))
newdata.df <- rbind(TB_since_1990, rx)
newdata.df <- newdata.df[order(newdata.df$saldo),]


color.plot <- "darkorchid4"
size.line <- 1.5

ggplot(newdata.df, aes(x=year, y=saldo, fill = "Trade balance")) + 
  geom_area(alpha = 0.6) + 
  geom_line(color= color.plot, size = size.line) +
  geom_hline(aes(yintercept = 0)) +
  labs(fill= "", title="Argentina Trade Balance",x="",y="Saldo") +
  scale_y_continuous(breaks = seq(from = -10e6, to = 20e6, by = 2.5e6), 
                     limits = c(-10e6, 20e6)) +
  scale_x_continuous(breaks = seq(from = 1990, to = 2018, by = 1), 
                     limits = c(1990, 2018)) +
  scale_fill_manual(values = c("Trade balance" = color.plot)) +
  theme_minimal() +
  theme(text = element_text(size=10), 
        axis.text.x = element_text(angle=90, hjust=1),
        plot.title = element_text(hjust = 0.5),
        legend.position = "bottom")

这是我目前的情节:

我想要这张图:

不知道怎么指出图中y轴的负值。另外,我想在 y 轴的每个值之间添加更多空间。

【问题讨论】:

  • 为什么要将轴中断设置为数据中的确切值,而不是均匀间隔的序列?
  • @camille 我的错误,我应该怎么做才能使其均匀分布在序列中?
  • 我用情节的“最终”版本编辑了我的答案。如果这对您有用,请告诉我,如果它解决了您的问题,请投票和/或接受答案

标签: r ggplot2


【解决方案1】:
library(ggplot2)
#> Registered S3 methods overwritten by 'ggplot2':
#>   method         from 
#>   [.quosures     rlang
#>   c.quosures     rlang
#>   print.quosures rlang

TB_ARG_since_1990 <- structure(list(year = c(1990, 1991, 1992, 1993, 1994, 1995, 1996, 
                        1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 
                        2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018
), saldo = c(8275867, 3702514, -2636805, -3665755, -5751042, 
             841426, 48908, -4019329, -4943662, -2199522, 1060542, 6223146, 
             16661053, 16087980, 12130455, 11699873, 12392519, 11272844, 12556386, 
             16885829, 11381863, 9020415, 12008167, 1521176, 2668277.78157, 
             -3419083.03045001, 2057032, -8308931, -3881619)), row.names = c(NA, 
                                                                             -29L), class = c("tbl_df", "tbl", "data.frame"))
ggplot(TB_ARG_since_1990,
       aes(x = year,
           y = saldo)) +
  geom_line(linetype = "solid",
            size = 1) +
  geom_area(fill = "red")

reprex package (v0.3.0) 于 2019 年 5 月 25 日创建

您可以通过修改此代码使图表更漂亮。

【讨论】:

    【解决方案2】:

    我同意@yarnabrina 的观点,geom_area 是您需要的选项。但是,ggplot 需要一些帮助才能完全满足您的要求,请参见下文。

    data.df <- structure(list(year = c(1990, 1991, 1992, 1993, 1994, 1995, 1996, 
                                   1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 
                                   2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018
    ), saldo = c(8275867, 3702514, -2636805, -3665755, -5751042, 
                 841426, 48908, -4019329, -4943662, -2199522, 1060542, 6223146, 
                 16661053, 16087980, 12130455, 11699873, 12392519, 11272844, 12556386, 
                 16885829, 11381863, 9020415, 12008167, 1521176, 2668277.78157, 
                 -3419083.03045001, 2057032, -8308931, -3881619)), row.names = c(NA, 
                                                                                 -29L), class = c("tbl_df", "tbl", "data.frame"))
    

    仅使用geom_area的图表

    ggplot(data.df, aes(x=year, y=saldo)) + 
           geom_area() + 
           geom_line(color= "red") + 
           geom_point(color= "red")
    

    我添加了一条线和点来显示您的数据。现在很明显,geom_area 在穿过 x 轴时无法正常工作。

    要解决这个问题,我们需要将 y 轴交叉点添加到数据集中。这可以通过 R 基础的 lmpredict 函数的组合来完成。

    见下面的代码来计算新的点(来自这个post

    rx <- do.call("rbind",
                  sapply(1:(nrow(data.df)-1), function(i){
                    f <- lm(year~saldo, data.df[i:(i+1),])
                    if (f$qr$rank < 2) return(NULL)
                    r <- predict(f, newdata=data.frame(saldo=0))
                    if(data.df[i,]$year < r & r < data.df[i+1,]$year)
                      return(data.frame(year=r,saldo=0))
                    else return(NULL)
                  }))
    newdata.df <- rbind(data.df, rx)
    newdata.df <- newdata.df[order(newdata.df$saldo),]
    

    并绘制图表。

    ggplot(newdata.df, aes(x=year, y=saldo)) + 
           geom_area() + 
           geom_line(color= "red") + 
           geom_point(color= "red")
    

    现在它看起来更像您正在寻找的东西。最后一件事是修改代码以使其更好。

    最终剧情

    设置一些值以便于配置(您可以找到更多颜色选项here 或使用谷歌^_^)

    color.plot <- "darkorchid4"
    size.line <- 1.5
    

    选项 1

    ggplot(newdata.df, aes(x=year, y=saldo, color = "Trade balance")) + 
      geom_area(fill = color.plot, alpha = 0.6, size = size.line) + 
      # geom_line(color= color.plot, size = size.line) +
      geom_hline(aes(yintercept = 0), size = size.line) +
      labs(color= "", title="Argentina Trade Balance",x="",y="Saldo") +
      scale_y_continuous(breaks = seq(from = -10e6, to = 20e6, by = 2.5e6), 
                         limits = c(-10e6, 20e6)) +
      scale_x_continuous(breaks = seq(from = 1990, to = 2018, by = 1), 
                         limits = c(1990, 2018)) +
      scale_color_manual(values = c("Trade balance" = color.plot)) +
      theme_minimal() +
      theme(text = element_text(size=10), 
            axis.text.x = element_text(angle=90, hjust=1),
            plot.title = element_text(hjust = 0.5),
            legend.position = "bottom")
    

    选项 2

    ggplot(newdata.df, aes(x=year, y=saldo, fill = "Trade balance")) + 
      geom_area(alpha = 0.6) + 
      geom_line(color= color.plot, size = size.line) +
      geom_hline(aes(yintercept = 0)) +
      labs(fill= "", title="Argentina Trade Balance",x="",y="Saldo") +
      scale_y_continuous(breaks = seq(from = -10e6, to = 20e6, by = 2.5e6), 
                         limits = c(-10e6, 20e6)) +
      scale_x_continuous(breaks = seq(from = 1990, to = 2018, by = 1), 
                         limits = c(1990, 2018)) +
      scale_fill_manual(values = c("Trade balance" = color.plot)) +
      theme_minimal() +
      theme(text = element_text(size=10), 
            axis.text.x = element_text(angle=90, hjust=1),
            plot.title = element_text(hjust = 0.5),
            legend.position = "bottom")
    

    回答问题编辑

    我不确定您的问题出在哪里。在我的电脑中,它运行良好,并产生了与我帖子中的最后一个类似的情节。

    我在这里添加了完整的代码来生成绘图,以防万一它可能有用,实际上除了创建初始数据框之外的所有内容都是直接复制/粘贴您问题中的代码。

    TB_since_1990 <- structure(list(year = c(1990, 1991, 1992, 1993, 1994, 1995, 1996, 
                            1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 
                            2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018
    ), saldo = c(8275867, 3702514, -2636805, -3665755, -5751042, 
                 841426, 48908, -4019329, -4943662, -2199522, 1060542, 6223146, 
                 16661053, 16087980, 12130455, 11699873, 12392519, 11272844, 12556386, 
                 16885829, 11381863, 9020415, 12008167, 1521176, 2668277.78157, 
                 -3419083.03045001, 2057032, -8308931, -3881619)), row.names = c(NA, 
                                                                                 -29L), class = c("tbl_df", "tbl", "data.frame"))
    
    rx <- do.call("rbind",
                  sapply(1:(nrow(TB_since_1990)-1), function(i){
                    f <- lm(year~saldo, TB_since_1990[i:(i+1),])
                    if (f$qr$rank < 2) return(NULL)
                    r <- predict(f, newdata=data.frame(saldo=0))
                    if(TB_since_1990[i,]$year < r & r < TB_since_1990[i+1,]$year)
                      return(data.frame(year=r,saldo=0))
                    else return(NULL)
                  }))
    newdata.df <- rbind(TB_since_1990, rx)
    newdata.df <- newdata.df[order(newdata.df$saldo),]
    
    
    color.plot <- "darkorchid4"
    size.line <- 1.5
    
    ggplot(newdata.df, aes(x=year, y=saldo, fill = "Trade balance")) + 
      geom_area(alpha = 0.6) + 
      geom_line(color= color.plot, size = size.line) +
      geom_hline(aes(yintercept = 0)) +
      labs(fill= "", title="Argentina Trade Balance",x="",y="Saldo") +
      scale_y_continuous(breaks = seq(from = -10e6, to = 20e6, by = 2.5e6), 
                         limits = c(-10e6, 20e6)) +
      scale_x_continuous(breaks = seq(from = 1990, to = 2018, by = 1), 
                         limits = c(1990, 2018)) +
      scale_fill_manual(values = c("Trade balance" = color.plot)) +
      theme_minimal() +
      theme(text = element_text(size=10), 
            axis.text.x = element_text(angle=90, hjust=1),
            plot.title = element_text(hjust = 0.5),
            legend.position = "bottom")
    

    根据您的图像,ggplot 似乎没有正确解释颜色,更重要的是,geom_area 没有按预期工作。您可能想再次安装 ggplot 或在另一台计算机上尝试该代码。祝你好运!

    【讨论】:

    • 你的解决方案肯定比我的好。但我认为最好归因于原始解决方案:stackoverflow.com/a/27137211
    • 谢谢!但我还有一个问题。我想在 y 轴的每个值之间添加更多空间(轴的每个值之间的距离非常小,看起来很乱)。所以我希望 y 的所有值都显示在图中。我想要一个整洁的图,所以我希望值 0 作为分界线出现在图表中,以明确下面的值是负数。
    • 感谢@yarnabrina 找到了带有插值代码的帖子,我很久以前就找到了,不记得了。添加到答案中。
    • @Jose Montoya 我不确定我是否理解您的问题。在您的第二张图像中,y 轴刻度是均匀分布的(最常见的)。请参阅原始问题中的 camille 评论
    • @Alfonso 嗨,我有这个问题ibb.co/3CWHCXP 情节很好,但很奇怪。
    猜你喜欢
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 2013-03-20
    • 2013-07-12
    • 2018-06-15
    相关资源
    最近更新 更多