【问题标题】:Changing the widths of plotly boxes and/or the space between them更改绘图框的宽度和/或它们之间的空间
【发布时间】:2022-07-15 19:13:11
【问题描述】:

我已使用Rplotly 包将我想要绘制为一组箱线图的数据分组,并控制箱的宽度和/或主题之间的空间。

以下是数据:

set.seed(1)
df <- data.frame(type = c(rep("t1", 1000), rep("t2", 1000), rep("t3", 1000), rep("t4", 1000), rep("t5", 1000), rep("t6", 1000)),
                 age = rep(c(rep("y", 500),rep("o", 500)), 6),
                 value = rep(c(runif(500, 5, 10), runif(500, 7.5, 12.5)), 6),
                 stringsAsFactors = F)
df$age <- factor(df$age, levels = c("y", "o"), ordered = T)

按照plotlytutorial,这就是我的绘图方式:

library(plotly)
library(dplyr)
plot_ly(x = df$type, y = df$value, name = df$age, color = df$type, type = 'box',showlegend = F) %>%
  layout(yaxis=list(title="Diversity"),boxmode='group')

这给出了:

盒子出来的地方太窄,相同type的盒子之间的空间以及不同types之间的空间都很大。

知道如何更改框的宽度和/或空格吗?

根据this post,在pythonboxgapboxgroupgap控制这些方面。

【问题讨论】:

    标签: r plotly width boxplot space


    【解决方案1】:

    类似于 python 版本,here 记录的布局参数可以更改为函数layout 的参数:

    plot_ly(x = df$type, y = df$value, name = df$age, color = df$type,
      type = "box", showlegend = F) %>%
      layout(yaxis = list(title = "Diversity"),
       boxmode = "group", boxgap = 0, boxgroupgap = 0
      )
    

    【讨论】:

    • 谢谢。不幸的是,即使将boxgapboxgroupgap 都设置为0,框仍然太窄。知道如何调整框的宽度吗?
    • 必须是更深层次的实现。这是我们根据文档可以做的一切
    【解决方案2】:

    另一种方法是使用连续的 x 轴。这里用 ggplotly 代替:

    # convert factors to numbers
    df$itype <- as.numeric (factor (df$type))
    sc <- scale (unique (as.numeric (factor (df$age))))
    df$iage <- sc[as.numeric (factor (df$age))] * .3
    
    # plot
    gg <- 
      ggplot (df, aes (x=itype+iage, y=value, color=type, group=itype+iage)) + 
      geom_boxplot() + 
      scale_x_continuous(labels = levels (factor (df$type)), breaks = 1:length (levels (factor (df$type)))) +
      labs (x="", y="Diversity")
    
    
    ggplotly (gg) %>%
      layout(boxgroupgap = 0, boxgap=0)
    
    

    plot

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-07
      • 2021-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多