简体   繁体   English

如何在R中使用plotly更改x轴布局

[英]How to change x-axis layout using plotly in r

I'm trying to customise the x-axis. 我正在尝试自定义X轴。 I currently have the "Site" variable on the x-axis. 我目前在x轴上有“网站”变量。 The range is from 15 to 24 with site a and b for each number ie for 15, there is 15a and b (and so on to 24). 范围是从15到24,每个数字都有站点a和b,即对于15,有15a和b(依此类推,直到24)。 When I label all bars, it looks messy and I'm trying to customise it so for example, the number 15 is below but the individual bars are labelled a and b. 当我标记所有条形时,它看起来很杂乱,因此我试图对其进行自定义,因此,例如,数字15在下面,但各个条形都标记为a和b。 This way, the individual bars will be identifiable but it won't look so crowded. 这样,可以识别各个小节,但看起来不会那么拥挤。 目前的Barplot

Here is my code so far: 到目前为止,这是我的代码:

#Stacked bar chart exp2 
BarChartData2 <- read.csv("Bar chart data exp 2.csv")
Site <- BarChartData2[,4]
Card <- BarChartData2[,2]
Pen <- BarChartData2[,3]
data2 <- data.frame(Site, Card, Pen)
pstacked2 <- plot_ly(data2, x = ~Site, y = ~Card, type = 'bar', name = 'Card', marker = list(color = 'Black')) %>%
  add_trace(y = ~Pen, name = 'Pen', marker = list(color = 'red')) %>%
 layout(yaxis = list(title = 'Number of Bee Visits'), barmode = 'stack',  font = list(family = 'Times New Roman', size =14, color ="black"), xaxis = list(autotick = F, dtick = 2))
pstacked2

Any help/other ideas of how to do this will be much appreciated! 任何帮助/其他想法如何做到这一点将不胜感激!

Here is something to help you. 这里有一些可以帮助您的东西。 You can play with shapes and annotations to get exatly what you want. 您可以使用shapesannotations来获得所需的内容。

plot_ly(data2, x = ~Site, y = ~Card, type = 'bar', name = 'Card', marker = list(color = 'Black')) %>%
  add_trace(y = ~Pen, name = 'Pen', marker = list(color = 'red')) %>%
  layout(yaxis = list(title = 'Number of Bee Visits'), 
         barmode = 'stack',  
         font = list(family = 'Times New Roman', size =14, color ="black"), 
         xaxis = list(autotick = F, dtick = 2),
         margin = list(
           r = 10, 
           t = 25, 
           b = 100, 
           l = 110
         ),
         shapes = list(
           list(
             line = list(
               color = "rgba(68, 68, 68, 0.5)", 
               width = 1
             ), 
             type = "line", 
             x0 = 0, 
             x1 = 1, 
             xref = "paper", 
             y0 = -0.05, 
             y1 = -0.05, 
             yref = "paper"
           )
         ),
         annotations = list(
           list(
             x = 0.04, 
             y = -0.1, 
             showarrow = FALSE, 
             text = "15", 
             xref = "paper", 
             yref = "paper"
           ), 
           list(
             x = 0.14, 
             y = -0.1, 
             showarrow = FALSE, 
             text = "16", 
             xref = "paper", 
             yref = "paper"
           ), 
           list(
             x = 0.24, 
             y = -0.1, 
             showarrow = FALSE, 
             text = "17", 
             xref = "paper", 
             yref = "paper"
           ), 
           list(
             x = .35, 
             y = -0.1, 
             showarrow = FALSE, 
             text = "18", 
             xref = "paper", 
             yref = "paper"
           ),
           list(
             x = .45, 
             y = -0.1, 
             showarrow = FALSE, 
             text = "19", 
             xref = "paper", 
             yref = "paper"
           ),
           list(
             x = .55, 
             y = -0.1, 
             showarrow = FALSE, 
             text = "20", 
             xref = "paper", 
             yref = "paper"
           ),
           list(
             x = .65, 
             y = -0.1, 
             showarrow = FALSE, 
             text = "21", 
             xref = "paper", 
             yref = "paper"
           ),
           list(
             x = .75, 
             y = -0.1, 
             showarrow = FALSE, 
             text = "22", 
             xref = "paper", 
             yref = "paper"
           ),
           list(
             x = .85, 
             y = -0.1, 
             showarrow = FALSE, 
             text = "23", 
             xref = "paper", 
             yref = "paper"
           ),
           list(
             x = .95, 
             y = -0.1, 
             showarrow = FALSE, 
             text = "24", 
             xref = "paper", 
             yref = "paper"
           )
         )
         )

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM