How to add gradient to ridgeplot?

`ggplot(bangaloreweather,aes(x=so2, y=month,fill="red"))+
geom_density_ridges_gradient(alpha=0.8, scale = 0.9, rel_min_height = 0.01) +
theme_ipsum()+
theme(legend.position="none")+
labs(title = 'Sulfur dioxide in the air per month', subtitle="Bangalore")+
xlab("Amount of sulphur dioxide")+
ylab("Month")+
theme(axis.title.x = element_text(face="bold", color="darkblue", size=10,hjust=0.5)) +
theme(axis.title.y = element_text(face="bold", color="darkblue", size=10,hjust=0.5))+

###was unable to make it into gradient
scale_fill_brewer(type=fill, palette = "YlOrRd")
###This code used in example didnt work, package virgis not available
###scale_fill_viridis(name = "Temp. [F]", option = "C")`

2 January 2023 class

#End result is same, only equation to get the result is a little different

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

#ggplot(<DATA>, aes(<MAPPINGS>)) + <GEOM_FUNCTION>()
#ggplot(<DATA>) + <GEOM_FUNCTION>(aes(<MAPPINGS))


#how to change library you are working in
library(dplyr)
#mpg dataset is already a part of dplyr package
#How to see basic descriptive statistics
library(tidyverse)
library(ggplot2)
summary(mpg)
glimpse(mpg)
#head gives you first 5 observations of the data
head(mpg)
#tail gives you last 5 observations
tail(mpg)
#Plotting the data
ggplot(data=mpg)
#You can also write simply mpg
ggplot(mpg)
#Doesnt plot data, we need to add an aesthetic layer
ggplot(mpg, aes(x=class, y=displ))
#^^^^ This shows the graph with 2 variables, but data isnt plotted
#^^^^^ This is because the geometrics are not specified, so R doesnt know how to plot
#Also class is categorical variable, so no point
#Plotting graph properly now
ggplot(mpg, aes(x=cty, y=displ)) + geom_point()
#For histogram, you can only have 1 aesthetic!
#Histogram on x axis (vertical)
ggplot(mpg, aes(x=hwy)) + geom_histogram()
#Histogram on y axis (horizontal)
ggplot(mpg, aes(y=hwy)) + geom_histogram()

#Now we try to type the function in another way    
ggplot(mpg) + geom_point(aes(x=displ, y=hwy))
#Segregating based on class, manufacturer, transmission of cars (Differentiating between cars based on colour)
ggplot(mpg) + geom_point(aes(x=displ, y=hwy, color=class))
ggplot(mpg) + geom_point(aes(x=displ, y=hwy, color=manufacturer))
ggplot(mpg) + geom_point(aes(x=displ, y=hwy, color=trans))

#Segregating based on shape of cars
ggplot(mpg) + geom_point(aes(x=displ, y=hwy, shape=class))

#Segregating based on shape as well as colour
ggplot(mpg) + geom_point(aes(x=displ, y=hwy, shape=trans, color=manufacturer))

#alpha = Change colour transparency
#When assigning colour, class etc. a specific colour etc., need to close brackets of aesthetics! ;it becomes a dimension within dataset
ggplot(mpg) + geom_point(aes(x=displ, y=hwy), shape=6, color='tomato', alpha=0.1)

#Showing the entire data in a spreadsheet format
view(mpg)

summary(mpg)
view(mpg)
head(mpg)
tail(mpg)

#Adding title
#xlab = Labelling x axis
#ylab = labelling y axis
# !!!! When writing codes in multiple lines, use + sign
ggplot(mpg) + geom_point(aes(x=displ,y=hwy, color=class)) +
  ggtitle("car performance", subtitle="MPG") +
  xlab("Displacement") +
  ylab("Highway Milage") +
  theme_grey() + 
  #Elements in x axis change to darkgreen colour, rotate by 180
  #Changes size to 10, text becomes bold and italic
  theme(axis.text.x = element_text(face='bold.italic',
                                   color = 'darkgreen',
                                   size = 10, angle = 180),
  #Elements in y axis change to blue colour, angle 90
  #Changes size to 20, text becomes blue
        axis.text.y = element_text(face = 'bold',
                                   color = 'blue',
                                   size = 20, angle = 90))

#Making changes to the legend in the graph - HOMEWORK

#Facets - We use when we have different categories and we want to club them
#Facets is for subplots
#We use ~ because we say we are looking at only 1 variable class
ggplot(mpg) + geom_point(aes(x=displ,y=hwy)) +facet_wrap(~ class, nrow=2)
ggplot(mpg) + geom_point(aes(x=displ,y=hwy)) +facet_wrap(~ class, nrow=7)
ggplot(mpg) + geom_point(aes(x=displ,y=hwy)) +facet_wrap(~ class, ncol=2)
ggplot(mpg) + geom_point(aes(x=displ,y=hwy)) +facet_wrap(~ class, ncol=10)

#What this means?
ggplot(mpg) + geom_point(aes(x=displ,y=hwy)) +facet_grid(drv~ cyl)


#Learn how to change title, subtitle
#Create 2-3 types of bar diagrams; grouped, horizontal, percentage
#Create one of the plots sir already gave and change title, size, color etc.
#Try making boxplot diagrams
#Go to R graph gallery
#Search on google - eg. ggplot2 percentage box plot - eg. Geek4Geeks










#### 4 January 2023 class
library(tidyverse)





jpeg(file="filename.jpeg")

#Changing dimensions of a saved graph?
#Just go to export > Change width and height in options

#Data extraction
#How to import excel file?
#In environment, click on spreadsheet (import dataset) > Choose excel 
#> Choose file > Choose sheet > Choose range > Skip?
#Or for csv, choose text(base)
#But how to import with codes?
library(readxl)
viewsheet <- read_excel("Gold_silver_prices.xlsx")  
view(viewsheet)


#Cleaning names
#Usually, we keep all variable names in small letters
#In R, we also dont put spaces between variables, but we put a connector
#Eg. gold_prices instead of gold prices
#There are packages that help us do this, eg. janitor
library(janitor)
#%>% is pipe operator
##For all cases https://www.rdocumentation.org/packages/janitor/versions/1.2.0/topics/clean_names
viewsheet<- viewsheet %>% clean_names(case="lower_camel")
view(viewsheet)
#This assigned the excel sheet "Gold_silver_prices.xlxs" to the variable "viewsheet"



#We can also assign a variable to quickly show the different graphs!
#Use show() instead of view() to show graph
line_graph <- ggplot(viewsheet, mapping=aes(x=year, y=gold))+
  geom_line()
show(line_graph)



#Plotting line graph for viewsheet (gold_silver_prices)
ggplot(viewsheet, mapping=aes(x=year, y=gold))+
  geom_line()
#Can also write command like this
ggplot(viewsheet)+
  geom_line(mapping=aes(x=year, y=gold))
#Plotting both gold and silver prices together
ggplot(viewsheet)+ 
  geom_line(aes(x=year, y=gold))+
  geom_line(aes(x=year, y=silver))
#However, if you have large number of variables eg. 100+, we cant add every variable individually
#To solve this, we can go ahead and change the structure of the data
#We can change the data into long format
#We use the reshape2 package to convert data from wide format to long format, vice versa
install.packages("reshape2")
library("reshape2")
#Changing data into long form
data_long <- melt(viewsheet, id.vars= "year")
view(data_long)
head(data_long)
library(ggplot2)
library(dplyr)
line_plot<-ggplot(data_long, aes(x=year, y=value, color=variable)) +  
  geom_line()
show(line_plot)

#To do: Treemap
# Use secondary axis

#Reading csv files
#nifty_50 <-read.csv("Exact file location")
install.packages("treemap")
library(treemap)
view(nifty_50)
library(janitor)
nifty_50<- nifty_50 %>% clean_names(case="lower_camel")
#Type=index

#How to remove percentage sign from some data
nifty_50$weightage<-as.numeric(sub("%","",nifty_50$weightage))
#We remove percentage sign because it gives errors otherwise
#sub() = substitute
#We put 2 "" without any spaces to remove anything completely
#We put $ sign before weightage to specify it is weightage that gets affected in "nifty_50" dataset


treemap(nifty_50, 
        index=c("company","industry"),
        vSize="weightage",
        type="index",
        palette="Spectral",
        title="Nifty 50 Index weightage",
        fontsize.title=12)




##RColorBrewe::display.brewer.all()
#Next class - reate pie chsrt using nifty 50 data   













#### 4 January 2023 class
#Making graphs on our own

#loading libraries
library(dplyr)
library(ggplot2)
library(janitor)
library(tidyverse)
library(readxl)
library(ggridges)
library(hrbrthemes)


#No line, bar, scatter graphs please!

#If there is blank cell without data
#which(is.na("state_gdp$gdp_growth"))
#This gives cells which have empty values
#How to eliminate missing data?
#na.omit("state.gdp")
#How to eliminate only once specific cell?

bangaloreweather<-read.csv("bangalore.csv")
view(bangaloreweather)
##Cleaning names
bangaloreweather<-bangaloreweather %>% clean_names(case="small_camel")
view(bangaloreweather)


##### Failed command to convert months into vectors so that months are in order in graph
### bangaloreweather$month <- factor(bangaloreweather$month, levels = c
                     ## ("January", "February", "March", "April", "May", "June", "July", "August", 
                                            ##  "September", "October", "November"))



ggplot(bangaloreweather,aes(x=so2, y=month,fill="red"))+
  geom_density_ridges_gradient(alpha=0.8, scale = 0.9, rel_min_height = 0.01) +
  theme_ipsum()+
    theme(legend.position="none")+
labs(title = 'Sulfur dioxide in the air per month', subtitle="Bangalore")+
  xlab("Amount of sulphur dioxide")+
  ylab("Month")+
  theme(axis.title.x = element_text(face="bold", color="darkblue", size=10,hjust=0.5)) +
  theme(axis.title.y = element_text(face="bold", color="darkblue", size=10,hjust=0.5))+
  ###was unable to make it into gradient
  scale_fill_brewer(type=fill, palette = "YlOrRd")
  ###This code used in example didnt work, package virgis not available
  ###scale_fill_viridis(name = "Temp. [F]", option = "C")

#How to add gradient?????

In example, they used

  install.packages("inferno")
  install.packages("hrbrthemes")
  install.packages("virgis")

Hi, perhaps you could provide a reproducible example of bangaloreweather

#### R codes data extraction

### 10 January 2023 class

install.packages("quantmod")
install.packages("rvest")
library(tidyverse)
library(quantmod)
library(ggplot2)
##Quantmod helps you to use Yahoo finance or Google as a source and then extract the data
##You need to define the duration as well, i.e which days you need the data for
library(rvest)


#getting data
#src = Source
#We generally look at Adjusted price 
getSymbols("MSFT", return.class="data.frame", src='yahoo', from ="2022-01-01", to ="2022-12-30")
view(MSFT)
getSymbols("ADANIENT.NS", return.class="data.frame", src='yahoo', from ="2022-01-01", to ="2022-12-30")
getSymbols("ADANIENT.NS", return.class="data.frame", src='yahoo')
view(ADANIENT.NS)
#If you get stock prices from BSE, use .BO; NSE use .NS
TCS
Op(ADANIENT.NS)
Lag(ADANIENT.NS)
monthlyReturn(ADANIENT.NS)

#Trying to plot a graph

chartSeries(ADANIENT.NS, type="line", theme=chartTheme("white"))
chartSeries(ADANIENT.NS, type="bar", theme=chartTheme("white"))
chartSeries(ADANIENT.NS, type="candlesticks", theme=chartTheme("white"), subset ='2017-05')

chartSeries(MSFT, type="line", theme=chartTheme("white"))


#We cant plot this chart in a conventional way because
library(dplyr)
#as.date() function is used to tell that this value is a date
#row.names(.) because the date column in data is blank
#We are adding a new variable from the row that is blank

#problem is that there is not a time variable eg. Month
#Therefore, we do a transformation in first column so we can plot the data

#mutate() function is used to add more variables to R. Here, 'Date' variable is added
adanient<-ADANIENT.NS %>% mutate(Date=as.Date(row.names(.))) %>%
  select(Date, ADANIENT.NS.Adjusted) %>% rename(Adjusted_Close = ADANIENT.NS.Adjusted) %>%
  mutate(Company="Adani Enterprises")
view(adanient)  

msft<-MSFT %>% mutate(Date=as.Date(row.names(.))) %>%
  select(Date, MSFT.Adjusted) %>% rename(Adjusted_Close = MSFT.Adjusted) %>%
  mutate(Company="Microsoft")
view(msft)


  ggplot(adanient, aes(x=Date, y=Adjusted_Close, color=Company)) + 
  geom_line(size=1)+
  labs(title= "Adjusted")
  
  ggplot(msft, aes(x=Date, y=Adjusted_Close, color=Company)) + 
    geom_line(size=1)+
    labs(title= "Adjusted")

  

combine<-rbind(adanient,msft)

view(combine)

ggplot(combine, aes(x=Date, y=Adjusted_Close, color=Company)) + 
  geom_line(size=1)+
  labs(title= "Adjusted")


#Gold (XAU) and silver (XAG)
#Sys.Date() = Today's date
#Sys.Date()-180 = 6 months before today
getMetals(c('XAU', 'XAG'), from=Sys.Date()-180)
view(XAGUSD)

###How to combine rows
#base R's merge() function
#dplyr join family
#rbind()function


library(dplyr)
combined <- left_join(silver,gold,by=c("date"))
#Silver comes on left, gold comes on right
view(combined)

joined<-merge(gold,silver,by.x="date",
              by.y="date", all.x = TRUE, all.y = FALSE)
view(joined)


###Web scrapping
#Extracting data from websites
library(rvest)
library(tidyverse)
wiki_link<-"https://en.wikipedia.org/wiki/Economy_of_India"

#Asking R to read the wiki link
wiki_page<-read_html(wiki_link)

#Asking R to read the wiki page and then look at html nodes
gdp_table= wiki_page %>% 
  #The problem is that the page might have other tables as well, so we look at "table" table only
  #We look at 3rd element (gdp)
  #Put . before [[1] to show there is nothing before it
  html_nodes("table") %>%
  #Identify which table you are looking for (3rd elemebt, 1st position)
  .[3] %>% html_table()%>%.[[1]]

view(gdp_table)


#trying web scrapping on my own

wiki_link2<-"https://en.wikipedia.org/wiki/Economy_of_China"
wiki_page2<-read_html(wiki_link)
gdp_table2= wiki_page2 %>% 
  html_nodes("table") %>%
  .[4] %>% html_table()%>%.[[1]]

view(gdp_table2)



##Back series data = Base year adjustment

##Asgn 1

ggplot(data=diamonds, mapping=aes(x=cut)) +geom_bar() + 
  ggtitle("Quality of diamonds", subtitle="Cut") +
  xlab("Cut quality") +
  ylab("Frequency") +
  theme(axis.title.x = element_text(face="bold", color="darkblue", size=10)) +
  theme(axis.title.y = element_text(face="italic", color="darkblue", size=10)) +
  theme(axis.text= element_text(face="italic", color = "black", size = 8))




#2.Plotting a percentage bar plot
ggplot(data=diamonds, mapping=aes(x=cut, y=value, fill=clarity)) +
  geom_bar(position="fill", stat="identity") + 
  ggtitle("Quality of diamonds", subtitle="Cut") +
  xlab("Cut quality") +
  ylab("Percentage") +
  theme(axis.title.x = element_text(face="bold", color="darkblue", size=10)) +
  theme(axis.title.y = element_text(face="italic", color="darkblue", size=10)) +
  theme(axis.text= element_text(face="italic", color = "black", size = 8)) +
  theme(legend.position = "bottom")+
  guides(fill=guide_legend("Legend"))




#3. Plotting a grouped bar plot (DOUBT!!!!!!)
#Could follow the logic by searching online, but values in graph are weird?
#Could you go through this one in class please, thanks!
ggplot(data=diamonds, mapping=aes(x=cut, fill=clarity)) +
  geom_bar(position = "dodge", stat = "count")+
  ggtitle("Quality of diamonds", subtitle="Cut") +
  xlab("Cut quality") +
  ylab("Frequency") +
  theme(axis.title.x = element_text(face="bold", color="darkblue", size=10)) +
  theme(axis.title.y = element_text(face="italic", color="darkblue", size=10)) +
  theme(axis.text= element_text(face="italic", color = "black", size = 8)) +
  theme(legend.position = "bottom")+
  guides(fill=guide_legend("Legend"))





#4. Plotting horizontal bar diagram
ggplot(data=diamonds, mapping=aes(y=cut,fill=clarity)) +geom_bar() + 
  ggtitle("Quality of diamonds", subtitle="Cut") +
  xlab("Frequency") +
  ylab("Cut quality") +
  theme(axis.title.x = element_text(face="italic", color="darkblue", size=10)) +
  theme(axis.title.y = element_text(face="bold", color="darkblue", size=10)) +
  theme(axis.text= element_text(face="italic", color = "black", size = 8)) +
  theme(legend.position = "bottom")+
  guides(fill=guide_legend("Legend"))




#5. Creating a boxplot
ggplot(data=diamonds, mapping=aes(x=cut, y=price, fill=clarity)) + geom_boxplot() + 
  geom_jitter(position="jitter")
  ggtitle("Quality of diamonds", subtitle="Cut") +
  xlab("Cut quality") +
  ylab("Frequency") +
  theme(axis.title.x = element_text(face="bold", color="darkblue", size=10)) +
  theme(axis.title.y = element_text(face="italic", color="darkblue", size=10)) +
  theme(axis.text= element_text(face="italic", color = "black", size = 8)) +
  theme(legend.position = "bottom")+
    guides(fill=guide_legend("Legend"))
library(tidyverse)
library(ggplot2)
library(dplyr)
library(reshape2)



##Asgn 2 

###1. Making pie chart with nifty_50 data

#seeing dataset in spreadsheet
library(readxl)
nifty_50<-read.csv("nifty_50.csv")
view(nifty_50)

#We remove the percentage sign from weightage as it gives errors
nifty_50$weightage<-as.numeric(sub("%","",nifty_50$weightage))
view(nifty_50)

#We convert all column names to lower case for simplicity
library(janitor)
nifty_50<- nifty_50 %>% clean_names(case="lower_camel")

#Changing data into long form
nifty_50_long <- melt(nifty_50, id.vars= "weightage")
view(nifty_50_long)

#Could not figure out how to combine all data of industries into one sum
#Would appreciate you going over it in class
pie_chart<- ggplot(nifty_50, aes(x="", y=weightage, fill=industry)) + 
  geom_bar(stat="identity", width=0.5, color="black") +
  coord_polar("y")+
  ggtitle("Pie chart showing weightage of different types of industry in Nifty 50 dataset")+    
  guides(fill=guide_legend("Legend"))+
  theme_bw()+
  theme(plot.title = element_text(hjust = 0.5, size = 10),
        axis.title = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        panel.grid=element_blank())+
  scale_fill_brewer(palette="Set3")+
  geom_text(aes(label = paste0(round(weightage,0), "%")), 
            position = position_stack(vjust = 0.5))


show(pie_chart)


#Failed attempt to try and add all values together
view(nifty_50)
nifty_50_crop<-nifty_50[c(-1,-2)]
view(nifty_50_crop)
nifty_50_long <- melt(nifty_50_crop, id.vars= "weightage")
view(nifty_50_long)
rowSums(nifty_50_crop)
summary(nifty_50_crop)



###2. Making a bubble chart


bubble_chart<-ggplot(nifty_50,aes(x=industry,y=weightage, size=weightage, label=nseSymbol))+
  geom_point(aes(color = industry), alpha = 0.5)+
  geom_text(size=1.8,hjust=-0.2)+
  theme(axis.title.x = element_blank())+
  theme(axis.text.x = element_blank())+
  scale_fill_brewer(palette="Paired")+
  ggtitle("Bubble chart showing weightage of different companies according to industry")+
  theme(plot.title = element_text(hjust = 0.5, size = 10))+
  guides(fill=guide_legend("Legend"))
#How to change legend title when there are multiple legends?
#Was unable to change color palette for bubble chart

show(bubble_chart)








###3. Converting long form of data to wide form
library(readxl)
goldsilver<-read_excel("Gold_silver_prices.xlsx")
view(goldsilver)

#Cleaning data
library(janitor)
goldsilver<- goldsilver %>% clean_names(case="lower_camel")

#Converting data to long form
library(reshape2)
goldsilver_long<- melt(goldsilver, id.vars= "year")
view(goldsilver_long)

#Converting data from long from to wide form
####### This can be done by melt() function also???
goldsilver_wide<-dcast(goldsilver_long, variable ~ year, value.var="value")
view(goldsilver_wide)
##Asgn 3

library(dplyr)
library(ggplot2)
library(janitor)
library(tidyverse)
library(readxl)
library(ggridges)
library(hrbrthemes)


##Dataset is titled "bangalore.csv"

bangaloreweather<-read.csv("bangalore.csv")
view(bangaloreweather)
##Cleaning names
bangaloreweather<-bangaloreweather %>% clean_names(case="small_camel")
view(bangaloreweather)


##### Failed command to convert months into vectors so that months are in order in graph

### bangaloreweather$month <- factor(bangaloreweather$month, levels = c
## ("January", "February", "March", "April", "May", "June", "July", "August", 
##  "September", "October", "November"))


##Final code
ggplot(bangaloreweather,aes(x=so2, y=month,fill="red"))+
  geom_density_ridges_gradient(alpha=0.8, scale = 0.9, rel_min_height = 0.01) +
  theme_ipsum()+
  theme(legend.position="none")+
  labs(title = 'Sulfur dioxide in the air per month', subtitle="Bangalore")+
  xlab("Amount of sulphur dioxide")+
  ylab("Month")+
  theme(axis.title.x = element_text(face="bold", color="darkblue", size=10,hjust=0.5)) +
  theme(axis.title.y = element_text(face="bold", color="darkblue", size=10,hjust=0.5))+
  ###was unable to make it into gradient
  scale_fill_brewer(type=fill, palette = "YlOrRd")
###This code used in example didnt work, package virgis not available
###scale_fill_viridis(name = "Temp. [F]", option = "C")


#How to add gradient?????
# In example, they used "virgis" package and code, but it is not available in this R version



install.packages("inferno")
install.packages("hrbrthemes")
install.packages("virgis")
#Asgn 4

library(rvest)
library(tidyverse)
library(janitor)

wiki_link2<-"https://en.wikipedia.org/wiki/Economy_of_China"
wiki_page2<-read_html(wiki_link2)
gdp_table2= wiki_page2 %>% 
  html_nodes("table") %>%
  .[4] %>% html_table()%>%.[[1]]

view(gdp_table2)



###Part 2 of assignment - Cleaning up the data



#Cleaning up the column headings with clean_names() command
gdp_table2_clean<-gdp_table2 %>% clean_names(case="lower_camel")
view(gdp_table2_clean)


#Removing N/A values



#This code replaces "n/a" values with "NA" (italics)
gdp_table2_clean[gdp_table2_clean == "n/a"] <- NA
view(gdp_table2_clean)

#Dropping N/A values - Way 1
finaltable<-na.omit(gdp_table2_clean)
view(finaltable)
#Dropping N/A values - Way 2
finaltable2<-gdp_table2_clean %>% drop_na()
view(finaltable2)


#Removing percentage (%) signs from unemployment and inflation
finaltable$inflationRateInPercent<-as.numeric(sub("%","",finaltable$inflationRateInPercent))
finaltable$unemploymentInPercent<-as.numeric(sub("%","",finaltable$unemploymentInPercent))

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.