For this assignment, we’ll be working through with data from the Recording Industry Association of America (RIAA) in an attempt to recreate an image similar to this one:
The columns other than year measure the amount of sales for that music format in millions of dollars.
Change the data to be in the proper format to create the graph. You’ll also need to reorder the music formats to be ordered as:
Streaming/Download/CD/Cassette/Vinyl/Other
Save the result as music2, then show the first 12 rows
music2 <-
music |>
# Need all the columns for each format to be in one column:
pivot_longer(
cols = -year,
names_to = "format",
values_to = "sales"
) |>
# Changing the order of the music formats to match the instructions
mutate(
format = factor(format,
levels = c("Other", "Vinyl", "Cassette",
"CD", "Download", "Streaming"))
)
# Displaying the first 12 rows
head(music2,
n = 12)
## # A tibble: 12 × 3
## year format sales
## <int> <fct> <dbl>
## 1 1973 Vinyl 1925
## 2 1973 Cassette 91.6
## 3 1973 CD 0
## 4 1973 Download 0
## 5 1973 Streaming 0
## 6 1973 Other 0
## 7 1974 Vinyl 2099.
## 8 1974 Cassette 100.
## 9 1974 CD 0
## 10 1974 Download 0
## 11 1974 Streaming 0
## 12 1974 Other 0
Create the basic graph seen in the Brightspace. To get the area
between the lines, use geom_line()
and to have the lines
stacked on top of one another, use the position =
argument along with the correct value for it.
In addition to the two geometries, add coord_cartesian()
along with an additional argument to remove the extra space on the 4
sides of the graph.
Once you have the graph that matches what is in blackboard, save it
as gg_music2
. Make sure this graph appears in the knitted
document!
gg_music2 <-
ggplot(
data = music2,
mapping = aes(
x = year,
y = sales/1000,
fill = fct_rev(format)
)
) +
# Adding the color regions
geom_area(
position = "stack",
show.legend = F
) +
# Adding the line between regions
geom_line(
position = "stack",
color = "white",
size = 1
) +
# Changing the labels
labs(
x = NULL,
y = NULL,
title = "The rise and fall of music formats",
subtitle = "Total annual revenue in billions in the USD",
caption = "Source: RIAA"
) +
# Removing the buffer space on all sides
coord_cartesian(expand = F)
gg_music2
Update gg_music2
by using the appropriate scale
functions for the 3 different aesthetics to match what is seen for
question 3 in Brightspace. The colors don’t need to be identical, just
close!
Once you have the graph that matches what is in blackboard, save it
as gg_music3
. Make sure this graph appears in the knitted
document!
gg_music3 <-
gg_music2 +
# Changing the x-axis
scale_x_continuous(
breaks = seq(from = 1973, to = 2021, by = 2)
) +
# Having the colors match for each of the formats
scale_fill_manual(
values = c(
"Other"= "grey70",
"Vinyl" = "skyblue",
"Cassette" = "green2",
"CD" = "gold2",
"Download" = "orchid",
"Streaming" = "red2"
)
) +
scale_y_continuous(
breaks = 1:15,
labels = scales::dollar,
limits = c(0, 15)
)
gg_music3
Using the theme()
function, make the appropriate updates
to gg_music3
. Save the result as gg_music4
and
have it appear in your knitted document
gg_music4 <-
gg_music3 +
#theme_classic() +
theme(
# Making the text white
text = element_text(color = "white"),
# Centering the title and subtitle
plot.title = element_text(hjust = 0.5, size = 16, face = "bold"),
plot.subtitle = element_text(hjust = 0.5, size = 10),
# Making the caption italicized and moved to the right side
plot.caption = element_text(hjust = 0, face = "italic"),
# Removing the grid lines and changing the panel and plot to be black
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "black"),
panel.border = element_rect(color = "white", fill = NA, linewidth = 1),
plot.background = element_rect(fill = "black"),
# Rotating the x-axis 90 degrees
axis.text.x = element_text(angle = 90,
vjust = 0.5,
color = "white"),
axis.text = element_text(color = "white"),
# Making the tick marks white
axis.ticks = element_line(color = "white")
)
gg_music4
Use one of the methods seen in class to add the music medium to the corresponding areas in the graph. They don’t need to match the spots exactly, just need to be close. You don’t need to save this one since it is the last graph!
gg_music4 +
annotate(
geom = "text",
x = 1978.5,
y = 1.25,
label = "Vinyl",
color = "white",
size = 8
) +
annotate(
geom = "text",
x = 1990.5,
y = 2,
label = "Cassette",
color = "white",
size = 8
) +
annotate(
geom = "text",
x = 2000,
y = 8,
label = "CD",
color = "white",
size = 12
) +
annotate(
geom = "text",
x = 2012,
y = 4.5,
label = "Downloads",
color = "white",
size = 6,
angle = -40
) +
annotate(
geom = "text",
x = 2017,
y = 7.7,
label = "Streaming",
color = "white",
size = 8,
angle = 57
)