Get startedGet started for free

Dropping Bars

1. Dropping Bars

In this video, we will explore the power of bar charts, an essential visualization tool that adds another dimension to our skill set. Let's get started!

2. Bar charts

Bar charts are widely utilized for visualizing categorical or grouped data, allowing us to easily compare values across different categories. In the example shown here, we have a bar chart that displays the average prices of various products in Indian markets. By looking at the chart, we can quickly see the price differences between the categories and understand how they compare. Bar charts make it easy to grasp this information at a glance.

3. Bar charts vs histograms

Histograms and bar charts serve distinct purposes in data visualization. On the one hand, histograms are primarily used to depict the distribution of numerical data. On the other hand, bar charts excel in comparing different categories, providing an effective means of visualizing and comparing categorical data.

4. Our dataset

This video will analyze a dataset containing prices of non-food products in India. Let us calculate the mean price per product using the Statistics package. We start by grouping the product DataFrame by each product. Next, we calculate the mean of the price column within the grouped DataFrame, employing the combine function. Lastly, we sort the result in descending order based on the mean price.

5. Creating bar charts

To generate a bar chart, we use the bar function. The first argument specifies the categorical column to be displayed on the x-axis, while the second represents the values plotted on the y-axis. By utilizing the bar_width argument, we can adjust the width of the bars. Its value, ranging from zero to one, determines the portion of available space occupied by the bars. In this instance, we set it to 0.5, making bars occupy half of the available space. This process produces a bar chart that compares the average prices of the products.

6. Horizontal bar charts

To generate a horizontal bar chart, we utilize the bar function, reversing the order of values for each axis. Initially, we provide the data for the y-axis, followed by the data for the x-axis. After that, we define the permute argument as a tuple, specifying the axes to be permuted, namely x and y. Observe how the axes are flipped in this horizontal chart.

7. Products by state

Now, let's examine a subset of our dataset and compare the prices of these products in three different states: Maharashtra, Goa, and Gujarat. First, we filter the product DataFrame to include only data for these three states. To obtain the average price of each product based on the state, we start by grouping the DataFrame by state and product and then calculating the average price using the combine function.

8. Grouped bar charts

To visualize the average product prices by state, we can utilize the groupedbar function from StatsPlots. We invoke this function with the categories to be displayed on the x-axis as the first argument and the corresponding values to be shown on the y-axis as the second argument. In addition, we specify the column that contains the different groups, which in this case is the state column, using the group argument. By following these steps, we generate a grouped bar chart that illustrates the average price of each product, segmented by state.

9. Stack the bars

We can also create a stacked bar plot by setting the bar position argument to stack.

10. Let's practice!

Categorical variables play a significant role in our data explorations. Now that we have learned how to visualize categorical data using bar charts, it's time to put our knowledge into practice in the exercises!