###1
library(heatmaply)
## Warning: package 'heatmaply' was built under R version 4.1.3
## Loading required package: plotly
## Warning: package 'plotly' was built under R version 4.1.3
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.1.3
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
## Loading required package: viridis
## Warning: package 'viridis' was built under R version 4.1.3
## Loading required package: viridisLite
## Warning: package 'viridisLite' was built under R version 4.1.3
##
## ======================
## Welcome to heatmaply version 1.4.0
##
## Type citation('heatmaply') for how to cite the package.
## Type ?heatmaply for the main documentation.
##
## The github page is: https://github.com/talgalili/heatmaply/
## Please submit your suggestions and bug-reports at: https://github.com/talgalili/heatmaply/issues
## You may ask questions at stackoverflow, use the r and heatmaply tags:
## https://stackoverflow.com/questions/tagged/heatmaply
## ======================
mat <- mtcars
mat[] <- paste("This cell is", rownames(mat))
mat[] <- lapply(colnames(mat), function(colname) {
paste0(mat[, colname], ", ", colname)
})
heatmaply(
percentize(mtcars),
xlab = "Features",
ylab = "Cars",
main = "Data transformation using 'percentize'",
custom_hovertext = mat
)
###1 In Base R
library(wesanderson)
## Warning: package 'wesanderson' was built under R version 4.1.3
library(ggplot2)
# Discrete color
bp <- ggplot(iris, aes(Species, Sepal.Length))
bp + scale_fill_manual(values = wes_palette("GrandBudapest1", n = 3))
# Gradient color
pal <- wes_palette("Zissou1", 100, type = "continuous")
ggplot(heatmap, aes(x = X2, y = X1, fill = value)) +
geom_tile() +
scale_fill_gradientn(colours = pal) +
scale_x_discrete(expand = c(0, 0)) +
scale_y_discrete(expand = c(0, 0)) +
coord_equal()
###1 IN Base R
df <- scale(mtcars)
heatmap(df, scale = "none")
col<- colorRampPalette(c("red", "white", "blue"))(256)
library("RColorBrewer")
## Warning: package 'RColorBrewer' was built under R version 4.1.3
col <- colorRampPalette(brewer.pal(10, "RdYlBu"))(256)
heatmap(df, scale = "none", col = col,
RowSideColors = rep(c("blue", "pink"), each = 16),
ColSideColors = c(rep("purple", 5), rep("orange", 6)))
library("gplots")
## Warning: package 'gplots' was built under R version 4.1.3
##
## Attaching package: 'gplots'
## The following object is masked from 'package:stats':
##
## lowess
heatmap.2(df, scale = "none", col = bluered(100),
trace = "none", density.info = "none")
library("pheatmap")
## Warning: package 'pheatmap' was built under R version 4.1.3
pheatmap(df, cutree_rows = 4)
###2
# Load packages
library(shiny)
## Warning: package 'shiny' was built under R version 4.1.3
library(shinythemes)
## Warning: package 'shinythemes' was built under R version 4.1.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.1.3
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(readr)
## Warning: package 'readr' was built under R version 4.1.3
# Load data
trend_data <- read_csv("D:/charan/Documents/FIFA 18/STAT651/trend_data.csv")
## Rows: 454 Columns: 3
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (1): type
## dbl (1): close
## dttm (1): date
##
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
trend_description <- read_csv("D:/charan/Documents/FIFA 18/STAT651/trend_description.csv")
## Rows: 5 Columns: 2
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (2): type, text
##
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Define UI
ui <- fluidPage(theme = shinytheme("lumen"),
titlePanel("Google Trend Index"),
sidebarLayout(
sidebarPanel(
# Select type of trend to plot
selectInput(inputId = "type", label = strong("Trend index"),
choices = unique(trend_data$type),
selected = "Travel"),
# Select date range to be plotted
dateRangeInput("date", strong("Date range"), start = "2007-01-01", end = "2017-07-31",
min = "2007-01-01", max = "2017-07-31"),
# Select whether to overlay smooth trend line
checkboxInput(inputId = "smoother", label = strong("Overlay smooth trend line"), value = FALSE),
# Display only if the smoother is checked
conditionalPanel(condition = "input.smoother == true",
sliderInput(inputId = "f", label = "Smoother span:",
min = 0.01, max = 1, value = 0.67, step = 0.01,
animate = animationOptions(interval = 100)),
HTML("Higher values give more smoothness.")
)
),
# Output: Description, lineplot, and reference
mainPanel(
plotOutput(outputId = "lineplot", height = "300px"),
textOutput(outputId = "desc"),
tags$a(href = "https://www.google.com/finance/domestic_trends", "Source: Google Domestic Trends", target = "_blank")
)
)
)
# Define server function
server <- function(input, output) {
# Subset data
selected_trends <- reactive({
req(input$date)
shiny::validate(need(!is.na(input$date[1]) & !is.na(input$date[2]), "Error: Please provide both a start and an end date."))
shiny::validate(need(input$date[1] < input$date[2], "Error: Start date should be earlier than end date."))
trend_data %>%
filter(
type == input$type,
date > as.POSIXct(input$date[1]) & date < as.POSIXct(input$date[2]
))
})
# Create scatterplot object the plotOutput function is expecting
output$lineplot <- renderPlot({
color = "#434343"
par(mar = c(4, 4, 1, 1))
plot(x = selected_trends()$date, y = selected_trends()$close, type = "l",
xlab = "Date", ylab = "Trend index", col = color, fg = color, col.lab = color, col.axis = color)
# Display only if smoother is checked
if(input$smoother){
smooth_curve <- lowess(x = as.numeric(selected_trends()$date), y = selected_trends()$close, f = input$f)
lines(smooth_curve, col = "#E6553A", lwd = 3)
}
})
# Pull in description of trend
output$desc <- renderText({
trend_text <- filter(trend_description, type == input$type) %>% pull(text)
paste(trend_text, "The index is set to 1.0 on January 1, 2004 and is calculated only for US search traffic.")
})
}
# Create Shiny object
shinyApp(ui = ui, server = server)
## PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.