Skewed Ratios in R Programming

setwd("C:/Users/Lenovo/OneDrive/Documents/Data Visualisation")

install.packages("tidyverse")
library(tidyverse)
install.packages("broom")
##dplyr
install.packages("ggplot2")
geom


library(ggplot2)
##ggplot2 for graphs
## data :- elements and the data present in the loaded file
## Aesthetics includes x- axis, y-axis, width, color
##geometrics : geoms --> specify what kind of graph to be plotted 

##facets : subset of the data
## statistics: intermediate, descriptive, smoothing, Binning
##Co-Ordinates: the space between data and dsiplay using cartesian, fixed, polar, limits
## Themes: non-data link

##Template
## ggplot(data=<DATA>)+ <GEOM_FUNCTION>(mapping = aes(<MAPPING>))
## ggplot(data=<DATA>), mapping = aes(<mapping>)) + <GEOM_FUNCTION>()

##ggplot(<DATa>, aes(<MAPPING> + <GEOM_FUNCTION>)
## ggplot(<DATA> + <GEOM_FUNCTION>)(aes(<mapppings>))

library(dplyr)
summary(mpg)
glimpse(mpg)
View(mpg)

ggplot(data=mpg)+ geom_bar(aes(x=displ, color=class))
ggplot(data=mpg)+geom_boxplot()
## first 6 observations are given by head
tail(mpg)
## last 6 observations are given by tail
ggplot(data = mpg)
ggplot(mpg, aes(x= class, y=displ))
ggplot(mpg, aes(x= class, y=displ)) + geom_point()
ggplot(mpg, aes(x= cty,y=displ)) + geom_point()
ggplot(mpg) + geom_histogram(aes(x=manufacturer), y="year")

ggplot(mpg) + geom_point(aes(x= displ, y =hwy,))
#segregating on the basis of class
ggplot(mpg) + geom_point(aes(x= displ, y =hwy, color=class))
# segregating on the basis of size
ggplot(mpg) + geom_point(aes(x= displ, y =hwy, size=class))

#segregating based on shape
ggplot(mpg) + geom_point(aes(x= displ, y= hwy, shape=class))
#segregating based on shades
ggplot(mpg) + geom_point(aes(x= displ, y= hwy, alpha=class))

ggplot(mpg) + geom_point(aes(x= displ, y =hwy, color='tomato', shape=6, alpha=0.1))

##Adding Title
ggplot(mpg) + geom_point(aes(x= displ, y =hwy, color=class))+ 
  ggtitle("car performance", subtitle= "MPG") +
  xlab("Displacement") +
  ylab("Highway Milage") +
  theme_grey() + 
  theme(axis.text.x= element_text(face = 'bold.italic',
                                  color = 'darkgreen',
                                  size = 10, angle = 180),
        axis.text.y= element_text(face = 'bold',
                                              color = 'blue',
                                              size = 10, angle = 90))
#facets for suplots-- different geometrical graphs for each of the diagram
ggplot(mpg) + geom_point(aes(x= displ, y =hwy)) + facet_wrap(~class, nrow=5)+
  ggtitle("car performance", subtitle= "MPG") +
  xlab("Displacement") +
  ylab("Highway Milage") +
  theme_grey() + 
  theme(axis.text.x= element_text(face = 'bold.italic',
                                  color = 'darkgreen',
                                  size = 10, angle = 180),
        axis.text.y= element_text(face = 'bold',
                                  color = 'blue',
                                  size = 10, angle = 90))


install.packages("ggplot2")
library(ggplot2)
summary(diamonds)
diamonds
print(diamonds)
View(diamonds)

##1. Bar Plot
ggplot(diamonds)+ geom_bar(aes(x= cut, color=cut))

##2.Percentage Bar Diagram


ggplot(diamonds)+ geom_bar(aes(x=cut, fill = clarity), alpha = 1 , position = "fill")


ggplot(diamonds)+ geom_bar(aes(x=cut, fill = clarity), alpha = 1/5, position = "fill")


##3. Grouped Bar Diagram

ggplot(diamonds)+ geom_bar(aes(x = cut, fill = clarity), alpha = 9/10, position = "dodge")

##4. Horizontal Bar Diagram

ggplot(diamonds)+ coord_flip()+ geom_bar(aes(x = cut, fill = clarity), alpha = 1, position = "dodge")

##5. Boxplot

ggplot(diamonds) + geom_boxplot(aes(x=cut, y=price, fill="pink")) 

##6. Titles & Labels
ggplot(diamonds, aes(x=cut, y=price, fill="pink")) + geom_boxplot() +  ggtitle("Diamonds", subtitle= "Quality") +
  xlab("Type of Cut") +
  ylab("Price") +
  theme_light() + theme(axis.text.x= element_text(face = 'italic',
                                  color = 'black',
                                  size = 8, angle = 0),
        axis.text.y= element_text(face = 'italic',
                                  color = 'black',
                                  size = 8, angle = 90))+
  theme(plot.title = element_text(face='bold.italic',
                                  size= 10, 
                                  color = 'darkblue'))

install.packages("ggplot2")
library(ggplot2)
summary(diamonds)
diamonds
print(diamonds)
View(diamonds)

jpeg(file= "boxplot1.jpg")

ggplot(diamonds) + geom_boxplot(aes(x=cut, y=price, fill="pink")) 
dev.off()

library(readxl)
prices <- read_excel("C:/Users/Lenovo/OneDrive/Documents/R Class Practice/Gold_silver_prices.xlsx")
View(prices)

##getwd() <-- to get the working directory
## to load excel files  use function --> read_excel("__") 
## the bracket should contain the path to the file 
## name it and use view function

library(janitor)
prices <- prices %>% clean_names(case="lower_camel")
prices <- prices %>% clean_names(case="snake")
## clean_names(data, case = c(snake, lowercamel, upper camel)) --> used to clean names, headings, they were capital and different before, now they all will be small cap)
##Line Graph
library(ggplot2)
line_plot <- ggplot(prices, mapping= aes(x=year ,y=gold)) + geom_line()
show(line_plot)
ggplot(prices) + geom_line(aes(x=year ,y=gold))+ geom_line(aes(x=year, y=silver))

install.packages("reshape2")
library(reshape2)
## reshape function changes the data from wide to long
data_long <- melt(prices, id.vars = "year")
head(data_long)
View(data_long)
## melt function will change the data format from wide to long form of data
ggplot(data_long, aes(x=year, y=value, color=variable)) + geom_line()

nifty_50 <- read.csv("C:/Users/Lenovo/OneDrive/Documents/R Class Practice/nifty_50.csv")
View(nifty_50)

#treemap
install.packages("treemap")
library(treemap)
treemap(nifty_50, index=c("Company","Industry"), vSize= "Weightage", 
        type="index", palette = "Spectral", title= "Nifty 50 Index Weightage", 
        fontsize.title=12)
## R is viewing the weighatge as a string variable due to the % sign
## so to remove it we use the as.numeric function to remove the % sign
nifty_50$Weightage <- as.numeric(sub("%","", nifty_50$Weightage))

treemap(nifty_50, index=c("Company","Industry"), vSize= "Weightage", 
        type="index", palette = "Spectral", title= "Nifty 50 Index Weightage", 
        fontsize.title=12)

RColorBrewer::display.brewer.all()

library(ggplot2)
nifty_50 <- read.csv("C:/Users/Lenovo/OneDrive/Documents/R Class Practice/nifty_50.csv")
View(nifty_50)
data(nifty_50)
attach(nifty_50)


##1.a Pie Chart

nifty_50$Weightage <- as.numeric(sub("%","", nifty_50$Weightage))
ggplot(nifty_50,aes(x="", y=Weightage, fill=Industry)) + 
  geom_bar(stat ="identity", width = 1)+  
  coord_polar("y", start = 0)
theme_void()+
  theme(legend.position = "bottom")

##2. Bubble Chart

nifty_50$Weightage <- as.numeric(sub("%","", nifty_50$Weightage))
ggplot(nifty_50, aes(Industry, Company, size=Weightage, color="rainbow")) + geom_point(alpha = 0.7)
+ scale_size(range = sizeRange)

##3.long to wide data form 
#first convert to long 
install.packages("reshape2")
library(reshape2)
prices <- read_excel("C:/Users/Lenovo/OneDrive/Documents/R Class Practice/Gold_silver_prices.xlsx")
View(prices)
library(janitor)
prices <- prices %>% clean_names(case="lower_camel")
data_long <- melt(prices, id.vars="year")
#now convert to wide 
data_wide <- dcast(data_long,year ~ variable)

setwd("C:/Users/Lenovo/OneDrive/Documents/Financial Economics (BASE)")
install.packages("tidyverse")
library(tidyverse)
library(quantmod)
tickers = c('SBIN.NS','RELIANCE.NS','TCS.NS','HDFCBANK.NS','ITC.NS')
getSymbols(tickers)

data<-do.call(merge,lapply(tickers, function(x) Ad(get(x))))

mydata<- fortify.zoo(do.call(merge,lapply(tickers, function(x) Ad(get(x)))))
View(mydata)
which(is.na(mydata))
mydata<-na.omit(mydata)

install.packages("writexl")
library(writexl)
write_xlsx(mydata, "stock_price.xlsx")
View(mydata)

install.packages("ggplot2")
install.packages("tidyverse")
library(tidyverse)
library(ggplot2)
library(dplyr)
library(readxl)


H_FI <- read_xlsx("C:/Users/Lenovo/Downloads/hfi.xlsx")
View(H_FI)
attach(H_FI)

plot1 <- ggplot(data = H_FI, aes(x= Month, y = CPI_Core))+
  geom_segment(aes(x =Month, xend= Month, y=CPI_Core, yend = CPI_Core), color= "blue", size=3)+ geom_point(color="red",size=5)
plot1
install.packages("gganimate")
library(gganimate)
 plot1+transition_time(Month)+
   labs(title="Month:{frame_time}")+ shadow_mark(alpha=0.5,size=0.8)
 plot1
 

library(dplyr)
install.packages('quantmod')
library(quantmod)
getSymbols("GAIL.NS",return.class = "data.frame",src = 'yahoo')
View(GAIL.NS)
getSymbols('GAIL.NS', return.class = 'data.frame',src = 'yahoo', from = '2017-04-02', to = '2018-03-12')
View(GAIL.NS)
chartSeries(GAIL.NS,type='line',theme=chartTheme('white'))
chartSeries(GAIL.NS,type='bar',theme=chartTheme('white'))
chartSeries(GAIL.NS,type='line',subset='2017-06', theme=chartTheme('white'))
gail <- GAIL.NS %>% mutate(Date = as.Date(row.names(.))) %>% select(Date, GAIL.NS.Adjusted) %>% rename(Adjuste_Close = GAIL.NS.Adjusted) %>% mutate(Company = 'GAIL')      
View(gail)

##PIE CHART USING GGPLOT##
ggplot(nifty50,aes(x="",y=Weightage,fill=Industry))+
  geom_bar()+
  coord_polar("y",start=0)+
  ggtitle("INDUSTRIAL WEIGHTAGE")+
  theme(plot.title = element_text(face ='bold.italic',
                                  size = 12,
                                  colour = 'black'))+
  geom_text(aes(x=1.5,label=Weightage),position = position_stack(vjust = 0))+
  scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9","#111111","#E70E00","#76B4E8","#56E7B9","#45B7F8","#23B5E8","#555555","#666666","#333333","#444444"))

##BUBBLE PLOT##
install.packages("rainbow")
library(rainbow)
ggplot(nifty50,aes(x=Industry,y=Weightage,size=Weightage))+
  geom_point(alpha=0.5,colour="53")+
  xlab("INDUSTRY")+
  theme(axis.title.x =element_text(face ='bold.italic',
                                   size = 8,
                                   colour = 'blue'))+
  ylab("WEIGHTAGE")+
  theme(axis.title.y = element_text(face = 'bold.italic',
                                    size = 8,
                                    colour = 'red'))

install.packages("ggplot2")
library(ggplot2)
summary(diamonds)
diamonds
print(diamonds)
View(diamonds)

jpeg(file= "boxplot1.jpg")

ggplot(diamonds) + geom_boxplot(aes(x=cut, y=price, fill="pink")) 
dev.off()

library(readxl)
prices <- read_excel("C:/Users/Lenovo/OneDrive/Documents/R Class Practice/Gold_silver_prices.xlsx")
View(prices)

##getwd() <-- to get the working directory
## to load excel files  use function --> read_excel("__") 
## the bracket should contain the path to the file 
## name it and use view function

library(janitor)
prices <- prices %>% clean_names(case="lower_camel")
prices <- prices %>% clean_names(case="snake")
## clean_names(data, case = c(snake, lowercamel, upper camel)) --> used to clean names, headings, they were capital and different before, now they all will be small cap)
##Line Graph
library(ggplot2)
line_plot <- ggplot(prices, mapping= aes(x=year ,y=gold)) + geom_line()
show(line_plot)
ggplot(prices) + geom_line(aes(x=year ,y=gold))+ geom_line(aes(x=year, y=silver))

install.packages("reshape2")
library(reshape2)
## reshape function changes the data from wide to long
data_long <- melt(prices, id.vars = "year")
head(data_long)
View(data_long)
## melt function will change the data format from wide to long form of data
ggplot(data_long, aes(x=year, y=value, color=variable)) + geom_line()

nifty_50 <- read.csv("C:/Users/Lenovo/OneDrive/Documents/R Class Practice/nifty_50.csv")
View(nifty_50)

#treemap
install.packages("treemap")
library(treemap)
treemap(nifty_50, index=c("Company","Industry"), vSize= "Weightage", 
        type="index", palette = "Spectral", title= "Nifty 50 Index Weightage", 
        fontsize.title=12)
## R is viewing the weighatge as a string variable due to the % sign
## so to remove it we use the as.numeric function to remove the % sign
nifty_50$Weightage <- as.numeric(sub("%","", nifty_50$Weightage))

treemap(nifty_50, index=c("Company","Industry"), vSize= "Weightage", 
        type="index", palette = "Spectral", title= "Nifty 50 Index Weightage", 
        fontsize.title=12)

RColorBrewer::display.brewer.all()

## Next Class create a pie chart using nifty 50 data with according to industries
## customization for treemaps`Preformatted text`
````Preformatted text`

Ehm, what is your question?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.