wk10
hotdogs <- read.csv("http://datasets.flowingdata.com/hot-dog-contest-winners.csv")
head(hotdogs)
## Year Winner Dogs.eaten Country New.record
## 1 1980 Paul Siederman & Joe Baldini 9.10 United States 0
## 2 1981
Thomas DeBerry 11.00 United
States 0
## 3 1982
Steven Abrams 11.00 United
States 0
## 4 1983 Luis Llamas 19.50 Mexico 0
## 5 1984
Birgit Felden 9.50 Germany 0
## 6 1985
Oscar Rodriguez 11.75 United
States 0
library("ggplot2")
#find color codes
my_colors <- c("#4285F4", "#DB4437", "#F4B400", "#0F9D58", "#AB47BC", "#00ACC1", "#FF7043", "#9E9D24", "#5E35B1", "#EF6C00")
# Assign colors to each bar based on their value
colors <- my_colors[cut(hotdogs$Dogs.eaten, breaks = length(my_colors),
labels = FALSE)]
# Create the barplot
barplot(hotdogs$Dogs.eaten, names.arg = hotdogs$Year, col=colors, border="white",
main = "Nathan's
Hot Dog Eating Contest Results, 1980-2010",
xlab="Year", ylab="Hot dogs and buns
(HDBs) eaten",
ylim = c(0, 80), xlim = c(0, length(hotdogs$Year) + 1), las = 2, cex.axis = 0.8, cex.lab = 0.9, font.lab = 2)
#2
my_colors <- c("#F44336", "#9C27B0", "#3F51B5", "#4CAF50", "#FFC107")
# Define a custom theme with a white background
and no gridlines
my_theme <- theme(panel.background
=
element_rect(fill = "white"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
# Create the plot. change geom_bar to geom_col
ggplot(hotdogs) +
geom_col(aes(x = Year, y = Dogs.eaten,
fill = factor(New.record)), width = 0.8) +
scale_fill_manual(values = my_colors) +
labs(title = "Nathan's
Hot Dog Eating Contest Results, 1980-2010",
x = "Year",
y = "Hot dogs
and buns (HDBs) eaten",
fill = "New
Record") +
my_theme +
coord_flip() +
theme(plot.title = element_text(face = "bold", size = 20),
axis.title = element_text(face = "bold", size = 16),
axis.text = element_text(size = 14),
legend.position = "bottom",
legend.title = element_text(face = "bold", size = 14),
legend.text = element_text(size = 12))
#This code creates a horizontal bar
chart using the ggplot2 package in R to visualize the results of Nathan's hot
dog eating contest between 1980 and 2010. The my_colors variable is used to set
custom colors for each bar. A custom theme is defined with a white background
and no gridlines. The plot includes a title, x and y labels, and a legend
showing whether a new record was set. The coord_flip() function is used to flip
the x and y axes.
#4
library(gridExtra)
## Warning: package 'gridExtra' was built under R
version 4.2.3
# First plot
plot1 <- qplot(unemploy/pop,
uempmed, data = economics, geom = c("point", "path"), color = "darkred") +
xlab("Unemployment rate") +
ylab("Median duration of unemployment") +
ggtitle("Unemployment rate and median duration of
unemployment") +
theme_classic() +
theme(legend.position = "none")
## Warning: `qplot()` was deprecated in ggplot2
3.4.0.
# Second plot
plot2 <- qplot(unemploy/pop,
uempmed, data = economics, geom = c("point", "path"), color = I("blue")) +
xlab("Unemployment rate") +
ylab("Median duration of unemployment") +
ggtitle("Unemployment rate and median duration of unemployment by
year") +
theme_classic() +
theme(legend.position = "none")
# Arrange the plots in a grid
grid.arrange(plot1, plot2,
ncol = 2)
#This code creates two plots
side-by-side using the R package gridExtra. Both plots show the relationship
between unemployment rate and median duration of unemployment using the
economics dataset. The plots have different color schemes and titles. The
theme() function is used to remove the legend.
Comments
Post a Comment