title: "Plotly-interactive plots" author: "Reethika Banerjee" date: "2024-07-10" output: html_document ---

{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE)

Objective: Creating an interactive plot to see the bar chart and scatter plot of the dataset "Food Orders" {r} install.packages("plotly") {r} library(plotly)

```{r} data <- read.csv("C:/Users/baner/OneDrive/Documents/R codes/food_coded.csv")

Creating a Scatterplot{r}

Create some example data

data <- data.frame( caloriesday = rnorm(100, mean = 2000, sd = 500),
comfort
food = rnorm(100, mean = 5, sd = 2)
)

Create the scatterplot

fig <- plotly(data, x = ~caloriesday, y = ~comfort_food, type = 'scatter', mode = 'markers')

Add title and axis labels

fig <- fig %>% layout( title = "Scatterplot of Calories per Day vs Comfort Food Rating", xaxis = list(title = "Calories per Day"), yaxis = list(title = "Comfort Food Rating") )

Display the plot

fig

Creating a boxplot{r}

Load necessary packages

library(plotly) library(dplyr)

Create some example data

set.seed(123) # For reproducibility data <- data.frame( income = sample(20000:100000, 100, replace = TRUE), # 100 random income values calories_day = rnorm(100, mean = 2000, sd = 500) # 100 random points for calories per day )

Summarize data to calculate the average calories per day for each income group

Create income bins to make the data more interpretable

data <- data %>% mutate(income_group = cut(income, breaks = seq(20000, 100000, by = 10000), include.lowest = TRUE))

summarizeddata <- data %>% groupby(incomegroup) %>% summarize(averagecalories = mean(calories_day, na.rm = TRUE))

Create the bar chart

fig <- plotly(summarizeddata, x = ~incomegroup, y = ~averagecalories, type = 'bar')

Add title and axis labels

fig <- fig %>% layout( title = "Average Calories per Day by Income Group", xaxis = list(title = "Income Group"), yaxis = list(title = "Average Calories per Day") )

Display the plot

fig

```