更改绘图框的宽度和/或它们之间的空间

发布于 2025-02-01 04:11:39 字数 1363 浏览 2 评论 0原文

我已经对数据进行了分组,我想使用r的 plotly 软件包将其绘制为一组框图,并控制框的宽度和/或主题之间的空间。

以下是数据:

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)

以下plotly's tutorial 这是我如何绘制它:

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')

给出:

盒子出来太窄的地方以及相同类型的框之间的空间以及不同的类型 s之间的空间大的。

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

根据 a>,在python boxgapboxgroupgap控制这些方面。

I have grouped data which I want to plot as a group of box plots using R's plotly package, and control the width of the boxes and/or the space between theme.

Here are the data:

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)

Following plotly's tutorial this is how I'm plotting it:

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')

Which gives:
enter image description here

Where the boxes come out too narrow and the space both between boxes of the same type as well as the space between the different types are big.

Any idea how to change the box widths and/or the spaces?

According to this post, in python the boxgap and boxgroupgap control these aspects.

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

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

发布评论

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

评论(2

调妓 2025-02-08 04:11:39

类似于Python版本,布局参数为被记录为在这里可以作为参数更改为功能布局

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
  )

Analogous to the python version, layout parameters as being documented here can be changed as arguments of the function 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
  )
奢望 2025-02-08 04:11:39

一种选择是使用连续的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

One alternative is to use a continuous x-axis. Here with ggplotly instead:

# 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

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