ggplotly with points, error bars and facets error
还没有人认领这个 Issue。
- 主要语言
- R
- 星标
- 2.7k
- 派生
- 641
- PR 合并指标
- 30 天内没有已合并 PR
描述
I am building a ggplot which has points, error bars and facets. I use the ggplotly() function to create a plotly graph, but I get the error returned below. With a bit of tracing back, I found the error to be in ggplotly() -> layers2traces() -> to_basic() when it's applied to the error bar geometry (it works when applied to the points geometry). In particular, it crashes on this line
data$width <- (data[["xmax"]] - data[["x"]])/(data[["x_max"]] -
data[["x_min"]])
Because x is not an element in data. In a slightly larger case than the reprex below, but with the same properties, I found the following when applying head() to the data object in the browser:
Browse[4]> head(data)
PANEL colour x.x y ymin ymax group x_plotlyDomain
1 1 #00BFC4 1.125 0.3621916 0.3348249 0.3891862 2 2004
2 1 #F8766D 0.875 0.3628851 0.3388994 0.3877805 1 2004
3 1 #00BFC4 2.125 0.3107964 0.2823060 0.3398029 4 2006
4 1 #F8766D 1.875 0.3113829 0.2856144 0.3391679 3 2006
5 1 #00BFC4 3.125 0.3019772 0.2791706 0.3261124 6 2008
6 1 #F8766D 2.875 0.3030195 0.2814583 0.3247936 5 2008
y_plotlyDomain xmin xmax colour_plotlyDomain size linetype width alpha
1 0.3621916 1.125 1.125 Model 2 1.5 1 0 NA
2 0.3628851 0.875 0.875 Model 1 1.5 1 0 NA
3 0.3107964 2.125 2.125 Model 2 1.5 1 0 NA
4 0.3113829 1.875 1.875 Model 1 1.5 1 0 NA
5 0.3019772 3.125 3.125 Model 2 1.5 1 0 NA
6 0.3030195 2.875 2.875 Model 1 1.5 1 0 NA
hovertext
1 year: 2004<br />fit: 0.3621916<br />model: Model 2<br />lower: 0.3348249<br />upper: 0.3891862
2 year: 2004<br />fit: 0.3628851<br />model: Model 1<br />lower: 0.3388994<br />upper: 0.3877805
3 year: 2006<br />fit: 0.3107964<br />model: Model 2<br />lower: 0.2823060<br />upper: 0.3398029
4 year: 2006<br />fit: 0.3113829<br />model: Model 1<br />lower: 0.2856144<br />upper: 0.3391679
5 year: 2008<br />fit: 0.3019772<br />model: Model 2<br />lower: 0.2791706<br />upper: 0.3261124
6 year: 2008<br />fit: 0.3030195<br />model: Model 1<br />lower: 0.2814583<br />upper: 0.3247936
ROW COL x.y SCALE_X SCALE_Y xaxis yaxis xanchor yanchor x_min x_max y_min
1 1 1 Male 1 1 xaxis yaxis y x 0.4 5.6 0.1775765
2 1 1 Male 1 1 xaxis yaxis y x 0.4 5.6 0.1775765
3 1 1 Male 1 1 xaxis yaxis y x 0.4 5.6 0.1775765
4 1 1 Male 1 1 xaxis yaxis y x 0.4 5.6 0.1775765
5 1 1 Male 1 1 xaxis yaxis y x 0.4 5.6 0.1775765
6 1 1 Male 1 1 xaxis yaxis y x 0.4 5.6 0.1775765
y_max
1 0.5294233
2 0.5294233
3 0.5294233
4 0.5294233
5 0.5294233
6 0.5294233
The full reprex is below.
library(tibble)
library(ggplot2)
library(plotly)
tmp <- tibble(
year = rep(2004,4),
x = factor(c(1,2,1,2), labels=c("Male", "Female")),
fit = c(0.3628851, 0.3698281, 0.3621916, 0.3689059),
lower = c(0.3388994,0.3408131,0.3348249,0.3419387),
upper = c(0.3877805, 0.3998003, 0.3891862, 0.3980086),
model = factor(c(1,1,2,2), labels=c("Model 1", "Model 2"))
)
g <- ggplot(tmp, aes(x=year, y=fit, colour=model, ymin=lower, ymax=upper)) +
geom_point(position=position_dodge(width=.5), size=3) +
geom_errorbar(width=0, position=position_dodge(width=.5), size=1.5) +
facet_wrap(.~x)
ggplotly(g)
#> Error in `$<-.data.frame`(`*tmp*`, "width", value = numeric(0)): replacement has 0 rows, data has 4
Created on 2020-02-25 by the reprex package (v0.3.0)
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先使用 ggplot2 和 plotly 运行提供的 reprex,然后在处理 geom_errorbar 图层时,跟踪 ggplotly() 经过 layers2traces() 到达 to_basic()。将误差条数据列与其中访问的字段进行比较;当提供的分面图能够成功转换且不再出现替换大小错误,并且相关测试覆盖此情况时,即表示完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- r
- 领域
- data-visualization
- Issue 类型
- 缺陷
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100