如何修复我的阴谋图形y轴上的错位?

时间:2016-10-27 18:07:51

标签: r plotly yaxis

这就是我的代码现在的样子:

library(plotly)
library(RColorBrewer)

year <- c(1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006)
amt <- c(11000, 16000, 21000, 27000, 33000, 37000, 43000, 54000, 68000, 94000, 128000, 170000, 213000, 258000, 307000, 348000, 385000)

data <- data.frame(year, amt)

data$year <- factor(data$year, levels = data[["year"]])

a <- list(title = "Years",
      showline = TRUE,
      showgrid = FALSE,
      showticklabels = TRUE,
      linecolor = 'black',
      linewidth = 1,
      autotick = FALSE,
      ticks = 'outside',
      tickcolor = 'black',
      tickwidth = 2,
      ticklen = 5,
      tickfont = list(family = 'Cambria',
                      size = 10,
                      color = 'rgb(82, 82, 82)'))

b <- list(title = "Years",
      showline = TRUE,
      showgrid = FALSE,
      showticklabels = TRUE,
      linecolor = 'black',
      linewidth = 1,
      autotick = FALSE,
      ticks = 'outside',
      tickcolor = 'black',
      tickwidth = 2,
      ticklen = 5,
      tickfont = list(family = 'Cambria',
                      size = 10,
                      color = 'rgb(82, 82, 82)'))


plot_ly(data, x = ~year,
y = ~amt,
name= '',
type='scatter',
mode = 'lines+markers',
line = list(color = toRGB('#964f4d')),
marker = list(color = toRGB("#964f4d"))) %>%
layout(title = 'Pre-purchase financing poll',
xaxis = a,
yaxis = b)

但是y轴的每个刻度都显示在0标记处。有没有办法改变滴答声的分布范围?图形本身看起来很好。谢谢!

1 个答案:

答案 0 :(得分:0)

您需要:

  • 指定autotick = TRUE
  • 或手动指定tickvalticktext,如下所示:

    b <- list(title = "Years",
          showline = TRUE,
          showgrid = FALSE,
          showticklabels = TRUE,
          linecolor = 'black',
          linewidth = 1,
          tickval = amt,
          ticktext = amt,
          ticks = 'outside',
          tickcolor = 'black',
          tickwidth = 2,
          ticklen = 5,
          tickfont = list(family = 'Cambria',
                          size = 10,
                          color = 'rgb(82, 82, 82)'))