# We will make a app which will make scatter plot of variable which ever we choose
# and we can also make the points colour according to the another variable which we can choose in Color
data("mtcars")
library(shiny)
library(ggplot2)
#Making all categorical variables as factor
mtcars[,c(2, 8, 9, 10, 11)] <- apply(mtcars[,c(2, 8, 9, 10, 11)], 2, as.factor)
ui <- shinyUI(pageWithSidebar(
headerPanel("Mtcars Plot"),
sidebarPanel(
selectInput('x', 'X Axis', names(mtcars)),
selectInput('y', 'Y Axis', names(mtcars), names(mtcars)[[2]]),
selectInput('color', 'Color', c('None', names(mtcars))),
checkboxInput('smooth', 'Smooth')
),
mainPanel(
plotOutput('plot') )
))
server <- function(input, output) {
output$plot <- renderPlot({
p <- ggplot(mtcars, aes_string(x=input$x, y=input$y)) + geom_point(size = 5)
if (input$color != 'None')
p <- p + aes_string(color=input$color)
if (input$smooth)
p <- p + geom_smooth()
print(p)
}, height = 550)}
shinyApp(ui, server)
**************************************************************************************************************
library(ggplot2)
library(gganimate)
library(dplyr)
library(gifski)
data("mtcars")
theme_set(theme_bw())
#changing variable type to categorical for those who are factor
mtcars[,c(2, 8, 9, 10, 11)] <- apply(mtcars[,c(2, 8, 9, 10, 11)], 2, as.factor)
# plot 1
# Here we are making plot of horsepower vs Miles per gallon which varies in size of bubble according to weight of vechile
# And color varies according to transmission mode (automatic for red and manual for blue)
# And it animates according to the number of cylinders in the cars
plot <- ggplot(mtcars, aes(x = hp, y = mpg, size = wt, colour = am)) + geom_point(alpha = 0.8) +
labs(x = "Horsepower", y = "Miles per Gallon")
#############PLOT 1#######################
plot + transition_states(cyl) + labs(title = "No of Cylinders : {closest_state}")
# Now we will uses multiple types of transitions, exits and entry functions
##########PLOT 2##########################
# Here y axis will be fixed and x axis will move according to the Miles per gallon
plot + transition_states(cyl) + labs(title = "No of Cylinders : {closest_state}")+
view_follow(fixed_y = TRUE)
##########PLOT 3##########################
# with different transistion length and state
plot + transition_states(cyl, transition_length = 2, state_length = 1) +
labs(title = "No of Cylinders : {closest_state}")
##########PLOT 4##########################
#with different entry and exit state
plot + transition_states(cyl, transition_length = 2, state_length = 1) +
labs(title = "No of Cylinders : {closest_state}") + enter_fade() +
exit_shrink()
##########PLOT 5##########################
plot + transition_states(cyl, transition_length = 2, state_length = 1) +
labs(title = "No of Cylinders : {closest_state}") + enter_fade() + enter_drift(x_mod = -1) +
exit_shrink() + exit_drift(x_mod = 5)