Plotly R:在Tableau这样的酒吧图中创建分隔线

发布于 2025-01-23 10:30:11 字数 710 浏览 3 评论 0原文

我有此数据框架:

df = data.frame(
  Period = c("FY2015", "FY2015", "FY2015", "FY2015", "FY2015", "FY2015", "FY2016", "FY2016", 
              "FY2016", "FY2016", "FY2016", "FY2016"),
  Region = c("Africa", "Asia", "Australia", "Europe", "North America", "South America", 
             "Africa", "Asia", "Australia", "Europe", "North America", "South America"),
  Revenue = c(1.0e+07, 2.0e+07, 7.0e+07, 1.2e+08, 1.5e+08, 6.0e+07, 8.0e+06, 1.6e+07, 5.6e+07, 
              9.6e+07, 1.2e+08, 4.8e+07))

我想在区域中拥有一个plotly bar绘图,就像我做过在Tableau中

这里有提示吗?

I have this dataframe:

df = data.frame(
  Period = c("FY2015", "FY2015", "FY2015", "FY2015", "FY2015", "FY2015", "FY2016", "FY2016", 
              "FY2016", "FY2016", "FY2016", "FY2016"),
  Region = c("Africa", "Asia", "Australia", "Europe", "North America", "South America", 
             "Africa", "Asia", "Australia", "Europe", "North America", "South America"),
  Revenue = c(1.0e+07, 2.0e+07, 7.0e+07, 1.2e+08, 1.5e+08, 6.0e+07, 8.0e+06, 1.6e+07, 5.6e+07, 
              9.6e+07, 1.2e+08, 4.8e+07))

I would like to have a plotly bar plot in divided by Region, just like I did here in Tableau.

Any tips here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨落成白 2025-01-30 10:30:11

我不知道一种完全自动的方式,但是我们可以通过制作刻面,去除它们之间的填充并在边界上添加垂直线来伪造它。

在这种情况下,GGPLOT以两个离散类别(“ FY2015”和“ FY2016”),将它们视为1和2的值。默认条形宽度和填充意味着这些面板的边缘在右侧为0.4右边为2.6。

library(ggplot2)
library(plotly)

my_plot <- ggplot(df, aes(Period, Revenue)) +
  geom_col() +
  geom_vline(xintercept = c(0.4, 2.6), size = 0.2, color = "gray70") +
  facet_wrap(~Region, nrow = 1) +
  theme_minimal() +
  theme(panel.grid.major.x = element_blank(),
        panel.spacing = unit(0, "cm"))

ggplotly(my_plot)

I don't know a totally automatic way, but we can fake it by making facets, removing the padding between them, and adding vertical lines at the borders.

In this case, with two discrete categories ("FY2015" and "FY2016"), ggplot treats them under the hood as values of 1 and 2. The default bar width and padding means that the edges of these panels are at 0.4 on the right and 2.6 on the right.

library(ggplot2)
library(plotly)

my_plot <- ggplot(df, aes(Period, Revenue)) +
  geom_col() +
  geom_vline(xintercept = c(0.4, 2.6), size = 0.2, color = "gray70") +
  facet_wrap(~Region, nrow = 1) +
  theme_minimal() +
  theme(panel.grid.major.x = element_blank(),
        panel.spacing = unit(0, "cm"))

ggplotly(my_plot)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文