无法使用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 之前有什么具体需要做的吗?
谢谢
测试数据
击球手非前锋跑
JA Morkel Joginder 夏尔马 21
JA Morkel MS Dhoni 3
JA Morkel MS 戈尼 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
中的 long pending open issue,其中字幕从 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>')))
[![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 之前有什么具体需要做的吗?
谢谢
测试数据
击球手非前锋跑 JA Morkel Joginder 夏尔马 21 JA Morkel MS Dhoni 3 JA Morkel MS 戈尼 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
中的 long pending open issue,其中字幕从 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>')))