# Loading necessary libraries
library(dplyr)
##
## 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(ggplot2)
# Defining the data
product_category <- c("Electronics", "Clothing", "Home & Kitchen", "Books", "Electronics", "Clothing", "Home & Kitchen", "Books")
date <- as.Date(c("2023-01-01", "2023-01-02", "2023-01-03", "2023-01-04", "2023-01-05", "2023-01-06", "2023-01-07", "2023-01-08"))
sales <- c(5000, 3000, 7000, 2000, 6000, 4000, 8000, 2500)
price <- c(100, 50, 80, 30, 120, 60, 90, 25)
# Creating the data frame
Sales <- data.frame(
Product_Category = product_category,
Date = date,
Sales = sales,
Price = price
)
# Displaying the data frame
View(Sales)
# Grouping sales by Product_Category and calculate total sales
sales_by_category <- Sales %>%
group_by(Product_Category) %>%
summarise(Total_Sales = sum(Sales))
# Creating a bar chart using ggplot2
library(ggplot2)
ggplot(sales_by_category, aes(x = Product_Category, y = Total_Sales)) +
geom_bar(stat = "identity", fill = "skyblue") +
labs(title = "Total Sales by Product Category",
x = "Product Category",
y = "Total Sales") +
theme_minimal()
# Creating a line chart using ggplot2
ggplot(Sales, aes(x = Date, y = Sales)) +
geom_line(color = "blue", size = 1) +
geom_point(color = "red", size = 3) +
labs(title = "Sales Trends Over Time",
x = "Date",
y = "Sales") +
theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE
parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.