Create a horizontal bar chart
To create a horizontal bar chart we need a data frame so we have created a data frame that is used to create a horizontal bar chart with the help of ggvis() method of ggvis library.
R
# Importing ggvis package library ( "ggvis" ) # Declaring a data frame data_frame <- data.frame (col1 = c ( "a" , "b" , "c" , "d" , "e" ), col2 = c (1,2,3,4,5), col3 = c ( "Green" , "Orange" , "Green" , "Green" , "Orange" )) # Printing the data frame print ( "Data Frame" ) print (data_frame) # Plotting the data data_frame %>% ggvis (x =~col2, y=~col1, fill =~ col3) %>% layer_rects (x2 = 0, height = band ()) |
Output:
[1] "Data Frame" > print (data_frame) col1 col2 col3 1 a 1 Green 2 b 2 Orange 3 c 3 Green 4 d 4 Green 5 e 5 Orange
Explanation :
The col2 values are taken as x coordinates and the col1 values of the data frame are taken as y coordinates of the plot constructed. The colors are then assigned based on the col3 values depicted by the fill parameter of the ggvis() method.
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.
Contact Us