Plotly - 用 R 发布我的情节

Plotly - Publishing my plot with R

我有一个 Plotly 帐户和一个创建情节的 R 脚本,我想在我的 Plotly 页面上发布。查看 Plotly 入门页面和其他一些在线建议后,我最终得到了这段代码。

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)

但是,当我执行代码时出现此错误:

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") 

我该如何解决这个问题并发布我的情节?我还尝试了以下方法:

ggplotly(plot_region)

但是它出现了这个错误:

Error in `[[<-.data.frame`(`*tmp*`, a, value = c("ΣΤΕΡΕΑ ΕΛΛΑΔΑ",  : 
  replacement has 483 rows, data has 13

您的 ggplotly 语法是正确的,在这种情况下,从 ggplotplotly 的转换似乎失败了。我在这里报告了这个错误:https://github.com/ropensci/plotly/issues/278

另一种解决方案是使用 plotly's native R syntax 创建图形。以下是您的数据示例:

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

创建此图的对象:https://plot.ly/~christopherp/1129
(来源:christopherp at plot.ly

More histogram examples