【问题标题】:R Shiny dates only show in 5 year intervalsR 闪亮日期仅以 5 年为间隔显示
【发布时间】:2017-05-30 15:56:55
【问题描述】:

这是我在 R 图中使用的日期

dates <- c("2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", 
           "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017")

当我绘制图表时,这里是代码,日期仅以 5 年为间隔显示,而不是每年显示。

  plot(x = dates, y=Comp_selected_DF[idx_country, 2:18], type = "l"
         , xlab = "", ylab = "", col="grey20", ylim = c(-2, overall_max_z), lwd=3)
    mtext("Financial Vulnerability Indicator", side=3, adj=0, line=1.2, cex=2, font=2)
    mtext("Z-Score", side=3, adj=0, line=0.1, cex=1, font=0.5)
    lines(dates, Bank_selected_DF[idx_country, 2:18], col="green2", lwd=3)
    lines(dates, Curr_selected_DF[idx_country, 2:18], col="purple", lwd=3)
    lines(dates, Sover_selected_DF[idx_country, 2:18], col="red", lwd=3)
    lines(dates, Sudden_selected_DF[idx_country, 2:18], col="orange", lwd=3)
    legend(
      "bottom", 
      lty=c(1,1,1,1), 
      col=c("grey20", "green2", "purple", "red", "orange"), 
      legend = c("Composite", "Banking", "Currency", "Sovereign", "Sudden Stop")
      ,ncol=5
    )

编辑:这是我在运行您的代码时得到的。似乎传说现在已经传开了。

【问题讨论】:

  • 如果将las=2 添加到绘图函数会发生什么?

标签: r plot shiny


【解决方案1】:

在基本图形中,如果您想对轴进行精细控制,可以使用整数来绘制数据并明确设置标签。

进行了以下更改:

  • 伪造一些数据进行实验
  • 创建了一个新的整数值 x 轴范围 (1:17)
  • 使用axis 调用将dates 映射到这些值
  • 按照 Ryan Morton 的建议,使用 las=2 将 x 轴标签旋转 90 度。
  • 使用lwd 参数将图例中的线型设置为3(如lines)。

代码如下:

dates <- c("2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", 
           "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017")

# fake up some data
set.seed(123)
gendf <- function(){
  cname <- c("USA","Mexico","Canada")
  df <- data.frame(dum=1:18)
  for (i in 1:3){
    df[[cname[i]]] <- cumsum(rnorm(18,1,1))
  }
  df$dum <- NULL
  rownames(df) <- c("country",dates)
  ndf <- as.data.frame(t(df))
  ndf$country <- cname
  ndf
}
Comp_selected_DF <- gendf()
Bank_selected_DF <- gendf()
Curr_selected_DF <- gendf()
Sover_selected_DF <- gendf()
Sudden_selected_DF <- gendf()

overall_max_z <- max(Comp_selected_DF[,2:18])
idx_country <- 1


# Now plot it out
xvals <- 1:17   # our new xvalues (instead of dates)

plot(x = xvals, y=Comp_selected_DF[idx_country, 2:18], type = "l"
     , xlab = "", ylab = "", col="grey20", ylim = c(-2, overall_max_z), lwd=3,las=2)
mtext("Z-Score", side=3, adj=0, line=0.1, cex=1, font=0.5)
axis(1,at=xvals,label=dates, cex.axis=1, las=2)
mtext("Financial Vulnerability Indicator", side=3, adj=0, line=1.2, cex=2, font=2)
 lines(xvals, Bank_selected_DF[idx_country, 2:18], col="green2", lwd=3)
 lines(xvals, Curr_selected_DF[idx_country, 2:18], col="purple", lwd=3)
 lines(xvals, Sover_selected_DF[idx_country, 2:18], col="red", lwd=3)
 lines(xvals, Sudden_selected_DF[idx_country, 2:18], col="orange", lwd=3)
 legend(
   "bottom", 
   lty=c(1,1,1,1), 
   lwd=c(3,3,3,3), 
   col=c("grey20", "green2", "purple", "red", "orange"), 
   legend = c("Composite", "Banking", "Currency", "Sovereign", "Sudden Stop")
   ,ncol=5
 )

这就是它的外观:

【讨论】:

  • 还有一点,您是如何设法使图例位于图表的绘图区域下方的?在我的版本中,图例位于底部轴上,但在绘图区域内
  • 运行我提供的代码是否会得到与我发布的相同的结果?如果没有,那就奇怪了。
  • 我真的不知道。尝试更改我的代码(使用假数据),以便获得类似的东西。那将是MWE。然后将其作为问题发布 - 但在问题标题中将其称为“基础情节”。有些人的基础情节比我好得多(我更像是ggplot 人)。
  • 好的,谢谢。似乎很奇怪。当应用程序打开时,它看起来像我的问题中的上图,但是当我更改应用程序中的下拉列表以重新填充图表时,它会返回到显示在绘图区域中间的图例。正如您所说,您更像是一个 ggplot 人,您认为这张图表作为 ggplot 会更好看吗?如果是这样,翻译代码以使用 ggplot 而不是基本图有多难。
  • 不难,但我今天没时间做。你也可以问这个问题——有很多人会热切地这样做——只要你给他们工作数据和一些已经有效的东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-23
  • 2017-01-30
  • 2019-11-22
  • 2021-09-02
  • 2018-05-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多