Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ Building Basic Plots | Getting Started with ggplot2
/
Data Visualization in R with ggplot2

bookBuilding Basic Plots

メニューを表示するにはスワイプしてください

When you want to visualize your data in R, ggplot2 offers a variety of common plot types, each suited to different data and questions. Scatter plots are ideal when you want to show the relationship between two numerical variables, such as height versus weight. Bar charts are best for comparing values across categories, like sales by product or region. Line charts help you see trends over time, such as monthly revenue. Histograms are used to explore the distribution of a single numerical variable, while boxplots summarize distributions and highlight outliers. Choosing the right plot type depends on your data: use scatter plots for two continuous variables, bar charts for categorical comparisons, and histograms or boxplots for distributions.

1234567891011
# Create a hardcoded data frame for sales by product category sales_data <- data.frame( Category = c("Books", "Electronics", "Clothing", "Toys"), Sales = c(120, 300, 150, 80) ) # Build a bar chart using ggplot2 library(ggplot2) ggplot(sales_data, aes(x = Category, y = Sales)) + geom_bar(stat = "identity", fill = "steelblue") + labs(title = "Sales by Product Category", x = "Category", y = "Sales")
copy

To understand how this bar chart is constructed, look at each part of the code. First, the sales_data data frame holds two columns: Category (categorical) and Sales (numerical). The ggplot function initializes the plot, mapping Category to the x-axis and Sales to the y-axis using aes(). The geom_bar layer tells ggplot2 to draw bars for each category. By setting stat = "identity", you instruct ggplot2 to use the actual sales values instead of counting occurrences. The fill argument colors the bars. Finally, labs adds helpful labels. Mapping the categorical variable (Category) to the x-axis ensures each bar represents a distinct category, making the chart easy to interpret.

1. Which plot types are best suited for categorical versus numerical data?

2. What is the main purpose of geom_bar in the bar chart code sample above?

3. Why is it important to map categorical variables to the x-axis in a bar chart, as explained above?

question mark

Which plot types are best suited for categorical versus numerical data?

すべての正しい答えを選択

question mark

What is the main purpose of geom_bar in the bar chart code sample above?

正しい答えを選んでください

question mark

Why is it important to map categorical variables to the x-axis in a bar chart, as explained above?

すべての正しい答えを選択

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  2

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  2
some-alt