R Plotly-plot布局(顶部的边距和x轴)

时间:2018-05-17 14:13:29

标签: r plotly r-plotly

我在使用plotly和R shiny来显示数据时遇到了问题。我的示例代码是:

y <- c("q1", "q2", "q3", "q4", "q5", "q6", "q7")
x1 <- c(20, 10, 15, 15, 20, 10, 15)
x2 <- c(10, 20, 20, 10, 10, 30, 10)
x3 <- c(10, 10, 5, 10, 10, 5, 5)
x4 <- c(20, 25, 25, 35, 55, 40, 35)
x5 <- c(40, 35, 35, 30, 5, 15, 35)
num <-  c(1,3,5,6,7,2,4)
df <- data.frame(y, x1, x2, x3, x4, x5, num)



plot_ly(df[order(-xtfrm(df$num)),]) %>%
  add_trace(x = ~x1, y = ~y, marker = list(color = 'rgb(202,0,32)'), type = 'bar', orientation = 'h') %>%
  add_trace(x = ~x2, y = ~y,  marker = list(color = 'rgb(244,165,130)'), type = 'bar', orientation = 'h') %>%
  add_trace(x = ~x3, y = ~y, marker = list(color = 'rgb(223,223,223)'), type = 'bar', orientation = 'h') %>%
  add_trace(x = ~x4, y = ~y, marker = list(color = 'rgb(146,197,222)'), type = 'bar', orientation = 'h') %>%
  add_trace(x = ~x5, y = ~y,  marker = list(color = 'rgb(5,113,176)'), type = 'bar', orientation = 'h') %>%  
  layout(title="mytitle",
         xaxis = list(title = "",
                      showticklabels = TRUE,
                      zeroline = FALSE,
                      domain = c(0.15, 1)),
         yaxis = list(title = "",
                      showticklabels = FALSE,
                      zeroline = FALSE,
                      categoryorder = 'array', 
                      categoryarray = ~qt),
         barmode = 'relative',
         paper_bgcolor = 'rgb(248, 248, 255)', plot_bgcolor = 'rgb(248, 248, 255)',
         autosize=T,
         margin = list(l = 150, r = 10, t = 100, b = 50, pad=4),
         showlegend=F) %>%
  # labeling the y-axis
  add_annotations(xref = 'paper', yref = 'y', x = 0.14, y = df$y,
                  xanchor = 'right',
                  text = df$y,
                  font = list(family = 'Arial', size = 5,
                              color = 'rgb(67, 67, 67)'),
                  showarrow = FALSE, align = 'right')

问题在于,对于大量问题,边距不能很好地发挥作用。在我的数据中,其中一个类别包括40多个问题。有5个问题的情节看起来不错。 (长度= 500)plot with 5 questions 但是有超过40个问题的情节有很大的上下边缘。 (长度= 4000) plot with 40+ questions 请帮助我摆脱这些奇怪的边缘。 bottom margin and x-axis 另外,我想知道如何将x轴放在图的顶部和底部。谢谢!

0 个答案:

没有答案