# Sample data frame
<- data.frame(Item = c("Item1", "Item2", "Item3"),
data Variable1 = c(10, 15, 20),
Variable2 = c(5, 8, 12))
# Load the ggplot2 library
library(ggplot2)
# Reshape the data from wide to long format using tidyr
library(tidyr)
<- pivot_longer(data, cols = starts_with("Variable"),
data_long names_to = "Variable",
values_to = "Value")
# Create the grouped bar chart
ggplot(data_long, aes(x = Item, y = Value, fill = Variable)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.9)) +
labs(title = "Comparison of Variables for Three Items",
x = "Items",
y = "Value") +
theme_minimal()
More R Graphics Charts
Chart #1 Variable Width Column Chart Attempt by Team Member Yichao Jin from “Chart Suggestions - A Thought Starter”
Chart #2 Table or Table with Embedded Charts from “Chart Suggestions - A Thought Starter”
(Coming soon…)
Chart #3 Bar Chart Attempt by Team Member Yichao Jin from “Chart Suggestions - A Thought Starter”
# Sample data frame
<- data.frame(Item = c("Item1", "Item2", "Item3", "Item4","Item5","item6","item7","item8"),
data Value1 = c(30, 45, 25, 60, 35,40,31,37),
Value2 = c(20, 40, 50, 30, 45,21,65,35))
# Load the ggplot2 library
library(ggplot2)
# Create the first horizontal bar chart
<- ggplot(data, aes(x = Value1, y = Item, fill = Item)) +
chart1 geom_bar(stat = "identity", orientation = "y") +
labs(title = "", x = "Value", y = "Item") +
theme_minimal() +
theme(axis.text.y = element_text(hjust = 1)) # Right-align item labels
# Create the second horizontal bar chart
<- ggplot(data, aes(x = Value2, y = Item, fill = Item)) +
chart2 geom_bar(stat = "identity", orientation = "y") +
labs(title = "", x = "Value", y = "Item") +
theme_minimal() +
theme(axis.text.y = element_text(hjust = 1)) # Right-align item labels
# Display the charts side by side
library(gridExtra)
grid.arrange(chart1, chart2, ncol = 2)
Chart #4 Column Chart from “Chart Suggestions - A Thought Starter”
(Coming soon…)