Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly - Publishing my plot with R

Tags:

r

ggplot2

plotly

I have a Plotly account and an R script creating a plot I want to publish on my Plotly page. After looking at the Plotly Getting Started page and some other online advice, I ended up with this code.

library(XLConnect)
library(dplyr)
library(ggplot2)
library(devtools)
library(plotly)

##Preparing the data file
myurl <-     "http://minagric.gr/images/stories/docs/agrotis/Elia/egekrimenes_typop_monades220715.xls"
temp <- file.path(getwd(), "temp.xls")
download.file(myurl,temp)
mydata <- readWorksheetFromFile(temp, sheet=1, header = TRUE, startRow = 2)
colnames(mydata)<-c("name", "approval_code", "region", "prefecture")

plot_region <- ggplot(mydata,aes(x=region)) + 
      geom_histogram(fill="lightblue") +
      theme(plot.title = element_text(size=15, face="bold", vjust=2),     axis.text.x=element_text(angle=50, size=5, vjust=0.35)) +
      labs(title = "Olive oil plants per Region") + 
      xlab("Region") + ylab("Number of plants")
py <- plotly(username = "myusername", key = "mykey")    
response<-py$ggplotly(plot_region)

However, this error presents itself when I execute the code:

Error in eval(expr, envir, enclos) : attempt to apply non-function
In addition: Warning messages:
1: 'plotly' is deprecated.
Use 'ggplotly' instead.
See help("Deprecated") 
2: 'plotly' is deprecated.
Use 'plot_ly' instead.
See help("Deprecated") 

How can I solve this and publish my plots? I have also tried the following:

ggplotly(plot_region)

but it presents this error:

Error in `[[<-.data.frame`(`*tmp*`, a, value = c("ΣΤΕΡΕΑ ΕΛΛΑΔΑ",  : 
  replacement has 483 rows, data has 13
like image 575
lios Avatar asked Oct 19 '22 03:10

lios


1 Answers

Your ggplotly syntax is correct, it looks like the conversion from ggplot to plotly failed for this case. I've reported the bug here: https://github.com/ropensci/plotly/issues/278

An alternative solution is to create the graph with plotly's native R syntax. Here is an example with your data:

plot_ly(mydata, x=region, type="histogram", filename="olive oil plants per region") %>%     
    layout(xaxis=list(title='Region'),
           yaxis=list(title='Number of plants'),
           margin=list(b=100), # expand the bottom margin a bit to accommodate the long labels
           title='Olive oil plants per region')

Which creates this graph: https://plot.ly/~christopherp/1129 Example of a histogram in R created with Plotly
(source: christopherp at plot.ly)

More histogram examples

like image 129
Chris P Avatar answered Oct 27 '22 21:10

Chris P