My plot is not coloring the columns in my stacked barplot. I am able to color the points that show the overall mean values for the different years, but I also want to color the bars according to the colors. And I just want the diamond points’ and bar colors to show up in the legend so that I have two points for “Temperature” and two filled squares for “Precipitation” as my legend.
I have tried turning my mean values into a factor as well but that did not work. My code below is the closest I’ve gotten to getting some desired outcome without errors. Here the problem is the bars are still not coloring the way I want, and I don’t want the “Precip” continuous scale (hence why I tried factors) to show up in the legend.
active.means <- active %>% group_by(Year) %>% summarize(Temp = mean(Mean.Temp..C.), Precip = mean(Total.Precip..mm.))
nonactive.means <- nonactive %>% group_by(Year) %>% summarize(Temp = mean(Mean.Temp..C.), Precip = mean(Total.Precip..mm.))
ylim.prim <- c(0,10) # scale for precipitation
ylim.sec <- c(-15,30) # scale for mean temperature
b <- diff(ylim.prim)/diff(ylim.sec)
a <- ylim.prim[1] - b*ylim.sec[1]
ggplot() + theme_bw() +
geom_col(data=active.means, aes(x=Year, y=Precip, fill=Precip, color="Active"), size=0.1) +
geom_col(data=nonactive.means, aes(x=Year, y=Precip, fill=Precip, color="Non-Active"),size=0.1) +
geom_point(data=active.means, aes(y=a + Temp*b, x=Year, fill=Temp, color="Active"), size=5, shape=18) +
geom_point(data=nonactive.means, aes(y=a + Temp*b, x=Year, fill=Temp, color="Non-Active"), size=5, shape=18) +
# Borders for point data
geom_point(data=active.means, aes(y=a + Temp*b, x=Year), color="black", size=3, shape=5, stroke=2) +
geom_point(data=nonactive.means, aes(y=a + Temp*b, x=Year), color="black", size=3, shape=5, stroke=2) +
scale_y_continuous(name="Precipitation (mm)", breaks=c(0,1,2,3,4,5,6,7,8,9,10), limits=c(0,10),
sec.axis = sec_axis(~ (. - a)/b, name = "Temperature (°C)")) +
scale_x_continuous(name="Year") +
theme(axis.text.x = element_text(size=15, colour="black"),
axis.text.y = element_text(size=15, colour="black"),
axis.title = element_text(size=15),
axis.title.y = element_text(size=15),
legend.text = element_text(size=15),
legend.title = element_text(size=15),
axis.title.y.right = element_text(angle=90)) +
scale_color_manual(values=c("lightcoral","lightblue"))
> dput(active)
structure(list(Longitude..x. = c(-79.4, -79.4, -79.4, -79.4,
-79.4, -79.4, -79.4, -79.4, -79.4, -79.4, -79.4, -79.4, -79.4,
-79.4, -79.4, -79.4, -79.4, -79.4, -79.4, -79.4, -79.4, -79.4,
-79.4, -79.4), Latitude..y. = c(43.63, 43.63, 43.63, 43.63, 43.63,
43.63, 43.63, 43.63, 43.63, 43.63, 43.63, 43.63, 43.63, 43.63,
43.63, 43.63, 43.63, 43.63, 43.63, 43.63, 43.63, 43.63, 43.63,
43.63), Station.Name = c("TORONTO CITY CENTRE", "TORONTO CITY CENTRE",
"TORONTO CITY CENTRE", "TORONTO CITY CENTRE", "TORONTO CITY CENTRE",
"TORONTO CITY CENTRE", "TORONTO CITY CENTRE", "TORONTO CITY CENTRE",
"TORONTO CITY CENTRE", "TORONTO CITY CENTRE", "TORONTO CITY CENTRE",
"TORONTO CITY CENTRE", "TORONTO CITY CENTRE", "TORONTO CITY CENTRE",
"TORONTO CITY CENTRE", "TORONTO CITY CENTRE", "TORONTO CITY CENTRE",
"TORONTO CITY CENTRE", "TORONTO CITY CENTRE", "TORONTO CITY CENTRE",
"TORONTO CITY CENTRE", "TORONTO CITY CENTRE", "TORONTO CITY CENTRE",
"TORONTO CITY CENTRE"), Year = c(2020L, 2020L, 2020L, 2020L,
2020L, 2020L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2022L,
2022L, 2022L, 2022L, 2022L, 2022L, 2023L, 2023L, 2023L, 2023L,
2023L, 2023L), Month = c(4L, 5L, 6L, 7L, 8L, 9L, 4L, 5L, 6L,
7L, 8L, 9L, 4L, 5L, 6L, 7L, 8L, 9L, 4L, 5L, 6L, 7L, 8L, 9L),
Max.Temp..C. = c(9.62, 15.34, 23.77, 28.17, 25.83, 20.71,
10.92, 17.96, 24.1, 24.37, 26.97, 22.41, 9.82, 15.99, 23.44,
25.78, 26.14, 21.35, 11.75, 17.27, 22.84, 26.03, 23.77, 22.18
), Min.Temp..C. = c(2.4, 7.1, 14.18, 19.79, 17.64, 13.59,
3.54, 8.21, 14.6, 17.76, 20.14, 14.53, 2.97, 8.68, 13.32,
15.98, 18.57, 14.13, 4.02, 8.66, 15.86, 18.39, 16.28, 15.3
), Mean.Temp..C. = c(6, 11.2, 19, 24, 21.7, 17.2, 7.2, 13.1,
19.4, 21.1, 23.6, 18.5, 6.4, 12.3, 18.4, 20.9, 22.4, 17.7,
7.9, 13, 19.4, 22.2, 20, 18.7), Total.Precip..mm. = c(1.55,
1.35, 2.16, 2.78, 2.6, 1.23, 1.76, 0.84, 2.3, 3.74, 0.24,
4.18, 1.6, 1.06, 1.7, 1.7, 2.44, 1.41, 3.72, 1.36, 3.24,
3.21, 1.05, 0.49)), row.names = c(4L, 5L, 6L, 7L, 8L, 9L,
16L, 17L, 18L, 19L, 20L, 21L, 28L, 29L, 30L, 31L, 32L, 33L, 40L,
41L, 42L, 43L, 44L, 45L), class = "data.frame")
> dput(nonactive)
structure(list(Longitude..x. = c(-79.4, -79.4, -79.4, -79.4,
-79.4, -79.4, -79.4, -79.4, -79.4, -79.4, -79.4, -79.4, -79.4,
-79.4, -79.4, -79.4, -79.4, -79.4, -79.4, -79.4, -79.4, -79.4,
-79.4, -79.4), Latitude..y. = c(43.63, 43.63, 43.63, 43.63, 43.63,
43.63, 43.63, 43.63, 43.63, 43.63, 43.63, 43.63, 43.63, 43.63,
43.63, 43.63, 43.63, 43.63, 43.63, 43.63, 43.63, 43.63, 43.63,
43.63), Station.Name = c("TORONTO CITY CENTRE", "TORONTO CITY CENTRE",
"TORONTO CITY CENTRE", "TORONTO CITY CENTRE", "TORONTO CITY CENTRE",
"TORONTO CITY CENTRE", "TORONTO CITY CENTRE", "TORONTO CITY CENTRE",
"TORONTO CITY CENTRE", "TORONTO CITY CENTRE", "TORONTO CITY CENTRE",
"TORONTO CITY CENTRE", "TORONTO CITY CENTRE", "TORONTO CITY CENTRE",
"TORONTO CITY CENTRE", "TORONTO CITY CENTRE", "TORONTO CITY CENTRE",
"TORONTO CITY CENTRE", "TORONTO CITY CENTRE", "TORONTO CITY CENTRE",
"TORONTO CITY CENTRE", "TORONTO CITY CENTRE", "TORONTO CITY CENTRE",
"TORONTO CITY CENTRE"), Year = c(2020L, 2020L, 2020L, 2020L,
2020L, 2020L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2022L,
2022L, 2022L, 2022L, 2022L, 2022L, 2023L, 2023L, 2023L, 2023L,
2023L, 2023L), Month = c(1L, 2L, 3L, 10L, 11L, 12L, 1L, 2L, 3L,
10L, 11L, 12L, 1L, 2L, 3L, 10L, 11L, 12L, 1L, 2L, 3L, 10L, 11L,
12L), Max.Temp..C. = c(2.19, 1.02, 6.26, 13.25, 10.47, 3.3, 1.05,
-0.6, 7.48, 16.45, 8.54, 5.13, -1.91, 0.53, 4.79, 14.15, 8.52,
3.01, 2.2, 3.44, 4.4, 15.35, 7.77, 5.5), Min.Temp..C. = c(-3.37,
-4.81, 0.27, 6.27, 3.74, -1.89, -3.77, -6.51, -1.8, 11.48, 2.11,
-0.32, -10.92, -6.7, -1.67, 6.38, 2.41, -1.89, -2.09, -4.35,
-1.13, 9.19, 1.51, 1.25), Mean.Temp..C. = c(-0.6, -1.9, 3.3,
9.8, 7.1, 0.7, -1.4, -3.6, 2.8, 14, 5.3, 2.4, -6.4, -3.1, 1.6,
10.3, 5.5, 0.6, 0.1, -0.5, 1.6, 12.3, 4.6, 3.4), Total.Precip..mm. = c(4.07,
0.94, 1.96, 1.65, 2.32, 1.48, 0.81, 0.74, 1.2, 3.36, 1.08, 1.59,
0.66, 2.08, 1.58, 1.03, 1.55, 3.11, 1.93, 1.59, 2.28, 1.6, 1.5,
2.19)), row.names = c(1L, 2L, 3L, 10L, 11L, 12L, 13L, 14L, 15L,
22L, 23L, 24L, 25L, 26L, 27L, 34L, 35L, 36L, 37L, 38L, 39L, 46L,
47L, 48L), class = "data.frame")
Here is how the plot looks and again, I can’t figure out how to color the bars for precipitation values, nor get rid of the “Precip” continuous scale in the legend. And then I also just want to add a legend that shows the diamond points and bars colored for Active and Non-active seasons, with “Temperature” and “Precipitation” labels.
Below is the plot I am trying to imitate with my own data: