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}
data <- data.frame( caloriesday = rnorm(100, mean = 2000, sd =
500),
comfortfood = rnorm(100, mean = 5, sd = 2)
)
fig <- plotly(data, x = ~caloriesday, y = ~comfort_food, type = 'scatter', mode = 'markers')
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") )
fig
Creating a boxplot
{r}
library(plotly) library(dplyr)
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 )
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))
fig <- plotly(summarizeddata, x = ~incomegroup, y = ~averagecalories, type = 'bar')
fig <- fig %>% layout( title = "Average Calories per Day by Income Group", xaxis = list(title = "Income Group"), yaxis = list(title = "Average Calories per Day") )
fig
```