Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Bar Charts for Categorical Data | section
Hands-On Data Visualization with ggplot2 in R

bookBar Charts for Categorical Data

Deslize para mostrar o menu

Bar charts are one of the most effective ways to visualize categorical data in R using ggplot2. They allow you to quickly compare the frequency or value of different categories, making patterns and differences easy to spot. In ggplot2, you have two main functions for creating bar charts: geom_bar() and geom_col(). Each serves a specific purpose depending on whether you want to plot counts or actual values for each category.

12345678910
library(ggplot2) # Sample data: favorite fruit survey fruit_data <- data.frame( fruit = c("Apple", "Banana", "Orange", "Apple", "Banana", "Banana", "Orange", "Apple") ) # Bar chart of fruit counts using geom_bar() ggplot(fruit_data, aes(x = fruit)) + geom_bar()
copy
1234567891011
library(ggplot2) # Sample data: fruit sales sales_data <- data.frame( fruit = c("Apple", "Banana", "Orange"), sales = c(30, 45, 25) ) # Bar chart of specified sales values using geom_col() ggplot(sales_data, aes(x = fruit, y = sales)) + geom_col()
copy

While both geom_bar() and geom_col() create bar charts, their behavior is different. Use geom_bar() when your data contains only the categories and you want to show the count of each category - geom_bar() automatically counts the occurrences for you. On the other hand, use geom_col() when your data already contains summary values (such as totals or means) for each category - geom_col() will use the provided values as the bar heights. Choosing between these functions depends on whether you need to count the data or already have summarized values ready for plotting.

1. Which ggplot2 function should you use when your data contains raw observations and you want to show the count of each category?

2. When should you use geom_col() instead of geom_bar()?

question mark

Which ggplot2 function should you use when your data contains raw observations and you want to show the count of each category?

Selecione a resposta correta

question mark

When should you use geom_col() instead of geom_bar()?

Selecione a resposta correta

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 3

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Seção 1. Capítulo 3
some-alt