Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PLotly chart with 2 y-axes display 2 x-axes as well one over the other

I have created a plotly chart with 2 y-axes. Im wondering why they seem to have 2 x-axes though. Its like one is over the other.

    df2<-structure(list(year = c(1951, 1954, 1955, 1957, 1958, 1960, 1961, 
    1962, 1963, 1964, 1965, 1967, 1968, 1969, 1970, 1971, 1972, 1973, 
    1974, 1975, 1976, 1977, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 
    1986, 1988, 1989, 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, 2019), 
        Scope = c(4, 3, 5, 27, 6, 13, 6, 6, 24, 1, 21, 24, 32, 57, 
        18, 1, 43, 39, 8, 58, 68, 78, 7, 20, 26, 4, 12, 19, 8, 37, 
        35, 51, 209, 478, 395, 355, 453, 457, 262, 148, 196, 180, 
        367, 283, 360, 414, 175, 330, 314, 446, 428, 163, 213, 165, 
        307, 231, 53, 189, 18, 104, 39), count = c(1, 1, 1, 1, 2, 
        4, 1, 2, 2, 1, 3, 2, 3, 6, 4, 1, 9, 6, 3, 4, 5, 5, 2, 3, 
        2, 2, 2, 4, 3, 3, 5, 5, 14, 26, 18, 25, 23, 26, 19, 13, 12, 
        13, 20, 22, 24, 25, 14, 21, 15, 22, 18, 8, 13, 6, 11, 9, 
        5, 5, 1, 3, 1), fiveYears = c("1951-1955", "1951-1955", "1951-1955", 
        "1957-1961", "1957-1961", "1957-1961", "1957-1961", "1962-1965", 
        "1962-1965", "1962-1965", "1962-1965", "1967-1971", "1967-1971", 
        "1967-1971", "1967-1971", "1967-1971", "1972-1977", "1972-1977", 
        "1972-1977", "1972-1977", "1972-1977", "1972-1977", "1979-1982", 
        "1979-1982", "1979-1982", "1979-1982", "1983-1986", "1983-1986", 
        "1983-1986", "1983-1986", "1988-1992", "1988-1992", "1988-1992", 
        "1988-1992", "1993-1998", "1993-1998", "1993-1998", "1993-1998", 
        "1993-1998", "1993-1998", "1999-2003", "1999-2003", "1999-2003", 
    "1999-2003", "1999-2003", "2004-2008", "2004-2008", "2004-2008", 
        "2004-2008", "2004-2008", "2009-2013", "2009-2013", "2009-2013", 
        "2009-2013", "2009-2013", "2014-2019", "2014-2019", "2014-2019", 
        "2014-2019", "2014-2019", "2014-2019"), Scope_min = c(3, 
        3, 3, 6, 6, 6, 6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 8, 8, 8, 8, 
        8, 8, 4, 4, 4, 4, 8, 8, 8, 8, 35, 35, 35, 35, 148, 148, 148, 
        148, 148, 148, 180, 180, 180, 180, 180, 175, 175, 175, 175, 
        175, 163, 163, 163, 163, 163, 18, 18, 18, 18, 18, 18), Scope_max = c(5, 
        5, 5, 27, 27, 27, 27, 24, 24, 24, 24, 57, 57, 57, 57, 57, 
        78, 78, 78, 78, 78, 78, 26, 26, 26, 26, 37, 37, 37, 37, 478, 
        478, 478, 478, 457, 457, 457, 457, 457, 457, 367, 367, 367, 
        367, 367, 446, 446, 446, 446, 446, 428, 428, 428, 428, 428, 
        231, 231, 231, 231, 231, 231)), class = c("tbl_df", "tbl", 
    "data.frame"), row.names = c(NA, -61L))

old.y <- list(
  side = "left",
  title = "Scope"
)

new.y <- list(
  overlaying = "y",
  side = "right",
  title = "Count",
  range = c(0,500)
)

plot_ly(df2) %>%
  add_markers (x = ~fiveYears, y = ~Scope, yaxis="y1") %>%
  add_markers(x = ~fiveYears, y = ~count, yaxis = "y2") %>%
  layout(yaxis2 = new.y, yaxis = old.y, xaxis = list(title="Periods"))

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Imposing the same axis bounds will remove the line (or rather move it down to be conterminous with the first).

old.y <- list(
  side = "left",
  title = "Scope", 
  range= c(0,500)
)

new.y <- list(
  overlaying = "y",
  side = "right",
  title = "Count",
  range = c(0,500)
)

plot_ly(df2) %>%
  add_markers (x = ~fiveYears, y = ~Scope, yaxis="y1") %>%
  add_markers(x = ~fiveYears, y = ~count, yaxis = "y2") %>%
  layout(yaxis2 = new.y, yaxis = old.y, xaxis = list(title="Periods"))

enter image description here

Just as a suggestion, you could even relabel the axis ticks and rescale the count variable so the counts take up the whole plotting region.

old.y <- list(
  side = "left",
  title = "Scope", 
  range= c(0,500)
)

new.y <- list(
  overlaying = "y",
  side = "right",
  title = "Count",
  range = c(0,500), 
  tickvals = c(0,100,200,300,400,500), 
  ticktext = c(0,6,12,18,24,30)
  
)

plot_ly(df2) %>%
  add_markers (x = ~fiveYears, y = ~Scope, yaxis="y1") %>%
  add_markers(x = ~fiveYears, y = ~count*16.6667, yaxis = "y2") %>%
  layout(yaxis2 = new.y, yaxis = old.y, xaxis = list(title="Periods"))

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading