【问题标题】:How to change x-axis layout using plotly in r如何在 r 中使用 plotly 更改 x 轴布局
【发布时间】:2018-08-21 19:54:47
【问题描述】:

我正在尝试自定义 x 轴。我目前在 x 轴上有“站点”变量。范围是从 15 到 24,每个数字都有站点 a 和 b,即对于 15,有 15a 和 b(依此类推到 24)。当我标记所有条时,它看起来很乱,我正在尝试对其进行自定义,例如,数字 15 在下方,但各个条标记为 a 和 b。这样,各个酒吧将是可识别的,但看起来不会那么拥挤。

到目前为止,这是我的代码:

#Stacked bar chart exp2 
BarChartData2 <- read.csv("Bar chart data exp 2.csv")
Site <- BarChartData2[,4]
Card <- BarChartData2[,2]
Pen <- BarChartData2[,3]
data2 <- data.frame(Site, Card, Pen)
pstacked2 <- plot_ly(data2, x = ~Site, y = ~Card, type = 'bar', name = 'Card', marker = list(color = 'Black')) %>%
  add_trace(y = ~Pen, name = 'Pen', marker = list(color = 'red')) %>%
 layout(yaxis = list(title = 'Number of Bee Visits'), barmode = 'stack',  font = list(family = 'Times New Roman', size =14, color ="black"), xaxis = list(autotick = F, dtick = 2))
pstacked2

任何关于如何做到这一点的帮助/其他想法将不胜感激!

【问题讨论】:

  • 我们无法运行您的代码。您可以使用 dput() 添加数据集示例。
  • > dput(BarChartData2[,4]) 结构(1:20, .Label = c("15a", "15b", "16a", "16b", "17a", "17b ”、“18a”、“18b”、“19a”、“19b”、“20a”、“20b”、“21a”、“21b”、“22a”、“22b”、“23a”、“23b”、 "24a", "24b"), class= "因子") > dput(BarChartData2[,2]) c(15L, 7L, 11L, 8L, 9L, 8L, 2L, 5L, 10L, 15L, 6L, 9L, 3L, 2L, 3L, 4L, 5L, 1L, 3L, 5L) > dput(BarChartData2[,3]) c(10L, 8L, 12L, 5L, 8L, 14L, 2L, 6L, 9L, 16L, 7L, 9L , 4L, 7L, 4L, 2L, 7L, 1L, 7L, 5L)
  • 请将所有代码和信息放入问题本身。您可以通过单击标签下的edit 按钮来编辑您的问题

标签: r r-plotly


【解决方案1】:

这里有一些东西可以帮助你。您可以使用shapes 和annotations 来获得您想要的。

plot_ly(data2, x = ~Site, y = ~Card, type = 'bar', name = 'Card', marker = list(color = 'Black')) %>%
  add_trace(y = ~Pen, name = 'Pen', marker = list(color = 'red')) %>%
  layout(yaxis = list(title = 'Number of Bee Visits'), 
         barmode = 'stack',  
         font = list(family = 'Times New Roman', size =14, color ="black"), 
         xaxis = list(autotick = F, dtick = 2),
         margin = list(
           r = 10, 
           t = 25, 
           b = 100, 
           l = 110
         ),
         shapes = list(
           list(
             line = list(
               color = "rgba(68, 68, 68, 0.5)", 
               width = 1
             ), 
             type = "line", 
             x0 = 0, 
             x1 = 1, 
             xref = "paper", 
             y0 = -0.05, 
             y1 = -0.05, 
             yref = "paper"
           )
         ),
         annotations = list(
           list(
             x = 0.04, 
             y = -0.1, 
             showarrow = FALSE, 
             text = "15", 
             xref = "paper", 
             yref = "paper"
           ), 
           list(
             x = 0.14, 
             y = -0.1, 
             showarrow = FALSE, 
             text = "16", 
             xref = "paper", 
             yref = "paper"
           ), 
           list(
             x = 0.24, 
             y = -0.1, 
             showarrow = FALSE, 
             text = "17", 
             xref = "paper", 
             yref = "paper"
           ), 
           list(
             x = .35, 
             y = -0.1, 
             showarrow = FALSE, 
             text = "18", 
             xref = "paper", 
             yref = "paper"
           ),
           list(
             x = .45, 
             y = -0.1, 
             showarrow = FALSE, 
             text = "19", 
             xref = "paper", 
             yref = "paper"
           ),
           list(
             x = .55, 
             y = -0.1, 
             showarrow = FALSE, 
             text = "20", 
             xref = "paper", 
             yref = "paper"
           ),
           list(
             x = .65, 
             y = -0.1, 
             showarrow = FALSE, 
             text = "21", 
             xref = "paper", 
             yref = "paper"
           ),
           list(
             x = .75, 
             y = -0.1, 
             showarrow = FALSE, 
             text = "22", 
             xref = "paper", 
             yref = "paper"
           ),
           list(
             x = .85, 
             y = -0.1, 
             showarrow = FALSE, 
             text = "23", 
             xref = "paper", 
             yref = "paper"
           ),
           list(
             x = .95, 
             y = -0.1, 
             showarrow = FALSE, 
             text = "24", 
             xref = "paper", 
             yref = "paper"
           )
         )
         )

【讨论】:

    猜你喜欢
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-24
    相关资源
    最近更新 更多