X轴标签中的换行符,Plotly

时间:2018-04-20 07:29:17

标签: r plotly

如果两个单词之间有空格,我需要在多行中断开x轴标签,因为实时类别标签是大名称,我必须在小图表中显示它。

所以用给定的例子。我想看阿卡什然后在下一行它应该显示耆那教。 我使用以下代码绘制瀑布图。

source('./r_files/flatten_HTML.r')
library("plotly")
 dataset <- data.frame(Category = c("Akash Jain","Ankit Jain","Pankaj Jain","Nitin Pandey","Gopal Pandit","Ramnath Agarwal"),
                      TH =  c(-62,-71,-1010,44,-44,200))
#dataset <- data.frame(Category = Values$Category, TH = Values$TH)
#dataset <- as.data.frame(cbind(Values$Category,Values$TH))
dataset$Category = dataset$Category
dataset$TH = dataset$TH
dataset$SortedCategory <- factor(dataset$Category, levels = dataset$Category)
dataset$id <- seq_along(dataset$TH)
dataset$type <- ifelse(dataset$TH > 0, "in",   "out")
dataset$type <- factor(dataset$type, levels = c("out", "in"))
dataset$end <- cumsum(dataset$TH)
dataset$start <- c(0, head(dataset$end, -1))
Hover_Text <- paste(dataset$SortedCategory, "= ", dataset$TH, "<br>")
dataset$colors <- ifelse(dataset$type =="out","red","green")
g <- plot_ly(dataset, x = ~SortedCategory, y = ~start, type = 'bar', marker = list(color = 'rgba(1,1,1, 0.0)'), hoverinfo = 'text') %>%
  add_trace(y = dataset$TH , marker = list(color = ~colors), hoverinfo = "text", text = Hover_Text  ) %>%
  layout(title = '',
         xaxis = list(title = ""),
         yaxis = list(title = ""),
         barmode = 'stack',
         showlegend = FALSE) 

g

任何帮助都会非常感激。

此致 阿卡什

1 个答案:

答案 0 :(得分:1)

只需为SortedCategory中的每个空格插入换行符,然后生成您的情节:

dataset$SortedCategory <- sapply(dataset$SortedCategory, function(x) gsub(" ", " <br> ", x))