I have been working on a visualizations for a project I am working on. I keep running into trouble with the labels, specifically subtitles and captions. Every time I let these labels or the theme for these elements, they do not appear in the final plot.
Here is my code:
earnyears <- as.data.table(ACS_LI[ACS_LI$Fulltime == 1 & ACS_LI$INCEARN >0 & ACS_LI$Immigrant == 1, list(
"Total Median Earnings" = weighted.median(ACS_LI$INCEARN[ACS_LI$Fulltime ==1 & ACS_LI$INCEARN >0 & Immigrant == 1],
ACS_LI$PERWT[ACS_LI$Fulltime == 1 & ACS_LI$INCEARN >0]),
"Less than 5 Years" = weighted.median(ACS_LI$INCEARN[ACS_LI$Fulltime ==1 & ACS_LI$INCEARN >0 & Immigrant == 1 & ACS_LI$yrs3_1 == 1],
ACS_LI$PERWT[ACS_LI$Fulltime == 1 & ACS_LI$INCEARN >0 & ACS_LI$yrs3_1 ==1]),
"5-10 Years" = weighted.median(ACS_LI$INCEARN[ACS_LI$Fulltime ==1 & ACS_LI$INCEARN >0 & Immigrant == 1 & ACS_LI$yrs3_1 == 2],
ACS_LI$PERWT[ACS_LI$Fulltime == 1 & ACS_LI$INCEARN >0 & ACS_LI$yrs3_1 ==2]),
"More than 10 Years" = weighted.median(ACS_LI$INCEARN[ACS_LI$Fulltime ==1 & ACS_LI$INCEARN >0 & Immigrant == 1 & ACS_LI$yrs3_1 == 3],
ACS_LI$PERWT[ACS_LI$Fulltime == 1 & ACS_LI$INCEARN >0 & ACS_LI$yrs3_1 ==3])
)])
#Make a bar graph
median_earnings <- mean(earnyears_longer$`Total Median Earnings`)
earnyears_longer <- earnyears%>%
pivot_longer(-c(`Total Median Earnings`), names_to = "Years_in_US", values_to = "Median_Income")
earnyears_plot <- ggplot(aes(x = Years_in_US, y = Median_Income), data = earnyears_longer)+
geom_bar(width = 0.75,position = "dodge", stat = "identity", fill = "#1f78b4")+
geom_hline(yintercept = median_earnings,
color = "#fdbf6f",
linetype = "dashed") +
ggplot2::annotate("text",
x = 1.125, y = 66500,
label = "Median Earnings: $64,993.50",
color = "#fdbf6f")+
theme(legend.position = "none",
panel.background = element_rect(fill = "white"),
panel.grid.major.y = element_line(color = "#d9d9d9"),
panel.grid.minor.y = element_line(color = "#d9d9d9",
linetype = "dashed"),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
plot.title = element_text(family = "Arial", face = "bold", size = (15), hjust = 0, vjust = 1),
plot.subtitle = element_text(family = "Arial", size = (12)),
axis.title.x = element_text(family = "Arial", size = (12)),
axis.text.x = element_text(family = "Arial", size = (10)),
axis.title.y = element_text(family = "Arial", size = (12)),
plot.caption.position = c("bottom", "left"))+
labs(title = "Median Earnings for Immigrants",
subtitle = "By Time in the United States",
caption = "Source: ACS 2019, 5-Year Sample. Numbers in 2019 Dollars.",
x = "Years in the US",
y = "Median Earnings")
ggplotly(earnyears_plot)
And here is the resulting plot.
I appreciate any assistance!