Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the layout in the updated version of plotly?

Tags:

r

plotly

I'm trying to plot a simple graphic using the new version of plotly but I keep getting the following error

Error in UseMethod("layout") : no applicable method for 'layout' applied to an object of class "character"

This is how my code looks like:

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)

The layout command isn't working, if someone knows why and what should I do I would appreciate the help!

Thank you!

like image 500
LauraP Avatar asked Oct 20 '25 13:10

LauraP


2 Answers

You can try add %>% between plot_ly and layout.

like image 75
Gaonan Zhang Avatar answered Oct 22 '25 02:10

Gaonan Zhang


To be honest, I am not very advanced in the whole plotly package. I can't tell you why it isn't working (I simply don't know), but I changed your code and I got an output!

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)

I'm sorry I can't help you with an explanation, but I hope this helps a bit!

like image 35
Hav11 Avatar answered Oct 22 '25 02:10

Hav11