首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >显示完整的x-轴,即使没有点可绘制。

显示完整的x-轴,即使没有点可绘制。
EN

Stack Overflow用户
提问于 2020-03-03 10:51:41
回答 1查看 913关注 0票数 1

我正在巧妙地用line+markers绘制散点图。问题是,我有一个明确的x轴,我想修复它的所有情节的全范围静态。

下面是示例代码:

代码语言:javascript
运行
复制
import(plotly)
x_group <- c('[8-9]','[9-10]','[10-11]','[11-12]','[12-13]','[13-14]','[14-15]','[16-17]','[17-18]','[18-19]','[19-20]')
total <- c('22','17','27','8 ','16','4 ','17','12','15','2 ','22')

example_df <- as.data.frame(list(hour_bin_grp= x_group, total = total))

m <- list(
  l = 50,
  r = 50,
  b = 100,
  t = 100,
  pad = 4
)

hour_bins <- c("[0-7]","[7-8]","[8-9]","[9-10]","[10-11]", "[11-12]","[12-13]", "[13-14]", "[14-15]", "[15-16]","[16-17]", "[17-18]","[18-19]", "[19-20]", "[20-21]","[21-22]")
p <- plot_ly(example_df, x = ~hour_bin_grp, y = ~total, type = 'scatter', mode = 'markers+lines') %>% 
  layout(autosize = T, margin = m,
         xaxis = list(
           type='category',
           categoryorder= 'array',
           showgrid = TRUE,
           autorange = FALSE,
           showticklabels = TRUE,
           categoryarray= unique(hour_bins)
         ),
         yaxis = list(
           showgrid = TRUE,
           autorange = TRUE,
           showline = TRUE,
           showticklabels = TRUE,
           rangemode = "tozero",
         ))
p

这个图似乎没问题,但我想把x轴的范围修正为hour_bins的值。

我已经查过一些同样的文章,但在我的情况下似乎行不通:

Plotly.js: Cannot show full categorical x-axis

https://community.plot.ly/t/cannot-re-arrange-x-axis-when-axis-type-is-category/1274/3

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-10 11:20:09

作为一种解决办法,我建议使用带有字符ticktext的数字滴答:

代码语言:javascript
运行
复制
library(plotly)

bin_df <- data.frame(hour_bins = c("[0-7]","[7-8]","[8-9]","[9-10]","[10-11]", "[11-12]","[12-13]", "[13-14]", "[14-15]", "[15-16]","[16-17]", "[17-18]","[18-19]", "[19-20]", "[20-21]","[21-22]"))
bin_df$hour_bin_grp <- seq_len(nrow(bin_df))

example_df <- data.frame(hour_bin_grp = bin_df$hour_bin_grp[bin_df$hour_bins %in% c('[8-9]','[9-10]','[10-11]','[11-12]','[12-13]','[13-14]','[14-15]','[16-17]','[17-18]','[18-19]','[19-20]')], total = as.numeric(c('22','17','27','8','16','4','17','12','15','2','22')))

p <- plot_ly(example_df, x = ~hour_bin_grp, y = ~total, type = 'scatter', mode = 'markers+lines') %>% 
  layout(xaxis = list(
    tickmode = "array",
    tickvals = bin_df$hour_bin_grp,
    ticktext = example_df$hour_bins,
    range = list(min(bin_df$hour_bin_grp), max(bin_df$hour_bin_grp))
  ))
p

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60505584

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档