r - 删除 Plotly 中的条之间的间隙
问题描述
我正在尝试使用 Plotly 在 R 中创建一个 Marimekko 图表。本质上,这只是一个堆叠的、可变宽度的条形图,两个条形直接相邻。目前,我的尝试如下所示:
创建它的代码在这里:
bar.test <- plot_ly(type = "bar") %>%
layout(title = paste0("Newark Charter vs District BTO Makeup"),
xaxis = list(title = ""),
yaxis = list(title = "Percent BTO", tickformat = "%")) %>%
add_trace(x = ~test1$sch.type, y = ~test1$y, width = ~test1$width,
marker = list(color = c(blue.dark.opq, red.opq.2, blue.dark.opq, red.opq.2) ,
line = list(color = 'rgba(0,0,0,1)' , width = 2))) %>%
add_annotations(x = ~test1$sch.type, y = ~test1$annotation.y,
text = paste0("<b>", 100*round(test1$y, 3), "%"),
showarrow = F,
font = list(size = 14, color = 'rgba(0,0,0,1)')) %>%
add_annotations(x = ~test1$sch.type, y = ~test1$all.y,
text = paste0(test1$all.count), showarrow = F,
font = list(size = 14, color = 'rgba(0,0,0,1)')) %>%
hide_legend()
数据如下所示:
我的目标只是让钢筋之间没有间隙。我试图用bargap
参数来做到这一点,但已经读到为条分配宽度会使 Plotly 忽略该bargap
参数。我还阅读了一个潜在的解决方法是手动更改条形图的offset
参数。但是,我有许多具有不同宽度和百分比的数字,因此任何解决方案都不能是手动的。
解决方案
您可以通过自定义x
并bargap = 0
在布局中使用来做到这一点
library(plotly)
#> Loading required package: ggplot2
#>
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#>
#> last_plot
#> The following object is masked from 'package:stats':
#>
#> filter
#> The following object is masked from 'package:graphics':
#>
#> layout
test1 <- data.frame(
city.state = "Newark",
sch.type = c("Charter", "Charter", "District", "District"),
bto.stat = c(0,1,0,1),
y = c(.7, .3, .1, .9),
width = c(.3, .3, .7, .7),
x = c(.15, .15, .65, .65),
annotation.y = c(.3, .8, .05, .55),
all.count = c(46000, 46000, 99000, 99000),
all.y = c(1,1,1,1)
)
bar.test <- plot_ly(type = "bar") %>%
add_trace(x = test1$x,
y = test1$y,
width = test1$width,
marker = list(
color = c("blue", "red", "blue", "red") ,
line = list(color = 'rgba(0,0,0,1)' , width = 2)
)
) %>%
layout(bargap = 0)
bar.test
由reprex 包(v0.3.0)于 2019 年 11 月 19 日创建
推荐阅读
- c# - 在dot net core c#上使用OpenTK录制立体声(双通道)WAV文件
- git - VueJS 在更新时在不同的分支中保持相同的依赖版本
- vue.js - Vuex 4 模块不能使用全局 axios 属性
- python - 我正在尝试在循环中重新分配值,但它没有改变
- html - 我只有一个模板在 Flask 中获取 CSS
- azure - 尝试通过 Azure Function 中的 Azure Management Fluent Api 创建 KeyVault 时出现 ArgumentNullException
- python - 如何在 Python(Python REST API)中发送 POST 请求?
- google-app-engine - 使用迁移选项为 Google App Engine 设置流量
- matlab - Fmincon / Multistart:找到局部最小值,但违反了一个非线性约束
- list - 当我向其他邮件发送邮件时,如何制作一封邮件来提醒其他邮件