X轴扩展到动画折线图中的图形窗口之外[plotly] [r]

时间:2018-10-29 15:54:12

标签: r layout plotly

我正在创建一个动画折线图,显示一定时间段内IMR的趋势。

当我执行单年趋势时,图形会正确调整大小并适合图形窗口。

我遇到麻烦的地方是当我进行五年滚动平均值计算时。我认为标签会导致X轴超出图形窗口的范围。我可以通过先运行图形然后调整大小来解决此问题,但是如果我需要将其发送给外部参与者,那就无法达到目的。此expanded axis在此处显示。我还将其导出到html小部件中,以在RStudio外部进行检查,并以相同的问题结束。

所以,我想知道是否精通plot的人们可能对如何使xaxis相应地运行有所了解。代码如下。

包裹是密谋的/ dplyr。

数据

structure(list(yr2 = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 
21L, 22L, 23L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 
20L, 21L, 22L, 23L), .Label = c("1991-1995", "1992-1996", "1993-1997", 
"1994-1998", "1995-1999", "1996-2000", "1997-2001", "1998-2002", 
"1999-2003", "2000-2004", "2001-2005", "2002-2006", "2003-2007", 
"2004-2008", "2005-2009", "2006-2010", "2007-2011", "2008-2012", 
"2009-2013", "2010-2014", "2011-2015", "2012-2106", "2013-2017"
), class = "factor"), raceth = structure(c(2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 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, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1. All", "2. White, NH", 
"3. Black, NH", "4. Hispanic"), class = c("ordered", "factor"
)), rates = c(7.2, 7, 6.8, 6.7, 6.7, 6.8, 6.6, 6.6, 6.4, 6.3, 
6.3, 6.3, 6.4, 6.4, 6.3, 6, 5.8, 5.5, 5.2, 5, 5, 5, 4.9, 19.9, 
20.5, 19.4, 16.6, 16.5, 15.2, 14.5, 14.3, 15.3, 15.6, 16.6, 16.2, 
17.1, 16.7, 16.5, 15.6, 14.7, 13.6, 13.9, 12.9, 12.6, 13.1, 12.6, 
7.8, 6.6, 6.7, 6.5, 5.9, 6.1, 6.5, 6.5, 6.9, 7.1, 7.5, 7.2, 7.4, 
7.4, 7.5, 7.3, 7.4, 7.5, 7.2, 7.6, 7.5, 7.2, 6.9, 8.2, 8, 7.8, 
7.4, 7.3, 7.3, 7.1, 7.1, 7, 7, 7.2, 7.1, 7.3, 7.4, 7.4, 7.1, 
6.9, 6.6, 6.4, 6.3, 6.2, 6.1, 6.1)), class = "data.frame", row.names = c(NA, 
-92L))

创建框架

accumulate_by <- function(dat, var) {
    var <- lazyeval::f_eval(var, dat)
    lvls <- plotly:::getLevels(var)
    dats <- lapply(seq_along(lvls), function(x) {
            cbind(dat[var %in% lvls[seq(1, x)], ], frame = lvls[[x]])
    })
    dplyr::bind_rows(dats)
}

object2 <- im_rate2 


d2 <- object2 %>%
    accumulate_by(~yr2)

创建图形

p2 <-  d2 %>%
    plot_ly(
            x = ~yr2, 
            y = ~rates,
            split = ~raceth,
            frame = ~frame, 
            type = 'scatter',
            mode = 'lines',
            line = list(simplyfy = F)
    ) %>% 
    layout(xaxis = list(
            title = "Period",
            zeroline = F,
            type ='category',
            tickangle=45

    ),
    yaxis = list(
            title = "Infant Mortality Rate (per 1,000 live births)",
            zeroline = F,
            hoverformat ='.1f'
    )
    ) %>% 
    animation_opts(
            frame = 500, 
            transition = 500, 
            redraw = FALSE
    ) %>%
    animation_slider(
            hide = F
    ) %>%
    animation_button(
            x = 1, xanchor = "right", y = 0, yanchor = "bottom"
    )

0 个答案:

没有答案