A bar chart is a type of graph which allows us to compare the unique categories of a categorical variable in a dataset. In this type of graph, data can be represented by vertical or horizontal bars.
Plotly has been actively developed and supported by it's community.
This recipe demonstrates how to plot a bar chart in R using plotly package.
We will use an example of Expenses made by a student
# Data manipulation package library(tidyverse) # Lattice package for data visualisation install.packages("plotly") library(plotly) # Type of expense type_of_expense = c('Rent', 'Grocery', 'Transport') # Amount Amount_USD = c(7000, 3500, 900) #creating a dataframe df = data.frame(type_of_expense,Amount_USD)
We use the plot_ly() function to plot a bar plot.
Syntax: plot_ly( data = , x = , y = , type = "bar" )
fig <- plot_ly(data = df, x = ~type_of_expense, y = ~Amount_USD, type = "bar", color = "orange") %>% layout(title = 'bar chart using Plotly') embed_notebook(fig)