0% found this document useful (0 votes)
2 views

Assignment_2

This document presents an assignment on data visualizations using the ggplot2 library in R, showcasing various types of plots including scatter, line, bar, histogram, box, density, violin, and facet plots. Each plot is accompanied by code examples that demonstrate how to create them using a sample data frame. The assignment emphasizes the use of ggplot2 for effective data visualization techniques.

Uploaded by

kreeves75234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment_2

This document presents an assignment on data visualizations using the ggplot2 library in R, showcasing various types of plots including scatter, line, bar, histogram, box, density, violin, and facet plots. Each plot is accompanied by code examples that demonstrate how to create them using a sample data frame. The assignment emphasizes the use of ggplot2 for effective data visualization techniques.

Uploaded by

kreeves75234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Harsha A P

20221CSD0111

5CSD02

Assignment 2:
1. Data Visualizations with ggplot2 with examples

CODE

# Load necessary library

# install.packages("ggplot2")

library(ggplot2)

# Create a sample data frame

set.seed(123) # For reproducibility

data <- data.frame(

Category = rep(c("A", "B", "C", "D"), each = 10),

Values = c(rnorm(10, mean = 5), rnorm(10, mean = 10), rnorm(10, mean = 20), rnorm(10,
mean = 15)),

Score = c(sample(1:100, 40))

# Scatter plot

ggplot(data, aes(x = Values, y = Score, color = Category )) +

geom_point(size = 3) +

labs(title = "Scatter Plot", x = "Values", y = "Score") +


theme_minimal()

# Line plot (using average values for demonstration)

ggplot(data, aes(x = Category, y = Values, group = 1)) +

geom_line() +

geom_point() +

labs(title = "Line Plot", x = "Category", y = "Values") +

theme_minimal()

# Bar plot (average Values by Category)

ggplot(data, aes(x = Category, y = Values)) +

stat_summary(fun = mean, geom = "bar", fill = "lightblue") +

labs(title = "Bar Plot", x = "Category", y = "Average Values") +

theme_minimal()

# Histogram

ggplot(data, aes(x = Values)) +

geom_histogram(bins = 20, fill = "lightgreen", color = "black") +

labs(title = "Histogram", x = "Values", y = "Count") +

theme_minimal()

# Box plot

ggplot(data, aes(x = Category, y = Values, fill = Category)) +

geom_boxplot() +

labs(title = "Box Plot", x = "Category", y = "Values") +

theme_minimal()
# Density plot

ggplot(data, aes(x = Values, fill = Category)) +

geom_density(alpha = 0.5) +

labs(title = "Density Plot", x = "Values", y = "Density") +

theme_minimal()

# Violin plot

ggplot(data, aes(x = Category, y = Values, fill = Category)) +

geom_violin() +

labs(title = "Violin Plot", x = "Category", y = "Values") +

theme_minimal()

# Facet plot

ggplot(data, aes(x = Values, fill = Category)) +

geom_histogram(bins = 10, color = "black") +

labs(title = "Facet Plot", x = "Values", y = "Count") +

facet_wrap(~ Category) +

theme_minimal()

CONSOLE

library(ggplot2)

> # Create a sample data frame

> set.seed(123) # For reproducibility

> data <- data.frame(

+ Category = rep(c("A", "B", "C", "D"), each = 10),

+ Values = c(rnorm(10, mean = 5), rnorm(10, mean = 10), rnorm(10, mean = 20), rnorm(10, mean = 15)),
+ Score = c(sample(1:100, 40))

+)

> # Scatter plot

> ggplot(data, aes(x = Values, y = Score, color = Category )) +

+ geom_point(size = 3) +

+ labs(title = "Scatter Plot", x = "Values", y = "Score") +

+ theme_minimal()

> # Line plot (using average values for demonstration)

> ggplot(data, aes(x = Category, y = Values, group = 1)) +

+ geom_line() +

+ geom_point() +

+ labs(title = "Line Plot", x = "Category", y = "Values") +

+ theme_minimal()

> # Bar plot (average Values by Category)

> ggplot(data, aes(x = Category, y = Values)) +

+ stat_summary(fun = mean, geom = "bar", fill = "lightblue") +

+ labs(title = "Bar Plot", x = "Category", y = "Average Values") +

+ theme_minimal()

> # Histogram

> ggplot(data, aes(x = Values)) +

+ geom_histogram(bins = 20, fill = "lightgreen", color = "black") +

+ labs(title = "Histogram", x = "Values", y = "Count") +

+ theme_minimal()

> # Box plot

> ggplot(data, aes(x = Category, y = Values, fill = Category)) +


+ geom_boxplot() +

+ labs(title = "Box Plot", x = "Category", y = "Values") +

+ theme_minimal()

> # Density plot

> ggplot(data, aes(x = Values, fill = Category)) +

+ geom_density(alpha = 0.5) +

+ labs(title = "Density Plot", x = "Values", y = "Density") +

+ theme_minimal()

> # Violin plot

> ggplot(data, aes(x = Category, y = Values, fill = Category)) +

+ geom_violin() +

+ labs(title = "Violin Plot", x = "Category", y = "Values") +

+ theme_minimal()

> # Facet plot

> ggplot(data, aes(x = Values, fill = Category)) +

+ geom_histogram(bins = 10, color = "black") +

+ labs(title = "Facet Plot", x = "Values", y = "Count") +

+ facet_wrap(~ Category) +

+ theme_minimal()

You might also like