Skip to content

Commit a6051d7

Browse files
authored
Add files via upload
1 parent c88cd79 commit a6051d7

8 files changed

+762
-0
lines changed

Graphs-Advanced.R

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# "ggplot2" package
2+
#If using for first time, uncomment this line to install "ggplot2" package
3+
#install.packages("ggplot2")
4+
library(ggplot2)
5+
# Mapping "displ - displacement" and "cty - city mileage" attributes using ggplot
6+
# Differentiating points based on "manufacturer" attribute
7+
ggplot(mpg, aes(displ, cty, colour = manufacturer)) + geom_point()
8+
9+
##############################################################################################
10+
11+
# Individual data visualization of the dataset based on an attribute -> facet_wrap(attr)
12+
ggplot(mpg, aes(displ, cty)) + geom_point() + facet_wrap("class")
13+
14+
# Data visulaization in different formats
15+
#geom_smooth() -> computes conditional mean using LO(W)ESS method -> discrete to continuous
16+
# se = FALSE - hides the error shaded region around the curve
17+
ggplot(mpg, aes(displ, cty)) + geom_smooth(se = FALSE) + facet_wrap("class")
18+
ggplot(mpg, aes(displ, cty)) + geom_boxplot() + facet_wrap("class")
19+
20+
#geom_smooth by default takes all data points into consideration to fit the curve
21+
#if span value is lesser than 1, it takes that much proportion of nearest values
22+
ggplot(mpg, aes(displ, cty)) + geom_smooth(span = 1)
23+
ggplot(mpg, aes(displ, cty)) + geom_smooth(span = 0.2)
24+
25+
##############################################################################################
26+
27+
# geom_jitter ; geom_violin -> To avoid overplotting
28+
ggplot(mpg, aes(drv, hwy)) + geom_point()
29+
#Adds some random noise and more apt for smaller dataset
30+
ggplot(mpg, aes(drv, hwy)) + geom_jitter()
31+
#Calculates density estimate of distribution
32+
ggplot(mpg, aes(drv, hwy)) + geom_violin()
33+
34+
##############################################################################################
35+
36+
# Histogram and Frequency polygon used to study one attribute in detail
37+
ggplot(mtcars,aes(cyl)) + geom_histogram(bins = 5)
38+
ggplot(mtcars,aes(cyl)) + geom_freqpoly(bins = 5)
39+
40+
# Histogram and Frequency polygon of an attribute differentiated with repsect to another attribute
41+
ggplot(mpg, aes(displ, colour = drv)) + geom_freqpoly(bins = 5)
42+
ggplot(mpg, aes(displ, fill = drv)) + geom_histogram(bins = 5) + facet_wrap("drv", ncol = 1)
43+
44+
##############################################################################################
45+
46+
# Bar charts
47+
marks = data.frame(stud = c("SK7", "CR7", "Messi"), mark = c(98, 90, 100))
48+
print(marks)
49+
50+
# Use stat = "identity" to represent value of data rather than count
51+
# Need 2 aesthetic variables
52+
ggplot(marks, aes(stud, mark)) + geom_bar(stat = "identity") + geom_point()
53+
ggplot(marks, aes(stud, mark)) + geom_point()
54+
55+
# Time series
56+
ggplot(economics, aes(date, uempmed)) + geom_line()
57+
58+
##############################################################################################
59+
60+
# Basic Plot types using ggplot
61+
sample_text = data.frame(x = c(1,2,3), y = c(10,15,35), label = c("A","B","C"))
62+
print(sample_text)
63+
64+
sample_plot = ggplot(sample_text, aes(x, y, label = label)) + labs(x = "ID", y = "Value")
65+
print(sample_plot)
66+
67+
sample_plot+geom_point()+ggtitle("Point")
68+
# Plots the value by representing as labels
69+
sample_plot+geom_text()+ggtitle("Text")
70+
sample_plot+geom_bar(stat = "identity")+ggtitle("Bar chart")
71+
sample_plot+geom_polygon()+ggtitle("Polygon")
72+
sample_plot+geom_raster()+ggtitle("Raster")
73+
74+
##############################################################################################
75+
76+
# Usecase - highlighting specific portion of data using conditons in the plot
77+
#Using ggalt for first time, install the package
78+
#install.packages("ggalt")
79+
library(ggalt)
80+
81+
# Filtering data from midwest dataset based on poptotal and area constraints
82+
midwest_select = midwest[midwest$poptotal > 350000 & midwest$poptotal < 500000 &
83+
midwest$area > 0.01 & midwest$area < 0.1, ]
84+
print(midwest_select)
85+
86+
# Plotting
87+
ggplot(midwest, aes(x = area, y = poptotal)) + geom_smooth() +
88+
#Differentiating points based on "popdensity" and "State"
89+
geom_point(aes(color = state, size = popdensity)) +
90+
xlim(c(0, 0.1)) +
91+
ylim(c(0, 500000)) +
92+
geom_encircle(aes(x = area, y = poptotal),
93+
data = midwest_select,
94+
color = "red",
95+
size = 4,
96+
expand = 0.1) +
97+
labs(x = "Area", y = "Population",
98+
title = "Area vs Population in Midwest",
99+
caption = "Source: midwest dataset")
100+
101+
##############################################################################################

Graphs.R

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# 2D Pie chart
2+
# x should be a numeric vector
3+
x = c(10, 30, 50, 100)
4+
label = c("Data1", "Data2", "Data3", "Data4")
5+
pie(x, label, col = rainbow(length(x)), main = "SAMPLE 2D PIE CHART" )
6+
7+
##############################################################################################
8+
9+
# To use 3D charts install "plotrix" package and import it
10+
#If using for first time, uncomment this line to install plotrix package
11+
#install.packages("plotrix")
12+
library(plotrix)
13+
14+
# 3D Pie chart
15+
x = c(10, 30, 50, 100)
16+
label = c("Data1", "Data2", "Data3", "Data4")
17+
# No need for "col" parameter to be specified
18+
pie3D(x, labels = label, explode = 0.05, main = "SAMPLE 3D PIE CHART")
19+
20+
##############################################################################################
21+
22+
#BarPlot
23+
x = c(100, 75, 90, 82, 95)
24+
students = c("S1", "S2", "S3", "S4", "S5")
25+
# names.arg caries label for each bar in the barplot
26+
barplot(x, names.arg = students, col = rainbow(length(x)),
27+
xlab = "Students", ylab = "Marks", main = "BAR PLOT - STUDENT MARKS")
28+
29+
##############################################################################################
30+
31+
#Stack Barplot
32+
m1 = c(100, 75, 90, 82, 95)
33+
m2 = c(40, 70, 60, 54, 81)
34+
m3 = c(20, 12, 50, 23, 49)
35+
# Creating a matrix with 3 rows and 5 columns arranging the data row wise
36+
values = matrix(c(m1,m2,m3), nrow = 3, ncol = 5, byrow = TRUE)
37+
print("MATRIX OF MARKS SECURED BY 5 STUDENTS IN 3 SUBJECTS")
38+
print(values)
39+
students = c("S1", "S2", "S3", "S4", "S5")
40+
subjects = c("M1", "M2", "M3")
41+
barplot(values, names.arg = students, col = rainbow(length(subjects)),
42+
xlab = "Students", ylab = "Marks", main = "STACKED BAR PLOT - STUDENT MARKS SPLIT UP")
43+
#Adding legend for 3 subjects and specifying position of legend in the graph - 0,230
44+
legend(0,230, cex = .6, subjects, fill = rainbow(length(subjects)))
45+
46+
##############################################################################################
47+
48+
#Data of Boxplots - taking "mtcars" dataset in R
49+
count = 1
50+
cyl4 = c()
51+
cyl6 = c()
52+
cyl8 = c()
53+
for(i in mtcars[,'cyl'])
54+
{
55+
56+
if(i == 4)
57+
{
58+
cyl4 = c(cyl4, mtcars[count,'mpg'])
59+
}
60+
if(i == 6)
61+
{
62+
cyl6 = c(cyl6, mtcars[count,'mpg'])
63+
}
64+
if(i == 8)
65+
{
66+
cyl8 = c(cyl8, mtcars[count,'mpg'])
67+
}
68+
count = count+1
69+
}
70+
print("MPG for 4 cylinders")
71+
print(sort(cyl4))
72+
print("MPG for 6 cylinders")
73+
print(sort(cyl6))
74+
print("MPG for 8 cylinders")
75+
print(sort(cyl8))
76+
77+
#BoxPlot
78+
boxplot(mpg ~ cyl, data = mtcars,
79+
xlab = "No of Cylinders", ylab = "Miles per Gallon", main = "BOXPLOT SAMPLE",
80+
col = rainbow(length(mpg ~ cyl)))
81+
82+
##############################################################################################
83+
84+
# Histogram
85+
# Seq(start, end, by = range of each bin)
86+
v = c(10,20,30,4,5,35,60)
87+
hist(v, breaks = seq(0,100, by=20), main = "Histogram Plot", col = "red", xlab = "Values")
88+
89+
##############################################################################################
90+
91+
# Line charts - 1D
92+
v1 = c(10,20,30,4,5,35,60)
93+
v2 = c(15,25,35,40,15,25,10)
94+
# Plot types -> l, p, b, o, s, h
95+
plot(v1, type = "o", col = rainbow(length(v1)), xlab = "Index", ylab = "Frequency",
96+
main = "Line Plot")
97+
# To plot another line in same graph
98+
lines(v2, type = "o", col = rainbow(length(v2)))
99+
100+
##############################################################################################
101+
102+
# ScatterPlot
103+
input = mtcars[,c('mpg', 'wt')]
104+
plot(x = input$mpg, y = input$wt, xlim = c(10,40), ylim = c(0,6.5),
105+
xlab = "Miles per gallon", ylab = "Weight of the car", main = "Scatter Plot")
106+
107+
# Scatterplot with one hot encoding using pch
108+
with(iris, plot(Petal.Length, Petal.Width , pch = as.integer(Species)))
109+
110+
##############################################################################################
111+
112+
# Gamma Distribution as Histogram and density function
113+
# rgamma(n, shape, scale)
114+
sampledata = rgamma(500,1,2)
115+
print(sampledata)
116+
# Plotting Histogram and Density function for random gamma function generated
117+
hist(sampledata, prob = T)
118+
lines(density(sampledata))
119+
120+
##############################################################################################

Leaflet-Map.R

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# "leaflet" package
2+
# If using for first time, uncomment this line to install "leaflet" package
3+
# install.packages("leaflet")
4+
library(leaflet)
5+
6+
##############################################################################################
7+
8+
# Positioning marker in specified location using Leaflet - a JS "Map" library
9+
sample_map = leaflet() %>% addTiles() %>%
10+
addMarkers(lng = 80.161150, lat = 13.021940, popup = "Home")
11+
sample_map
12+
13+
##############################################################################################
14+
15+
# Locating 20 earthquake occured areas near Fiji since 1964 (quakes dataset)
16+
quake_data = quakes[1:20,]
17+
quake_data
18+
19+
quake_map = leaflet(data = quake_data) %>% addTiles() %>%
20+
addMarkers(lng = ~long, lat = ~lat, popup = ~as.character(mag))
21+
quake_map
22+
23+
##############################################################################################
24+
25+
# Emphasizing Labels for Markers
26+
#paste() -> To concatenate multiple strings and split them based on a seperator
27+
LabelContent = paste(sep = "<br>",
28+
"<b>KAILASH - LINKEDIN</b>",
29+
"<a href = 'https://linkedin.com/in/kailash-s-063207150'>KAILASH</a>"
30+
)
31+
32+
My_Home = leaflet() %>% addTiles() %>%
33+
addPopups(lng = 80.161150, lat = 13.021940,
34+
popup = LabelContent, options = popupOptions(closeButton = FALSE))
35+
My_Home
36+
37+
##############################################################################################
38+
39+
# Plotting Leaflet for customized input
40+
#read.csv -> reads a file in table format and generates dataframe for the same
41+
#Textconnection -> txt into csv file format
42+
dataset_df = read.csv(textConnection(
43+
"Name, Latitude, Longitude
44+
Samurai Noodele, 47.597131, -122.327298
45+
Kukai Ramen, 47.6154, -122.327157
46+
Tsukushinbo, 47.59987, -122.32678"))
47+
48+
dataset_df
49+
50+
custom_map = leaflet(data = dataset_df) %>% addTiles() %>%
51+
addMarkers(lng = ~Longitude, lat = ~Latitude, popup = ~as.character(Name))
52+
custom_map
53+
54+
##############################################################################################
55+
56+

0 commit comments

Comments
 (0)