Simple Shiny app which shows four Distribution Plots for Length and Width of Petals and Sepals after one of the IRIS Species is selected. The dataset used for this project is IRIS which is available in R.
About the IRIS Dataset
IRIS data set gives measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of three species of iris.
The three species of Iris are setosa, versicolor and virginica.
The Sepals are the lower, or outermost, part of the flower (green and leaf-like). The Petals are colored segments of the corolla of a flower.
IRIS Data
dim(iris)
## [1] 150 5
Column Names
data.frame(names(iris))
## names.iris.
## 1 Sepal.Length
## 2 Sepal.Width
## 3 Petal.Length
## 4 Petal.Width
## 5 Species
## This file takes Name of Species as input and send it to server.R for calculation and plotting
library(shiny)
library(ggplot2)
shinyUI(pageWithSidebar(
headerPanel('Distribution of Length and Width of Petal and Sepal based on Species'),
sidebarPanel(
selectInput('Species', 'Select Species', as.character(unique(iris$Species)))
),
mainPanel(
#Setting the multiplot size as 800x600 for better visibility
plotOutput('plot1',width = 800, height = 600)
)
))
## This files takes the name of Species as input from ui.R and plots 4 different graphs
## and stacks them into a 2x2 multiplot
## Plot 1: Distribution of Sepal Length
## Plot 2: Distribution of Sepal Width
## Plot 3: Distribution of Petal Length
## Plot 4: Disbribution of Petal Width
## Mean Line for each disbribution is marked in "RED"
library(ggplot2)
library(gridExtra)
shinyServer(
function(input, output) {
data <- reactive({iris[iris$Species == input$Species,]})
output$plot1 <- renderPlot({
#Plot 1: Distribution of Sepal Length
g1 <- ggplot(data(), aes(Sepal.Length))
g1 <- g1 + geom_histogram(binwidth = .5, fill="green", color = "black",alpha = .2)
g1 <- g1 + geom_vline( aes(xintercept = mean(data()$Sepal.Length)), colour="red", size=2, alpha=.6)
g1 <- g1 + labs(x = "Sepal Length")
g1 <- g1 + labs(y = "Frequency")
g1 <- g1 + labs(title = paste("Distribution of Sepal Length, mu =", round(mean(data()$Sepal.Length),2)))
#Plot 2: Disbribution of Sepal Width
g2 <- ggplot(data(), aes(Sepal.Width))
g2 <- g2 + geom_histogram(binwidth = .5, fill="green", color = "black",alpha = .2)
g2 <- g2 + geom_vline( aes(xintercept = mean(data()$Sepal.Width)), colour="red", size=2, alpha=.6)
g2 <- g2 + labs(x = "Sepal Width")
g2 <- g2 + labs(y = "Frequency")
g2 <- g2 + labs(title = paste("Distribution of Sepal Width, mu =", round(mean(data()$Sepal.Width),2)))
#Plot 3: Disbribution of Petal Length
g3 <- ggplot(data(), aes(Petal.Length))
g3 <- g3 + geom_histogram(binwidth = .5, fill="yellow", color = "black",alpha = .2)
g3 <- g3 + geom_vline( aes(xintercept = mean(data()$Petal.Length)), colour="red", size=2, alpha=.6)
g3 <- g3 + labs(x = "Petal Length")
g3 <- g3 + labs(y = "Frequency")
g3 <- g3 + labs(title = paste("Distribution of Petal Length, mu =", round(mean(data()$Petal.Length),2)))
#Plot 4: Disbribution of Petal Width
g4 <- ggplot(data(), aes(Petal.Width))
g4 <- g4 + geom_histogram(binwidth = .5, fill="yellow", color = "black",alpha = .2)
g4 <- g4 + geom_vline( aes(xintercept = mean(data()$Petal.Width)), colour="red", size=2, alpha=.6)
g4 <- g4 + labs(x = "Petal Width")
g4 <- g4 + labs(y = "Frequency")
g4 <- g4 + labs(title = paste("Distribution of Petal Width, mu =", round(mean(data()$Petal.Width),2)))
#Plotting 4 graphs
grid.arrange(g1,g2,g3,g4,nrow=2, ncol=2)
})
}
)