100% found this document useful (3 votes)
5K views

R Handson

The document provides instructions and examples for creating various plots in R, including histograms, bar plots, scatter plots, and box plots using built-in datasets like Orange, cars, and mtcars. It demonstrates how to customize plots by changing colors, labels, and layouts. The examples are intended for learning data visualization in R.

Uploaded by

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

R Handson

The document provides instructions and examples for creating various plots in R, including histograms, bar plots, scatter plots, and box plots using built-in datasets like Orange, cars, and mtcars. It demonstrates how to customize plots by changing colors, labels, and layouts. The examples are intended for learning data visualization in R.

Uploaded by

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

I hope are your doing great.

I have seen you got completed all R Courses in Fresco


Play.

I ahvve stuck with hands-on on Data Visualization using R-1 - Plotting in R. Its
saying below instrucations but unable to run "vim code.r" command.

plot(Orange$circumference,Orange$age,xlab="Circumference",ylab="Age",pch=19,col=Ora
nge$Tree)
legend('topleft',sort(levels(Orange$Tree)),col=1:length(levels(Orange$Tree)),pch=19
)

str(cars)
summary(cars)
summary(Puromycin)

plot(cars$speed, xlab = 'Speed', ylab = 'Dist', main='Frequency Histogram: Speed')

hist(AirPassengers, main="Histogram for Air Passengers")

hist(cars, xlab="speed", ylab="Frequency of speed")


hist(cars, main="Plot of Car Speed", xlab="Car Speed", border="blue", col="green",
xlim=c(100,700), las=1, breaks=5)
plot(cars$speed, cars$dist, frame.plot=FALSE)

boxplot(cars, main="Cars: Speed and stopping distance", frame.plot=FALSE)

Practice plots:----
hist(cars$speed)
hist(cars$speed, breaks=10)
hist(cars$speed, breaks=4:25, main="Car Speed", xlab="Speed (mph)")
hist(cars$speed, main="Plot of Car Speed", xlab="Car Speed", border="blue",
col="green")
plot(cars$speed, cars$dist, type="p", xlab="Speed (mph)", ylab="Stopping
distance(ft)", main="Cars: Speed and stopping distance", frame.plot=FALSE)
hist(cars$speed, breaks=10, labels=TRUE)

Practice Exercise:----
par(mfrow = c(2,2))
plot(mtcars$mpg, mtcars$cyl)
plot(mtcars$mpg, mtcars$hp)
plot(mtcars$mpg, mtcars$wt)
plot(mtcars$mpg, mtcars$vs)

counts <- table(Orange$Tree)


barplot(counts, main="Trees count", xlab="Tree Types", ylab="count",
col=c("Yellow","red"), legend = c("Not vs","vs"), beside=TRUE)

barplot(counts, main="Trees count", xlab="count", ylab="Tree Types", col="green")


counts <- table(Orange$Tree)
barplot(counts, main="Trees count", xlab="Tree Types", ylab="count", col="red",
horiz=TRUE)

counts <- table(mtcars$cyl, mtcars$gear)


barplot(counts, main="Car Distribution by Count of V-shaped engine and Carb",
xlab="Number of Carb", ylab="Count of cars", col=c("Yellow","red"), legend = c("Not
vs","vs"))
counts <- table(mtcars$mpg)
percentlabels<- round(100*counts/sum(counts), 1)
pielabels<- paste(percentlabels, "%", sep="")
pie(counts,col = rainbow(length(counts)), labels = pielabels , main = 'Pie Chart
for MTCars distribution of Carburetors', cex = 0.8)

boxplot(mtcars$mpg, mtcars$gear)
boxplot(mtcars$mpg, mtcars$gear, notch=TRUE)
plot(cars$speed, cars$distance, xlab="Speed (mph)", ylab="Stopping distance(ft)"
main="Scatterplot Matrix",pch=8, col="brown")
abline(lm(distance~speed))
plot(cars$speed, cars$distance, xlab="Speed (mph)", ylab="Stopping distance(ft)"
main="Scatterplot Matrix",pch=8, col="brown", cex = 0.5)

plot(cars$speed, cars$distance, xlab="Speed (mph)", ylab="Stopping distance(ft)"


main="Scatterplot Matrix", cex=0.5, cex.main=2, cex.lab=1.5, cex.axis=0.7,
font.main=4, font.lab=2, font.axis=3, col=5, col.main=4, col.lab=2, col.axis=3)
pairs(~mpg+disp+drat+wt,data=mtcars, main="Scatterplot Matrix", col="brown")

plot(log(Puromycin$conc), (Puromycin$rate), col=Puromycin$state)


text(x=5, y=7, label="start=rate", adj=0)
legend(13.5,3, fill=c(4,3), legend=c("treated", "untreated"))

library(lattice) VADeathRate <- as.data.frame.table(VADeaths)


barchart(Var1 ~ Freq | Var2, data = VADeathRate,
groups = Var2, main = "Bar Chart in R EXample",
ylab = "Var1", stack = TRUE,
auto.key = list(space = "right"),
scales = list(x = list(rot = 45)),
layout = c(4,1))

library(lattice)
botplot(Var1 ~ Freq, data = VADeathRate,
groups = Var2, main = "Bar Chart in R EXample")
auto.key = list(xn5,y=0.95,legend=c("mean","5th and 95th percentiles"),
scales = list(x = list(rot = 45)),
layout = c(4,1))

lattice::bwplot(runif(1000) ~ cut(runif(1000), c(0,0.3,0.6,1)) |


as.factor(c(1,2,3)), groups = sample(1:2, 1000, replace = TRUE), auto.key = TRUE
library(dplyr)

install.packages("ggplot2")
library(ggplot2)
data(mpg)
qplot(cty, hwy, data=mpg, color =drv) +
geom_point(alpha = .2) +
theme_bw()

You might also like