library(ggplot2)
ggplot(diamonds, aes(x = price, fill = cut)) +
geom_histogram(bins = 40, color = "white") +
facet_wrap(~ cut) +
labs(
title = "Distribution of Diamond Prices by Cut",
x = "Price (USD)",
y = "Count"
) +
theme_minimal() +
theme(
legend.position = "none",
strip.text = element_text(size = 10, face = "bold")
)
ggplot(diamonds, aes(x = carat, y = price, color = clarity)) +
geom_point(alpha = 0.5, size = 1) +
facet_grid(color ~ cut) +
labs(
title = "Diamond Price vs Carat by Cut and Color",
x = "Carat",
y = "Price (USD)",
color = "Clarity"
) +
theme_minimal() +
theme(
strip.text = element_text(size = 9, face = "bold")
)
library(ggplot2)
library(patchwork)
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
scatter <- ggplot(diamonds, aes(carat, price, color = cut)) +
geom_point(alpha = 0.4, size = 0.8) +
labs(title = "Carat vs Price") +
theme_minimal()
bar <- ggplot(diamonds, aes(cut, fill = cut)) +
geom_bar() +
labs(title = "Count by Cut", x = "Cut", y = "Count") +
theme_minimal() +
theme(legend.position = "none")
density <- ggplot(diamonds, aes(price, fill = cut)) +
geom_density(alpha = 0.5) +
labs(title = "Price Distribution") +
theme_minimal()
box <- ggplot(diamonds, aes(cut, price, fill = cut)) +
geom_boxplot() +
labs(title = "Price by Cut", x = "Cut", y = "Price") +
theme_minimal() +
theme(legend.position = "none")
#side by side and stacked
(scatter + bar) / (density + box)
#unequal sizes and fixed legend
final <-(scatter + bar) /
(density + box) +
plot_layout(guides = "collect", heights = c(2, 1)) +
theme(legend.position = "bottom")
final
#PNG with 300 DPI
ggsave(
filename = "combined_figure.png",
plot = final,
width = 10,
height = 8,
dpi = 300
)
#PDF
ggsave(
filename = "combined_figure.pdf",
plot = final,
width = 10,
height = 8
)
#Export dataframe as CSV
write.csv(diamonds, "diamonds_data.csv", row.names = FALSE)
#PNG as an image ```