MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 57 MINUTES 30 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE
Example Working Code:
#### Load and prep the data set####
data(diamonds)
### Create bins for carat ###
carat_bins <- seq(0, ceiling(max(diamonds$carat) * 2) / 2, by = 0.5)
### Add the bins to the table ###
diamonds$carat_bin <- cut(diamonds$carat, breaks = carat_bins, include.lowest = TRUE)
#### Plot ####
ggplot(diamonds, aes(x = carat_bin, y = price, fill = color)) +
geom_violin(position = "identity", alpha = 0.15) +
labs(x = "Carat", y = "Price", fill = "Color") +
ggtitle("Distribution of Diamond Prices by Carat and Color") +
theme_minimal()
Example Refactored Code (function replaces Plot
section above):
#### Plot Function ####
my_plot <- function(x_var, fill_var) {
### Ensure fill_var is factored ###
diamonds[[fill_var]] <- factor(diamonds[[fill_var]])
ggplot(diamonds, aes(x = x_var, y = price, fill = fill_var)) +
geom_violin(position = "identity", alpha = 0.15)
}
#### Test ####
my_plot("carat_bin", "color")
Output from working code:
MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 57 MINUTES 28 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE
Output from refactored code:
MYMEMORY WARNING: YOU USED ALL AVAILABLE FREE TRANSLATIONS FOR TODAY. NEXT AVAILABLE IN 23 HOURS 57 MINUTES 27 SECONDS VISIT HTTPS://MYMEMORY.TRANSLATED.NET/DOC/USAGELIMITS.PHP TO TRANSLATE MORE