# Load necessary library
library(ggplot2)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ lubridate 1.9.3 ✔ tibble 3.2.1
## ✔ purrr 1.0.2 ✔ tidyr 1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# Set seed for reproducibility
set.seed(123)
# Create a dummy dataset
dummy_data <- data.frame(
Category = rep(c("A", "B", "C"), each = 100),
Value = rnorm(300)
)
# Calculate percentage for each category
percentage_data <- dummy_data %>%
group_by(Category) %>%
summarise(Percentage = mean(Value))
# Create a bar plot with percentages inside the bars
ggplot(dummy_data, aes(x = Category, y = Value, fill = Category)) +
geom_bar(stat = "identity") +
geom_text(data = percentage_data, aes(label = scales::percent(Percentage), y = Percentage),
position = position_stack(vjust = 0.5)) +
labs(title = "Percentage of Each Category Inside Bars", y = "Value", x = "Category") +
theme_minimal()
