Skip to content

Commit 25f161d

Browse files
committed
new docs
1 parent d2a7fc8 commit 25f161d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+3226
-130
lines changed

Subjects/INT232 - DATA SCIENCE TOOLBOX R PROGRAMMING/.Rhistory

Whitespace-only changes.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
title: "Dashhboard"
3+
output:
4+
flexdashboard::flex_dashboard:
5+
orientation: columns
6+
vertical_layout: scroll
7+
---
8+
9+
```{r setup, include=FALSE}
10+
library(flexdashboard)
11+
library(knitr)
12+
library(ggplot2)
13+
library(plotly)
14+
library(dplyr)
15+
library(openintro)
16+
17+
library(DT)
18+
library(highcharter)
19+
```
20+
21+
Column {data-width=650}
22+
-----------------------------------------------------------------------
23+
24+
### Chart A
25+
26+
```{r}
27+
ggplot(mpg) +
28+
geom_bar(aes(x = class))
29+
30+
```
31+
32+
Column {data-width=500}
33+
-----------------------------------------------------------------------
34+
35+
### Chart B
36+
37+
```{r}
38+
ggplot(mtcars, aes(x = drat, y = mpg)) +
39+
geom_point()
40+
41+
```
42+
43+
### Chart C
44+
45+
```{r}
46+
val <-data.frame(course=c('DSA','C++','R','Python'),
47+
num=c(77,55,80,60))
48+
ggplot(data=val, aes(x=course, y=num, group=1)) +
49+
geom_line()+
50+
geom_point()
51+
52+
```
53+
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Define the cars vector with 5 values
2+
cars <- c(1, 3, 6, 4, 9)
3+
4+
# Graph the cars vector with all defaults
5+
plot(cars)
6+
7+
# Define the cars vector with 5 values
8+
cars <- c(1, 3, 6, 4, 9)
9+
10+
# Graph cars using blue points overlayed by a line
11+
plot(cars, type="o", col="blue")
12+
13+
# Create a title with a red, bold/italic font
14+
title(main="Autos", col.main="red", font.main=4)
15+
16+
# Define 2 vectors
17+
cars <- c(1, 3, 6, 4, 9)
18+
trucks <- c(2, 5, 4, 5, 12)
19+
20+
# Graph cars using a y axis that ranges from 0 to 12
21+
plot(cars, type="o", col="blue", ylim=c(0,12))
22+
23+
# Graph trucks with red dashed line and square points
24+
lines(trucks, type="o", pch=22, lty=2, col="red")
25+
26+
# Create a title with a red, bold/italic font
27+
title(main="Autos", col.main="red", font.main=4)
28+
29+
# Define 2 vectors
30+
cars <- c(1, 3, 6, 4, 9)
31+
trucks <- c(2, 5, 4, 5, 12)
32+
33+
# Calculate range from 0 to max value of cars and trucks
34+
g_range <- range(0, cars, trucks)
35+
36+
# Graph autos using y axis that ranges from 0 to max
37+
# value in cars or trucks vector. Turn off axes and
38+
# annotations (axis labels) so we can specify them ourself
39+
plot(cars, type="o", col="blue", ylim=g_range,
40+
axes=FALSE, ann=FALSE)
41+
42+
# Make x axis using Mon-Fri labels
43+
axis(1, at=1:5, lab=c("Mon","Tue","Wed","Thu","Fri"))
44+
45+
# Make y axis with horizontal labels that display ticks at
46+
# every 4 marks. 4*0:g_range[2] is equivalent to c(0,4,8,12). labels are parallel (=0) or perpendicular(=2) to axis
47+
axis(2, las=1, at=4*0:g_range[2])
48+
49+
# Create box around plot
50+
box()
51+
52+
# Graph trucks with red dashed line and square points
53+
lines(trucks, type="o", pch=22, lty=2, col="red")
54+
55+
# Create a title with a red, bold/italic font
56+
title(main="Autos", col.main="red", font.main=4)
57+
58+
# Label the x and y axes with dark green text
59+
title(xlab="Days", col.lab=rgb(0,0.5,0))
60+
title(ylab="Total", col.lab=rgb(0,0.5,0))
61+
62+
# Create a legend at (1, g_range[2]) that is slightly smaller
63+
# (cex) and uses the same line colors and points used by
64+
# the actual plots
65+
legend(1, g_range[2], c("cars","trucks"), cex=0.8,
66+
col=c("blue","red"), pch=21:22, lty=1:2);
67+
68+
#Let's start with a simple pie chart graphing the cars vector:
69+
# Define cars vector with 5 values
70+
cars <- c(1, 3, 6, 4, 9)
71+
72+
# Create a pie chart for cars
73+
pie(cars)
74+
#Now let's add a heading, change the colors, and define our own labels:
75+
# Define cars vector with 5 values
76+
cars <- c(1, 3, 6, 4, 9)
77+
78+
# Create a pie chart with defined heading and
79+
# custom colors and labels
80+
pie(cars, main="Cars", col=rainbow(length(cars)),
81+
labels=c("Mon","Tue","Wed","Thu","Fri"))
82+
data2=c(1,2,4,5,3,3,4,6,7,8)
83+
data2
84+
hist(data2)
85+
colors()
86+
colours()
87+
lines(density(data2),lwd=2)
88+
hist(data2,col='gray75',main=NULL,xlab='size class for data2',ylim=c(0,0.3),freq=FALSE)
89+
90+
dens=density(data2)
91+
dens
92+
names(dens)
93+
str(dens)
94+
plot(dens$x,dens$y)
95+
plot(density(data2))
96+
normde=rnorm(n=500,m=24.2,sd=2.2)
97+
hist(normde)
98+
normde$break
99+
100+
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#*********** GGPLOT2 GRAMMAR OF GRAPHICS ***********#
2+
# #
3+
# COMPONENTS OF GRAMMAR OF GRAPHICS #
4+
# 1. DATA : the dataset #
5+
# 2. AESTHETICS : the metric onto which we plot data #
6+
# 3. GEOMETRY : visual elements to plot the data #
7+
# 4. FACET : groups by which we divide the data #
8+
#***********************************************************#
9+
10+
library(ggplot2)
11+
12+
#*** SCATTERPLOTS ***#
13+
ggplot(data=iris, aes(y=Petal.Length,
14+
x=Sepal.Length))+geom_point()
15+
ggplot(data=iris, aes(y=Petal.Length,
16+
x=Sepal.Length,col=Species))+geom_point()
17+
ggplot(data=iris, aes(y=Petal.Length, x=Sepal.Length,
18+
shape=Species))+geom_point()
19+
ggplot(data=iris, aes(y=Petal.Length, x=Sepal.Length, col=Species,
20+
shape=Species))+geom_point()
21+
22+
str(house)
23+
24+
house<-read.csv(file.choose(),header = TRUE)
25+
library(dplyr)
26+
house1<- house[,-1]
27+
house1 <- house %>% select(c(-1))
28+
View (house)
29+
#histogram
30+
ggplot(data = house, aes(x=price))+ geom_histogram()
31+
#ggplot(data = house, aes(x=price))+ geom_histogram(bin=50)
32+
ggplot(data=house,aes(x=price))+
33+
geom_histogram(bins=50,fill="brown")
34+
ggplot(data=house, aes(x=price))+
35+
geom_histogram(bins=50,fill="brown",col="black")
36+
ggplot(data=house, aes(x=price, fill=air_cond))+geom_histogram
37+
(bins=50)
38+
ggplot(data=house, aes(x=price, fill= factor(air_cond)
39+
))+
40+
geom_histogram(bins=50,position="fill")
41+
42+
#***** BARPLOT *******#
43+
#to see distribution of continous variable we use histogram
44+
#to see distribution of categorical variable we use barplot
45+
ggplot(data=house, aes(x=waterfront))+geom_bar()
46+
ggplot(data=house, aes(x=waterfront,
47+
fill=air_cond))+geom_bar()
48+
ggplot(data=house, aes(x=waterfront,
49+
fill=air_cond))+geom_bar(position = "fill")
50+
ggplot(data=house, aes(x=waterfront,
51+
fill=sewer))+geom_bar(position="fill")
52+
53+
#**** FREQUENCY-POLYGON ******#
54+
# an alternative to a histogram used to see a distribution of continous #variable
55+
ggplot(data=house, aes(x=price))+geom_freqpoly()
56+
#increase variation
57+
ggplot(data=house, aes(x=price))+geom_freqpoly(bins=50)
58+
ggplot(data=house, aes(x=price))+geom_freqpoly(bins=100)
59+
ggplot(data=house, aes(x=price,
60+
col=air_cond))+geom_freqpoly(bins=60)
61+
62+
# Modify formatting of axis
63+
pl + scale_x_continuous(labels = comma)
64+
65+
pl + # Remove axis labels & ticks
66+
theme(axis.text.x = element_blank(),
67+
axis.ticks.x = element_blank(),
68+
axis.text.y = element_blank(),
69+
axis.ticks.y = element_blank())
70+
71+
#**** BOXPLOTS *****#
72+
# how does continous var change w.r.t. ategorical var
73+
#outliers are beyond the avg value
74+
ggplot(data=house, aes(x=factor(rooms),
75+
y=price))+geom_boxplot()
76+
ggplot(data=house, aes(x=factor(rooms),
77+
y=price, fill=factor(rooms)))+geom_boxplot()
78+
ggplot(data=house, aes(x=factor(rooms),
79+
y=price, fill=air_cond))+geom_boxplot()
80+
ggplot(data=house, aes(x=factor(rooms),
81+
y=price, fill=sewer))+geom_boxplot()
82+
83+
84+
#***** Smooth-Line ******#
85+
#how does one continous variable change w.r.t to other continous var
86+
ggplot(data=house,
87+
aes(y=price, x=living_area))+geom_smooth()
88+
89+
ggplot(data=house, aes(y=price, x=living_area),
90+
col=air_cond))+geom_smooth(se=F)
91+
ggplot(data=house, aes(y=price, x=living_area),
92+
col=heat))+geom_smooth(se=F)
93+
94+
#**** Applying "lm" (linear model) method ****#
95+
ggplot(data=house, aes(y=price, x=living_area))+geom_point()+
96+
geom_smooth(method="lm",se=F)
97+
ggplot(data=house, aes(y=price, x=living_area,
98+
col=air_cond))+geom_point()+
99+
geom_smooth(method="lm",se=F)
100+
ggplot(data=house, aes(y=price, x=living_area,
101+
col=heat))+geom_point()+
102+
geom_smooth(method="lm",se=F)
103+
104+
##facets
105+
ggplot(data=house, aes(y=price, x=living_area,
106+
col=air_cond))+geom_point()+
107+
geom_smooth(method="lm",se=F)+facet_grid(~air_cond)
108+
ggplot(data=house, aes(y=price, x=living_area,
109+
col=fireplaces))+geom_point()+
110+
geom_smooth(method="lm",se=F)+facet_grid(~fireplaces)
111+
ggplot(data=house, aes(y=price, x=age,
112+
col=fireplaces))+geom_point()+
113+
geom_smooth(method="lm",se=F)+facet_grid(~fireplaces)
114+
115+

0 commit comments

Comments
 (0)