Reputation: 21
I hope you guys can help me. I have a problem creating a graph with ggplot. I want a second y axis that shows the cummulativ rain sum (= kummulative Regenmenge 10 Tage) as points. So far so good. However, the blue points that represent the cummulative rain sum are somehow mixed with the fill for "transtype" in the legend. They should be a seperated element in the legend. I hope you understand what I mean. Thanks a lot in advance. Hope you guys can help me with this issue. :) Here is my data
BF_1189 <- structure(list(Datum = structure(c(18703, 18703, 18703, 18703,
18703, 18703, 18703, 18703, 18703, 18703, 18724, 18724, 18724,
18724, 18724, 18724, 18724, 18724, 18724, 18724, 18730, 18730,
18730, 18730, 18730, 18730, 18730, 18730, 18730, 18730, 18744,
18744, 18744, 18744, 18744, 18744, 18744, 18744, 18744, 18744,
18758, 18758, 18758, 18758, 18758, 18758, 18758, 18758, 18758,
18758, 18774, 18774, 18774, 18774, 18774, 18774, 18774, 18774,
18774, 18774), class = "Date"), Soll = c("1189", "1189", "1192",
"1192", "1202", "1202", "2484", "2484", "552", "552", "1189",
"1189", "1192", "1192", "1202", "1202", "2484", "2484", "552",
"552", "1189", "1189", "1192", "1192", "1202", "1202", "2484",
"2484", "552", "552", "1189", "1189", "1192", "1192", "1202",
"1202", "2484", "2484", "552", "552", "1189", "1189", "1192",
"1192", "1202", "1202", "2484", "2484", "552", "552", "1189",
"1189", "1192", "1192", "1202", "1202", "2484", "2484", "552",
"552"), Transtyp = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L), .Label = c("2", "5"), class = "factor"), mittelwert = c(34.015625,
33.8375, 36.15, 36.6518518518519, 36.975, 36.1193548387097, 33.828125,
33.3625, 36.1625, 36.5032258064516, 27.109375, 26.775, 28.7625,
30.128125, 29.590625, 28.50625, 25.7258064516129, 25.340625,
26.6322580645161, 26.446875, 30.5516129032258, 30.4375, 31.903125,
32.15625, 32.40625, 30.715625, 28.4875, 29.084375, 28.784375,
29.9387096774194, 12.15625, 12.26875, 14.6290322580645, 14.6516129032258,
15.90625, 15.428125, 12.884375, 12.89375, 15.1625, 14.4, 11.9171875,
14.078125, 13.675, 13.7896551724138, 18.5, 17.071875, 10.578125,
10.21875, 13.021875, 13.225, 17.065625, 19.83125, 16.2586206896552,
17.441935483871, 19.75625, 19.15, 15.4451612903226, 14.2741935483871,
17.641935483871, 17.358064516129), kumsum_1189 = c(36.94, 36.94,
36.94, 36.94, 36.94, 36.94, 36.94, 36.94, 36.94, 36.94, 21.11,
21.11, 21.11, 21.11, 21.11, 21.11, 21.11, 21.11, 21.11, 21.11,
40.56, 40.56, 40.56, 40.56, 40.56, 40.56, 40.56, 40.56, 40.56,
40.56, 7.76, 7.76, 7.76, 7.76, 7.76, 7.76, 7.76, 7.76, 7.76,
7.76, 25.04, 25.04, 25.04, 25.04, 25.04, 25.04, 25.04, 25.04,
25.04, 25.04, 31.86, 31.86, 31.86, 31.86, 31.86, 31.86, 31.86,
31.86, 31.86, 31.86)), row.names = c(NA, -60L), groups = structure(list(
Datum = structure(c(18703, 18703, 18703, 18703, 18703, 18724,
18724, 18724, 18724, 18724, 18730, 18730, 18730, 18730, 18730,
18744, 18744, 18744, 18744, 18744, 18758, 18758, 18758, 18758,
18758, 18774, 18774, 18774, 18774, 18774), class = "Date"),
Soll = c("1189", "1192", "1202", "2484", "552", "1189", "1192",
"1202", "2484", "552", "1189", "1192", "1202", "2484", "552",
"1189", "1192", "1202", "2484", "552", "1189", "1192", "1202",
"2484", "552", "1189", "1192", "1202", "2484", "552"), .rows = structure(list(
1:2, 3:4, 5:6, 7:8, 9:10, 11:12, 13:14, 15:16, 17:18,
19:20, 21:22, 23:24, 25:26, 27:28, 29:30, 31:32, 33:34,
35:36, 37:38, 39:40, 41:42, 43:44, 45:46, 47:48, 49:50,
51:52, 53:54, 55:56, 57:58, 59:60), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -30L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
and here is my code
solle_1189 = c("1189", "1192", "1202", "2484", "552")
BF_1189 %>% pull(Datum) %>% unique() -> sdates
for(i in solle_1189 ){
BF_1189 %>% filter (Soll == i) %>%
group_by(Datum, Soll, Transtyp) %>%
ggplot(aes(Datum, mittelwert, fill = Transtyp))+
geom_bar(stat = "Identity", position = "dodge") +
geom_point(aes(Datum, kumsum_1189), shape =18, color = "blue", size = 3) +
scale_y_continuous(name = "Bodenfeuchte in %", sec.axis = sec_axis(~., name = "Kummulative Regenmenge 10 Tage"))+
scale_x_date(breaks = sdates)+
theme_minimal()+
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
axis.text = element_text(size = 9),
axis.title = element_text(size = 10, face = "bold"))+
scale_fill_grey()+
labs (title = paste0("Soll ", i)) ->pb
print(pb)
#ggsave(paste0("Z:/FOR/FOR-Projects/IPP_Soelle/Daten/Data_Raw/Max_2021/Grafen/Bodenfeuchte_fdr_kumnied_", i, ".png"))
}
Upvotes: 0
Views: 137
Reputation: 196
You can put col
inside aes
of geom_point
argument:
BF_1189
solle_1189 = c("1189", "1192", "1202", "2484", "552")
BF_1189 %>% pull(Datum) %>% unique() -> sdates
for(i in solle_1189) {
BF_1189 %>% filter (Soll == i) %>%
group_by(Datum, Soll, Transtyp) %>%
ggplot(aes(Datum, mittelwert, fill = Transtyp)) +
geom_bar(stat = "Identity", position = "dodge") +
geom_point(
aes(Datum, kumsum_1189, color = 'blue'), ## Put color inside aes
shape = 18,
size = 3,
inherit.aes = FALSE
) +
scale_color_manual('Cumulative', values = 'blue')+
scale_y_continuous(name = "Bodenfeuchte in %",
sec.axis = sec_axis( ~ ., name = "Kummulative Regenmenge 10 Tage"),) +
scale_x_date(breaks = sdates) +
theme_minimal() +
theme(
axis.text.x = element_text(
angle = 90,
vjust = 0.5,
hjust = 1
),
axis.text = element_text(size = 9),
axis.title = element_text(size = 10, face = "bold")
) +
scale_fill_grey() +
labs (title = paste0("Soll ", i)) -> pb
print(pb)
#ggsave(paste0("Z:/FOR/FOR-Projects/IPP_Soelle/Daten/Data_Raw/Max_2021/Grafen/Bodenfeuchte_fdr_kumnied_", i, ".png"))
}
Upvotes: 1
Reputation: 26495
One potential solution to your problem is to create a new variable (e.g. "Regen") via mutate()
and map an aesthetic to it, e.g.
BF_1189 <- BF_1189 %>%
mutate(Regen = "Kummulative")
for (i in solle_1189) {
BF_1189 %>% filter (Soll == i) %>%
group_by(Datum, Soll, Transtyp) %>%
ggplot(aes(x = Datum, y = mittelwert))+
geom_bar(aes(fill = Transtyp), stat = "Identity",
position = "dodge") +
geom_point(aes(y = kumsum_1189, size = Regen),
shape = 18, color = "blue") +
scale_y_continuous(name = "Bodenfeuchte in %",
sec.axis = sec_axis(~., name = "Kummulative Regenmenge 10 Tage")) +
scale_x_date(breaks = sdates)+
theme_minimal()+
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
axis.text = element_text(size = 9),
axis.title = element_text(size = 10, face = "bold")) +
scale_fill_grey() +
labs (title = paste0("Soll ", i)) -> pb
print(pb)
#ggsave(paste0("Z:/FOR/FOR-Projects/IPP_Soelle/Daten/Data_Raw/Max_2021/Grafen/Bodenfeuchte_fdr_kumnied_", i, ".png"))
}
Does that solve your problem?
Upvotes: 1