I would like the top legend to not have the points. That legend is for the stacked barplot (percentage of total funding) and should just show the category color.
I would like the bottom legend to show the line + colored shape of the line plots (number of projects).
data
library(tidyverse)
library(openxlsx)
library(ggplot2)
library(cowplot)
# Load the data
new_data = read.xlsx("processed_2008-2020.xlsx")
max_projects <- max(new_data$number_of_projects, na.rm = TRUE)
# Create the main plot (without legends)
main_plot <- new_data %>%
ggplot(aes(x = year)) +
geom_bar(
aes(y = pct_total_funding * 100, fill = category),
stat = "identity",
position = "stack",
alpha = 0.6
) +
geom_line(
aes(y = number_of_projects * (100 / max_projects), group = category),
color = "black",
linewidth = 0.7,
alpha = 0.7
) +
geom_point(
aes(y = number_of_projects * (100 / max_projects), shape = category, fill = category),
color = "black", # Black outline for shapes
size = 3,
stroke = 0.5,
alpha = 0.7
) +
scale_shape_manual(values = c(21, 22, 23, 24)) +
scale_fill_brewer(palette = "Dark2") +
scale_x_continuous(
name = "Funding Year",
breaks = unique(new_data$year),
labels = function(x) str_glue("{x}")
) +
scale_y_continuous(
name = "Percentage of Total Funding",
breaks = seq(0, 100, by = 20),
minor_breaks = seq(0, 100, by = 10),
labels = percent_format(scale = 1),
sec.axis = sec_axis(~ . * (max_projects / 100),
name = "Number of Projects",
labels = scales::comma_format()),
limits = c(0, NA),
expand = expansion(mult = c(0, 0.05))
) +
labs(
title = "US Autism Research Funding by Category",
subtitle = "Federal and Private",
fill = "Percentage of Total Funding",
shape = "Number of Projects"
) +
theme_light() +
theme(
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
legend.position = "bottom",
legend.box = "vertical", # Stacked one above the other
legend.title = element_text(size = 12),
legend.text = element_text(size = 11),
axis.text.x = element_text(angle = 45, hjust = 1),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.border = element_blank(),
axis.ticks.y = element_blank()
)
# Adjust the top legend to remove points (only showing fill colors for the bar plot)
main_plot <- main_plot +
guides(
fill = guide_legend(title = "Percentage of Total Funding"),
)
# Print the final plot with refined legends
print(main_plot)
Recognized by R Language Collective
2
An easy option to achieve your desired result would be to user the ggnewscale
package which allows for multiple scales for the same aesthetic, i.e. the fill
aes in your case.
library(tidyverse)
library(scales, warn = FALSE)
library(ggnewscale)
# Load the data
new_data <- read_csv(
"https://gist.githubusercontent.com/abalter/14c9135f295419d6590f79b2b9b10c5c/raw/2493dfc087a34f091052a6586c608fb1be86ce1c/iacc_processed_2008-2020.csv",
show_col_types = FALSE
)
max_projects <- max(new_data$number_of_projects, na.rm = TRUE)
# Create the main plot (without legends)
new_data %>%
ggplot(aes(x = year)) +
geom_bar(
aes(y = pct_total_funding * 100, fill = category),
stat = "identity",
position = "stack",
alpha = 0.6
) +
scale_fill_brewer(palette = "Dark2", name = "Percentage of Total Funding") +
ggnewscale::new_scale_fill() +
geom_line(
aes(y = number_of_projects * (100 / max_projects), group = category),
color = "black",
linewidth = 0.7,
alpha = 0.7
) +
geom_point(
aes(y = number_of_projects * (100 / max_projects), shape = category, fill = category),
color = "black", # Black outline for shapes
size = 3,
stroke = 0.5,
alpha = 0.7
) +
scale_shape_manual(values = c(21, 22, 23, 24)) +
scale_fill_brewer(palette = "Dark2", name = "Number of Projects") +
scale_x_continuous(
name = "Funding Year",
breaks = unique(new_data$year),
labels = function(x) str_glue("{x}")
) +
scale_y_continuous(
name = "Percentage of Total Funding",
breaks = seq(0, 100, by = 20),
minor_breaks = seq(0, 100, by = 10),
labels = percent_format(scale = 1),
sec.axis = sec_axis(~ . * (max_projects / 100),
name = "Number of Projects",
labels = scales::comma_format()
),
limits = c(0, NA),
expand = expansion(mult = c(0, 0.05))
) +
labs(
title = "US Autism Research Funding by Category",
subtitle = "Federal and Private",
shape = "Number of Projects"
) +
theme_light() +
theme(
plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
legend.position = "bottom",
legend.box = "vertical", # Stacked one above the other
legend.title = element_text(size = 12),
legend.text = element_text(size = 11),
axis.text.x = element_text(angle = 45, hjust = 1),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.border = element_blank(),
axis.ticks.y = element_blank()
)
Recognized by R Language Collective