I have a dataset that looks like this:
columns are: 1-year 2-n_val 3-n_upper 4-n_lower 5-r_val 6-r_upper 7-r_lower
I want to create a graph with year as its x axis and two y axes, first being the n (number) and second being the r (rate). inside the graph, i want to have line chart of n_val separately colored red (n_upper and n_lower create the shaded area around the line) and the r_val colored blue (r_upper and r_lower create the shaded area around the line)
I would appreciate if you help me figure out how to write such code.
I tried this code:
death_trend_tidy <- pivot_longer(merged_death_trend, cols = c("n_ val", "n_ upper", "n_ lower", "r_ val", "r_ upper", "r_ lower"),
names_to = "variable",
values_to = "value")
death_trend_tidy$year <- as.numeric(death_trend_tidy$year)
ggplot(death_trend_tidy, aes(x = year)) +
geom_point(aes(y = value, color = variable)) +
scale_color_manual(values = c("n_ val" = "red", "r_ val" = "blue")) +
labs(x = "Year", y = "Value", color = "Variable") +
scale_y_continuous(name = "number",
sec.axis = sec_axis(~ . * 1/1000, name = "rate")) +
ggtitle("Death Trend")
and it does not do the expected thing that i explained earlier.
Bio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.