Create a horizontal stacked bar chart
The same procedure is followed to create a horizontal bar chart and to create a stacked bar chart we have the group_by() method which is used to create segments based on the values mapped to individual groups. The following code snippet indicates the grouping of the data by col3 values and therefore, creates three groups. Which results in the horizontal stacked bar chart.
R
# Import ggvis library library ( "ggvis" ) # Declaring a data frame data_frame <- data.frame (col1 = c ( "a" , "b" , "a" , "e" , "a" , "b" , "a" , "e" , "a" , "b" ), col2 = c (1:10), col3 = c (1,2,2,4,9,1,4,2,2,1)) # Printing the data frame print ( "Data Frame" ) print (data_frame) # Plotting the data data_frame %>% ggvis (x =~col2, y=~col1, fill =~ col3) %>% group_by (col3)%>% layer_rects (x2 = 0, height = band ()) |
Output:
[1] "Data Frame" > print (data_frame) col1 col2 col3 1 a 1 1 2 b 2 2 3 a 3 2 4 e 4 4 5 a 5 9 6 b 6 1 7 a 7 4 8 e 8 2 9 a 9 2 10 b 10 1
How to create horizontal stacked bar chart using ggvis in R?
In this article, we are going to learn how to create a horizontal stacked bar chart using ggvis in R programming language.