0% found this document useful (0 votes)
9 views2 pages

Ggplot Code

The document provides R code for visualizing the 'mtcars' dataset using the ggplot2 library. It includes various types of plots such as line plots, scatter plots, histograms, box plots, and bar charts to analyze the relationship between miles per gallon (MPG) and other variables. The code demonstrates how to create these visualizations with appropriate labels and themes.
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)
9 views2 pages

Ggplot Code

The document provides R code for visualizing the 'mtcars' dataset using the ggplot2 library. It includes various types of plots such as line plots, scatter plots, histograms, box plots, and bar charts to analyze the relationship between miles per gallon (MPG) and other variables. The code demonstrates how to create these visualizations with appropriate labels and themes.
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

library(dplyr)

library(ggplot2)

str(mtcars)

mtcars$Index <- 1:nrow(mtcars) ## Create an Index

ggplot(mtcars,aes(x = Index, y = mpg))+geom_line(color = "blue")+ geom_point(color = "red")


+theme_minimal()+labs(title ="Simulated Trend of MPG Over 'Time'",x="Car Index",y="Miles Per
Gallon")

ggplot(mtcars, aes(x = wt, y = mpg))+geom_point()+theme_minimal()+labs(title = "Scatter plot of


MPG vs Car Weight",x = "Weight (1000 lbs)",y = "Miles per Gallon")

ggplot(mtcars, aes(x = mpg)) + geom_histogram(binwidth = 5, fill = "yellow", color =


"black")+theme_minimal()+labs(title = "Histogram of MPG",x = "Miles per Gallon",y = "Frequency")

ggplot(mtcars, aes(x = factor(cyl), y = mpg)) + geom_boxplot()+theme_minimal()+labs(title = "Box


Plot of MPG by Cylinder Count", x = "Number of Cylinders",y = "Miles per Gallon")

data <- [Link](category = c("Category 1", "Category 2", "Category 3", "Category 4"),values =
c(30, 20, 15, 35))

data$percentage <- (data$values/sum(data$values))*100

ggplot(data, aes(x = "", y = percentage, fill = category))+geom_bar(stat ="identity", width =


1)+coord_polar(theta = "y")+labs(title = "Pie Chart-like Visualization",fill = "Category",x = NULL,y =
NULL)+theme_void()+theme([Link] = "right")

df <- [Link](dose=c("D0.5", "D1", "D2"), len=c(4.2, 10, 29.5))

p<-ggplot(data=df, aes(x=dose, y=len)) +

geom_bar(stat="identity")

# Horizontal bar plot

p + coord_flip()

ggplot(data=df, aes(x=dose, y=len)) +

geom_bar(stat="identity", width=0.5)

ggplot(data=df, aes(x=dose, y=len)) +

geom_bar(stat="identity", color="blue", fill="white")

p<-ggplot(data=df, aes(x=dose, y=len)) +

geom_col(stat="identity", fill="steelblue")+

theme_minimal()

p
head(mtcars)

library(ggplot2)

ggplot(data = mtcars)+labs(title = "MTCars Data Plot")

ggplot(data = mtcars,aes(x = hp,y = mpg,col=disp))+labs(title ="MTCars Data Plot")

ggplot(data=mtcars,aes(x=hp,y=mpg,col=disp))+geom_point()+labs(title ="Miles per Gallon vs


Horsepower",x="Horsepower",y="Miles per Gallon")

ggplot(data =
mtcars,aes(x=hp,y=mpg,col=factor(cyl),shape=factor(am)))+geom_point()+labs(title="Miles per
Gallon vs hp",x="Horsepower",y="Miles per Gallon")

ggplot(data = mtcars, aes(x = hp, y = mpg, shape =


factor(cyl)))+geom_point()+facet_grid(.~cyl)+labs(title = "Miles per Gallon vs Horsepower",

x = "Horsepower",

y = "Miles per Gallon")

ggplot(data = mtcars, aes(x = hp, y = mpg, shape = factor(cyl)))+geom_point()+facet_grid(am~.

)+labs(title = "Miles per Gallon vs Horsepower",

x = "Horsepower",

y = "Miles per Gallon")

ggplot(data = mtcars, aes(x = hp, y = mpg))+geom_point()+stat_smooth(method = lm, col = "red") +


labs(title = "Miles per Gallon vs Horsepower")

You might also like