To plot factors in R, you can use various plotting functions from the base R and ggplot2 packages to visualize the distribution and relationships of categorical data. Common plots include bar plots and pie charts, which help in understanding the frequency and proportion of factor levels.
In this example,
fruits
which contains the values 'Apple'
, 'Banana'
, 'Cherry'
, 'Apple'
, and 'Banana'
. This vector represents different types of fruits.factor()
function to convert the fruits
vector into a factor. We assign the result to a variable named fruit_factor
. The factor()
function automatically identifies the unique levels of the vector.table()
function to create a table of counts for each level of the fruit_factor
. We assign the result to a variable named fruit_counts
.barplot()
function to create a bar plot of the fruit_counts
. The barplot()
function takes the counts and plots a bar for each level of the factor.main
, xlab
, and ylab
arguments to provide a title and axis labels.fruits <- c('Apple', 'Banana', 'Cherry', 'Apple', 'Banana')
fruit_factor <- factor(fruits)
fruit_counts <- table(fruit_factor)
barplot(fruit_counts, main='Fruit Types', xlab='Fruit', ylab='Count')
In this example,
cars
which contains the values 'Toyota'
, 'Ford'
, 'Honda'
, 'Toyota'
, 'Ford'
, and 'Honda'
. This vector represents different car brands.factor()
function to convert the cars
vector into a factor. We assign the result to a variable named car_factor
. The factor()
function automatically identifies the unique levels of the vector.table()
function to create a table of counts for each level of the car_factor
. We assign the result to a variable named car_counts
.pie()
function to create a pie chart of the car_counts
. The pie()
function takes the counts and plots a slice for each level of the factor.main
argument to provide context for the visualization.cars <- c('Toyota', 'Ford', 'Honda', 'Toyota', 'Ford', 'Honda')
car_factor <- factor(cars)
car_counts <- table(car_factor)
pie(car_counts, main='Car Brands')
In this example,
employees
which contains two columns: name
and department
. The name
column represents employee names, and the department
column represents their respective departments (with values 'HR'
, 'Finance'
, and 'IT'
).ggplot2
package to create a bar plot. We load the ggplot2
package using the library()
function.ggplot()
function to create the plot object. We pass the employees
data frame and specify the aesthetics using aes(x=department)
to set the x-axis to the department factor.geom_bar()
function, which will automatically count the occurrences of each department.labs()
function to provide a title and axis labels.library(ggplot2)
employees <- data.frame(name = c('John', 'Sara', 'Mike', 'Anna', 'Tom'), department = c('HR', 'Finance', 'IT', 'HR', 'Finance'))
ggplot(employees, aes(x=department)) + geom_bar() + labs(title='Employee Departments', x='Department', y='Count')
In this tutorial, we learned How to Plot Factors in R language with well detailed examples.