簡體   English   中英

無法使用 ggplotly 轉換 ggplot2

[英]Unable to convert ggplot2 using ggplotly

在此處輸入圖像描述 我有一個帶有以下代碼的 ggplot2 圖表

   [![enter image description here][1]][1]a <-filter(match,team==theTeam)
   # Group batsman with non strikers and compute partnerships
   df <- data.frame(summarise(group_by(a,batsman,nonStriker),sum(runs)))
   names(df) <- c("batsman","nonStriker","runs")

if(plot==TRUE){
    plot.title <- paste(theTeam,"Batting partnership in match (vs.",opposition,")")
    ggplot(data=df,aes(x=batsman,y=runs,fill=nonStriker))+
        geom_bar(data=df,stat="identity") +
        xlab("Batmen") + ylab("Runs Scored") +
        ggtitle(bquote(atop(.(plot.title),
                                atop(italic("Data source:http://cricsheet.org/"),"")))) +
        theme(axis.text.x = element_text(angle = 90, hjust = 1))

生成如下圖表。

但是當我嘗試創建一個交互式圖表時

g<-ggplot(data=df,aes(x=batsman,y=runs,fill=nonStriker))+
        geom_bar(data=df,stat="identity") +
        xlab("Batmen") + ylab("Runs Scored") +
        ggtitle(bquote(atop(.(plot.title),
                                atop(italic("Data source:http://cricsheet.org/"),"")))) +
        theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggplotly(g)

我收到以下錯誤

Error in unique.default(x) : unique() applies only to vectors
In addition: Warning message:
In if (robust_nchar(plot$labels$title) > 0) { :
the condition has length > 1 and only the first element will be used

在轉換 ggplot -> ggplotly 之前有什么具體需要做的嗎?

謝謝

測試數據

擊球手 nonStriker 跑 JA Morkel Joginder Sharma 21 JA Morkel MS Dhoni 3 JA Morkel MS Gony 5 Joginder Sharma JA Morkel 16

MS Dhoni        JA Morkel       1
MS Dhoni        SK Raina        22
MS Gony JA Morkel       15
PA Patel        SP Fleming      4
S Anirudha      SP Fleming      1
S Badrinath     SK Raina        0
SK Raina        MS Dhoni        16
SK Raina        S Badrinath     9
SK Raina        SP Fleming      7
SP Fleming      S Anirudha      5
SP Fleming      SK Raina        9

這是plotly中長期懸而未決的問題,其中字幕從 ggplot -> plotly 中丟失。 現在的修復是在 plotly 中添加標題和副標題。

library(ggplot2)
library(plotly)

ggplot(data=df,aes(x=batsman,y=runs,fill=nonStriker))+
  geom_bar(data=df,stat="identity") +
  xlab("Batmen") + ylab("Runs Scored") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) -> g

ggplotly(g) %>%
  layout(title = list(text = paste0(plot.title, '\n<sup>Data source:http://cricsheet.org/</sup>')))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM