R provides powerful tools for data visualization. The base R system and additional libraries like ggplot2
allow us to create various types of plots to explore and present data effectively.
Letβs start with simple visualizations using base R.
# Sample data x <- c(1, 2, 3, 4, 5) y <- c(10, 20, 15, 25, 30) # Create a basic line plot plot(x, y, type = "o", col = "blue", main = "Line Plot", xlab = "X Axis", ylab = "Y Axis")
values <- c(20, 35, 30, 40) names <- c("A", "B", "C", "D") barplot(values, names.arg = names, col = "skyblue", main = "Bar Plot")
data <- c(5, 10, 20, 20, 15, 10, 25, 30) hist(data, col = "orange", main = "Histogram", xlab = "Value")
scores <- c(55, 70, 65, 80, 90, 85, 60) boxplot(scores, main = "Boxplot of Scores", col = "lightgreen")
slices <- c(30, 25, 20, 25) labels <- c("Math", "Science", "History", "English") pie(slices, labels = labels, col = rainbow(4), main = "Subjects Pie Chart")
ggplot2 is a powerful package for creating advanced and customized visualizations in R.
# Install and load ggplot2 (only once) # install.packages("ggplot2") library(ggplot2) # Sample dataframe df <- data.frame( x = c(1, 2, 3, 4, 5), y = c(10, 20, 15, 25, 30) ) # Create a scatter plot ggplot(df, aes(x, y)) + geom_point(color = "blue") + ggtitle("Scatter Plot with ggplot2")
rnorm()
.ggplot2
to create a line chart of sales data.Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!