0% found this document useful (0 votes)
10 views5 pages

Data Visualization Using R - Codes

The document provides a guide on data visualization using R, specifically with the ggplot2, lattice, and plotly libraries. It includes examples of creating various plots such as scatter plots, boxplots, histograms, and 3D scatter plots using the mtcars and iris datasets. The document also explains how to customize the appearance of these visualizations.

Uploaded by

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

Data Visualization Using R - Codes

The document provides a guide on data visualization using R, specifically with the ggplot2, lattice, and plotly libraries. It includes examples of creating various plots such as scatter plots, boxplots, histograms, and 3D scatter plots using the mtcars and iris datasets. The document also explains how to customize the appearance of these visualizations.

Uploaded by

krishamathur2u
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Data visualization using R

#to view datasets


Data()
?mtcars #describes the data
Mmtcars #recall datasets
[Link]("ggplot2")
library(ggplot2)

#using ggplot function


ggplot(mtcars,aes(x=hp,y=mpg))+
geom_point(size=3)+
geom_line(colour="green")+
labs(title="Horsepower vs Mileage per
Gallon",x="Horsepower",y="Miles per gallon")

#installing lattice library to create visuals with multiple variables


and facets
[Link]("lattice")
library(lattice)
xyplot(mpg~hp,data=mtcars,main="MPG vs Horsepower by Cylinders")
#adding facets to this above visual
xyplot(mpg~hp|cyl,data=mtcars,main="MPG vs Horsepower by
Cylinders")
[Link]("plotly")
library(plotly)
#Plotly R Code for mtcars
# Scatter Plot — MPG vs Horsepower library(plotly)

# ~ indicates a formula-style mapping used by Plotly to link data


columns to aesthetics.
plot_ly(
data = mtcars,
x = ~hp,
y = ~mpg,
type = 'scatter',
mode = 'markers',
marker = list(size = 10, color =
'blue')
) %>%
layout(
title = "MPG vs Horsepower",
xaxis = list(title =
"Horsepower (hp)"),
yaxis = list(title = "Miles per
Gallon (mpg)")

# Boxplot — MPG by Number of


Cylinders library(plotly) %>% is the pipe operator from
plot_ly( magrittr/dplyr, passing the plot object
to the next function (layout()).

 layout(: Customizes the appearance


data = mtcars,
x = ~factor(cyl), #factor(cyl) converts it to a categorical variable
(e.g., “4”, “6”, “8” cylinders)
y = ~mpg,
type = 'box',
color = ~factor(cyl)
) %>%
layout(
title = "MPG by Number of Cylinders",
xaxis = list(title = "Cylinders"),
yaxis = list(title = "Miles per Gallon (mpg)")
)

#Histogram — Distribution of MPG


library(plotly)

plot_ly(
data = mtcars,
x = ~mpg,
type = 'histogram',
marker = list(color = 'orange')
) %>%
layout(
title = "Distribution of MPG",
xaxis = list(title = "Miles per
Gallon (mpg)"),
yaxis = list(title = "Frequency")
)
# 3D Scatter Plot — MPG, HP, Weight
library(plotly)
plot_ly(
data = mtcars,
x = ~hp,

y = ~wt,
z = ~mpg,
type = 'scatter3d',
mode = 'markers',
marker = list(size = 5, color = ~mpg, colorscale = 'Viridis')
) %>%
layout(
title = "3D Plot: MPG, Horsepower, Weight",
scene = list(
xaxis = list(title = "Horsepower"),
yaxis = list(title = "Weight (1000 lbs)"),
zaxis = list(title = "MPG")
)
)

#iris dataset
library(plotly)
plot_ly(
data = iris,
x = ~[Link],
y = ~[Link],
color = ~Species,
colors = c("#1f77b4", "#2ca02c",
"#d62728"),
type = "scatter",
mode = "markers"
)

You might also like