-1

I want to create a stacked bar chart in R and plotly using iris dataset. In the x-axis, I want to set limits like iris_limits below in the code and the y-axis should contain all the Sepal.Length values which fit into these ranges. I want to pass the values as a single vector. Also, if the limits can be made dynamic by understanding the range of the Sepal.Length instead of hard coding it, please help. I have written a basic script with values to give you an idea. Thanks.

library(plotly)
iris_limits <- c("1-4", "4-6", "6-8")
sepal <- c(2.4,5.4,7.1)
data <- data.frame(iris_limits, sepal)
p <- plot_ly(data, x = ~iris_limits, y = ~sepal, type = 'bar', name = 
'Sepal') %>%
layout(yaxis = list(title = 'Count'), barmode = 'group')
p
Ashmin Kaul
  • 860
  • 2
  • 12
  • 37
  • What is being stacked for each `Sepal.Length` group? `Species`? – Djork Oct 27 '17 at 11:33
  • iris_limits <- c("1-4", "4-6", "6-8"), these ranges, and the sepal.length value should fall in these ranges. – Ashmin Kaul Oct 27 '17 at 11:34
  • So one bar, the bar count is divided (stacked) based on Sepal.Length range? – Djork Oct 27 '17 at 11:36
  • In the x-axis, I need it as "1-3","3-6","6-9". If this can be generated dynamically without hard coding it. Also, I want to pass iris$Sepal.Length as one vector from which every value of Sepal.Length is a bar which should fall into the one of the range above. Thus making it a stacked bar chart. This is my need? – Ashmin Kaul Oct 27 '17 at 11:37
  • @Djork, please help me with this link: https://stackoverflow.com/questions/47278387/creating-a-horizontal-bar-chart-in-r-to-display-sequence-of-activities?noredirect=1#comment81507641_47278387 – Ashmin Kaul Nov 14 '17 at 07:20

2 Answers2

2

I tried my best to understand. First dividing the sepal length to the desired categories iris_limits: "1-3","3-6","6-9"

iris$iris_limits <- cut(iris$Sepal.Length, c(1,3,6,9))

Note: no sepal length is in between 1-3, so you only have 2 groups.

Then you want each sepal length limit as a separate bar on the x axis, and each individual sepal length falling into category to be bar stacked onto each other? You linked to a stack bar chart with varying color for the stacked bars, is this what you want?

Create an ID for each sepal length:

iris$ID <- factor(1:nrow(iris))

Plot, set color=~ID if you want different colors for the stacked bars:

library(plotly)
p <- plot_ly(iris, x = ~iris_limits, y = ~Sepal.Length, type = 'bar', color=~ID) %>%
  layout(yaxis = list(title = 'Count'), barmode = 'stack')

enter image description here

EDITED For version that is not stacked but grouped by iris_limits, I switched to ggplot2 to make use of facet_wrap functionality to segregate by iris_limits, then use ggplotly.

gg <- ggplot(iris, aes(x=ID, y=Sepal.Length, fill=iris_limits)) + 
  geom_bar(stat="identity", position="dodge") +
  facet_wrap(~iris_limits, scales="free_x", labeller=label_both) +
  theme_minimal() + xlab("") + ylab("Sepal Length") +
  theme(axis.text.x=element_blank())
ggplotly(gg)

enter image description here

EDITED: Re: Changing legend title and tooltip display

To change the legend title, use labs. Here it was also necessary to change the legend.title font size under theme to fit the ggplotly margins.

To change the tooltip text, add text parameter to aes to create desired character string, then define aes values to be displayed in tooltip in ggplotly.

gg <- ggplot(iris, aes(x=ID, y=Sepal.Length, fill=iris_limits,
                   text=paste("Sepal Length:", Sepal.Length, "cm"))) + 
  geom_bar(stat="identity", position="dodge") +
  facet_wrap(~iris_limits, scales="free_x") +
  theme_minimal() + xlab("") + ylab("Sepal Length (cm)") +
  theme(axis.text.x=element_blank(), legend.title=element_text(size=10)) +
  labs(fill="Sepal \nLength (cm)")
ggplotly(gg, tooltip=c("x", "text"))
Djork
  • 3,319
  • 1
  • 16
  • 27
  • Very nice but nearly close, these sepal.length limits as a separate bar on the x axis should be vertical to x-axis and not parallel, and second, the right slider is not needed, kindly help. – Ashmin Kaul Oct 27 '17 at 12:02
  • If it's vertical to the x-axis then it's not a stacked bar, and you really want multiple bars that are grouped by sepal length limits? – Djork Oct 27 '17 at 12:12
  • yes multiple bars, like a grouped bar chart. See this link here. M sorry but I thought grouped and stacked to be same. https://plot.ly/r/bar-charts/ – Ashmin Kaul Oct 27 '17 at 12:13
  • Big thanks Djork, really really appreciate your help. – Ashmin Kaul Oct 27 '17 at 13:05
  • @AshminKaul It's a nice practice to upvote the answer along with marking answered! – amrrs Oct 27 '17 at 14:41
  • @Djork, this plot has helped me immensely. I need your help with little two tweaks: 1. Firstly, The labels iris_limits on top of plots and axes, I want to rename them, 2. Also when we hover on the plot, I want to assign "cm" to Sepal.Length in the tooltip. Thanks and please help. – Ashmin Kaul Oct 30 '17 at 07:42
  • Hmm, when I run it, I am not able to see "cm" besides Sepal.Length and also when I zoom the plot, Sepal.Length on Y-axis dissappears. – Ashmin Kaul Oct 31 '17 at 04:44
  • Please see the bottom edit, I just updated it. It works for me. – Djork Oct 31 '17 at 04:46
  • Yes, thanks, @Djork, please help me with this post, https://stackoverflow.com/questions/47018157/updating-multiple-infobox-using-plotly-click-in-r-and-plotly – Ashmin Kaul Oct 31 '17 at 05:12
  • Hi @Djork, I want to calculate how many number of bars that are present in these two ranges above. Just want to find the number, please help. – Ashmin Kaul Oct 31 '17 at 13:19
  • @Djork, Hi, I require your help with this post, please check, https://stackoverflow.com/questions/47920689/displaying-data-in-the-chart-based-on-plotly-click-in-r-shiny/47921511#47921511 – Ashmin Kaul Dec 22 '17 at 14:14
  • @Djork, I need your help to integrate the js code with R, https://stackoverflow.com/questions/48162029/integrating-the-js-code-in-r-to-make-the-visnetwork-edges-curved – Ashmin Kaul Jan 09 '18 at 07:52
0

Try using cut:

library(plotly)
iris$iris_limits <- as.numeric(cut(iris$Sepal.Length,3))
p <- plot_ly(iris, x = ~iris_limits, y = ~Sepal.Length, type = 'bar', name = 
               'Sepal') %>%
  layout(yaxis = list(title = 'Count'), barmode = 'group')
p

enter image description here

The grouping details:

> iris$Sepal.Length[iris$iris_limits==1]
 [1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 5.4 4.8 4.8 4.3 5.4 5.1 5.1 5.4 5.1 4.6 5.1 4.8 5.0 5.0 5.2 5.2 4.7 4.8
[29] 5.4 5.2 5.5 4.9 5.0 5.5 4.9 4.4 5.1 5.0 4.5 4.4 5.0 5.1 4.8 5.1 4.6 5.3 5.0 5.5 4.9 5.2 5.0 5.5 5.5 5.4 5.5 5.5
[57] 5.0 5.1 4.9
> iris$Sepal.Length[iris$iris_limits==2]
 [1] 5.8 5.7 5.7 6.4 6.5 5.7 6.3 6.6 5.9 6.0 6.1 5.6 6.7 5.6 5.8 6.2 5.6 5.9 6.1 6.3 6.1 6.4 6.6 6.7 6.0 5.7 5.8 6.0
[29] 6.0 6.7 6.3 5.6 6.1 5.8 5.6 5.7 5.7 6.2 5.7 6.3 5.8 6.3 6.5 6.7 6.5 6.4 5.7 5.8 6.4 6.5 6.0 5.6 6.3 6.7 6.2 6.1
[57] 6.4 6.4 6.3 6.1 6.3 6.4 6.0 6.7 5.8 6.7 6.7 6.3 6.5 6.2 5.9
> iris$Sepal.Length[iris$iris_limits==3]
 [1] 7.0 6.9 6.8 7.1 7.6 7.3 7.2 6.8 7.7 7.7 6.9 7.7 7.2 7.2 7.4 7.9 7.7 6.9 6.9 6.8
> 
amrrs
  • 6,215
  • 2
  • 18
  • 27
  • Hi amrrs, thanks but this is very different, I need ranges in x-axes like "1-3","3-6","6-9", and fit all the sepal.length values in that range. each sepal.length value should represent a bar. thanks again. – Ashmin Kaul Oct 27 '17 at 10:32
  • Did you try to hover it? that's what it does. – amrrs Oct 27 '17 at 10:37
  • In your x-axis, I see 1,2, and 3, I need it as "1-3","3-6","6-9". Also, I want to pass iris$Sepal.Length as one vector from which every value of Sepal.Length is a bar which should fall into the one of the range above. Thus making it a stacked bar chart. This is my need? – Ashmin Kaul Oct 27 '17 at 10:45
  • https://plot.ly/r/bar-charts/#stacked-bar-chart , the grouped bar chart in this link may help. – Ashmin Kaul Oct 27 '17 at 10:50
  • I appreciate your help amrrs, thank you for your help. – Ashmin Kaul Oct 30 '17 at 05:13
  • @AshminKaul I meant it for the actual answer. – amrrs Oct 30 '17 at 06:46
  • I am trying to update multple widgets using plotly_click, please help me with this link: https://stackoverflow.com/questions/47018157/updating-multiple-infobox-using-plotly-click-in-r-and-plotly – Ashmin Kaul Oct 31 '17 at 04:26