如何在更新版本的情节中设置布局?

时间:2016-10-26 16:46:07

标签: r plotly

我试图使用新版本的情节绘制一个简单的图形,但我一直收到以下错误

  

UseMethod中的错误(" layout"):没有适用于' layout'的方法   应用于类"字符"

的对象

这就是我的代码的样子:

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(11, 16, 21, 27, 33, 37, 43, 54, 68, 94, 128, 170, 213, 258, 307, 348, 385)

data <- data.frame(year, amt)

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

xaxis <- 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)'))

yaxis <- 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 = layout.xaxis,
   yaxis = layout.yaxis)

布局命令不起作用,如果有人知道为什么以及我该怎么做,我将不胜感激!

谢谢!

3 个答案:

答案 0 :(得分:3)

您可以尝试在%>%plot_ly之间添加layout

答案 1 :(得分:2)

说实话,我在整个情节包中并不是很先进。我不能告诉你为什么它不起作用(我根本就不知道),但是我改变了你的代码,我得到了一个输出!

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(11, 16, 21, 27, 33, 37, 43, 54, 68, 94, 128, 170, 213, 258, 307, 348, 385)

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)

对不起,我无法帮你解释,但我希望这有点帮助!

答案 2 :(得分:0)

这是上述%>%管道的简单示例

p <- plot_ly(
  type = "scatter",
  data = vep_wes_aff_rare_summary, 
  x = ~n, 
  y = ~mean_af
)%>%
layout(
  title = 'Styled Scatter',
  yaxis = list(zeroline = FALSE),
  xaxis = list(zeroline = FALSE)
)