Читать книгу Industrial Data Analytics for Diagnosis and Prognosis - Yong Chen - Страница 19
Distribution of A Categorical Variable – Bar Chart
ОглавлениеIn a bar chart, the horizontal axis corresponds to all possible values/categories of a categorical variable. The vertical axis shows the number of observations in each category. To draw a bar chart for a categorical variable in R
, we need to first use the table()
function to count the number of observations in each category. Then the barplot()
function can be used to plot the calculated counts. For example, the following R
codes plot the distribution of the body.style
variable in the auto_spec
data set.
bodystyle.freq <- table(auto.spec.df$body.style)
barplot(bodystyle.freq, xlab = "Body Style",
ylim = c(0, 100))
The plotted bar chart is shown in Figure 2.1. From the bar chart, it is clear that most of the cars in the data are either sedans or hatchbacks.
Figure 2.1 Bar chart of car body style.