Problems with scale_x_continuous - r

I want to show more dates in the x axis. Something like this: Mar 09, Mar 12, Mar 19 , etc
So this is my general data:
structure(list(Dia = structure(c(1583452800, 1583539200, 1583625600,
1583712000, 1583798400, 1583884800, 1583884800, 1583884800, 1583971200,
1584057600, 1584057600, 1584144000, 1584230400, 1584316800, 1584403200,
1584489600, 1584576000), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
Hora = structure(c(-2209010400, -2209010400, -2209075200,
-2209044600, -2209046400, -2209039200, -2209023600, -2209003200,
-2209039500, -2209044600, -2209017600, -2209041000, -2209027800,
-2209040160, -2209038720, -2209050000, -2209032000), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), Total_Pruebas = c(155, 219, 250,
318, 346, 652, 656, 714, 855, 983, 1232, 1545, 1822, 2315,
2680, 3075, 4075), Descartados = c(154, 213, 243, 309, 335,
640, 641, 697, 833, 955, 1194, 1502, 1751, 2229, 2563, 2930,
3841), Positivos = c(1, 6, 7, 9, 11, 12, 15, 17, 22, 28,
38, 43, 71, 86, 117, 145, 234), TasaPositivos = c(0.645161290322581,
2.73972602739726, 2.8, 2.83018867924528, 3.17919075144509,
1.84049079754601, 2.28658536585366, 2.38095238095238, 2.57309941520468,
2.84842319430315, 3.08441558441558, 2.7831715210356, 3.89681668496158,
3.71490280777538, 4.36567164179105, 4.71544715447155, 5.74233128834356
), Pruebas_dia = c(155, 64, 31, 99, 28, 306, 4, 58, 141,
128, 249, 313, 277, 493, 365, 395, 1000), Recuperados = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1, 1, 1,
1)), row.names = c(NA, 17L), class = "data.frame")
This is my code
dat1 <- dat %>%
mutate(pos_new = Positivos-lag(Positivos,default = 0)) %>%
group_by(Dia) %>%
summarise(pos_new = sum(pos_new), tot_pruebas = sum(Pruebas_dia)) %>%
mutate(cum_pos = cumsum(pos_new))
This is dat1 data base:
structure(list(Dia = structure(c(1583452800, 1583539200, 1583625600,
1583712000, 1583798400, 1583884800, 1583971200, 1584057600, 1584144000,
1584230400, 1584316800, 1584403200, 1584489600, 1584576000), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), pos_new = c(1, 5, 1, 2, 2, 6, 5, 16,
5, 28, 15, 31, 28, 89), tot_pruebas = c(155, 64, 31, 99, 28,
368, 141, 377, 313, 277, 493, 365, 395, 1000), cum_pos = c(1,
6, 7, 9, 11, 17, 22, 38, 43, 71, 86, 117, 145, 234)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -14L))
and this is my final code:
f1 <- dat1 %>%
ggplot(aes(x = Dia)) +
geom_bar(aes(y = pos_new, fill = "Nuevos"), stat = "identity", alpha=.5) +
geom_line(aes(y = cum_pos, col = "Acumulados"), size=1) +
geom_point(aes(y = cum_pos), col = "#8B1C62") +
geom_text(aes(y = pos_new, label = pos_new), vjust = -0.8, col = "#43CD80") +
geom_text(aes(y = cum_pos, label = cum_pos), vjust = -0.8, col = "#8B1C62") +
labs(y = "Número de casos reportados", color = " Casos", fill = " ",
title = paste0("Número de casos confirmados \nhasta: ", Sys.Date())) +
scale_fill_manual(values = c("Nuevos" = "#43CD80")) +
scale_color_manual(values = c("Acumulados" = "#8B1C62")) +
scale_y_continuous(sec.axis = sec_axis(~ .)) +
theme_minimal() +
theme(legend.position="bottom")+
scale_x_continuous(breaks = seq(from =3-06-20 , to = 3-06-20, by = 1),
limits = c(3-06-20,3-19-20))
But I get this message:
Error in as.POSIXct.numeric(value) : 'origin' must be supplied
I want to show more dates ON THE X-AXIS ( from Mar 09 to Mar 19)

Instead of using scale_x_continuous you can use scale_x_datetime or scale_x_date. As your day Dia is already in POSIXct format, I used scale_x_datetime.
For your breaks, make sure to also put in POSIXct format. You can add labels to show Month Day using date_format from scales package.
library(ggplot2)
library(scales)
dat1 %>%
ggplot(aes(x = Dia)) +
geom_bar(aes(y = pos_new, fill = "Nuevos"), stat = "identity", alpha=.5) +
geom_line(aes(y = cum_pos, col = "Acumulados"), size=1) +
geom_point(aes(y = cum_pos), col = "#8B1C62") +
geom_text(aes(y = pos_new, label = pos_new), vjust = -0.8, col = "#43CD80") +
geom_text(aes(y = cum_pos, label = cum_pos), vjust = -0.8, col = "#8B1C62") +
labs(y = "Número de casos reportados", color = " Casos", fill = " ",
title = paste0("Número de casos confirmados \nhasta: ", Sys.Date())) +
scale_fill_manual(values = c("Nuevos" = "#43CD80")) +
scale_color_manual(values = c("Acumulados" = "#8B1C62")) +
scale_y_continuous(sec.axis = sec_axis(~ .)) +
theme_minimal() +
theme(legend.position="bottom") +
scale_x_datetime(breaks = seq(from = as.POSIXct("2020-03-06"), to = as.POSIXct("2020-03-20-20"), by = "1 days"), labels = date_format("%b %d"))
Note: As suggested by #Dave2e you can simplify scale_x_datetime:
scale_x_datetime(date_breaks = "1 day", date_labels = "%b %d")
Output

Related

Adding p-values to ggplot; ggsignif says it can only handle data with groups that are plotted on the x-axis

I have data as follows, to which I am trying to add p-values:
library(ggplot2)
library(ggsignif)
library(dplyr)
data <- structure(list(treatment = c(0, 1, 0, 1, 0, 1, 0, 1, 0, 1), New_Compare_Truth = c(57,
61, 12, 14, 141, 87, 104, 90, 12, 14), total_Hy = c(135,
168, 9, 15, 103, 83, 238, 251, 9, 15), total = c(285, 305, 60,
70, 705, 435, 520, 450, 60, 70), ratio = c(47.3684210526316,
55.0819672131148, 15, 21.4285714285714, 14.6099290780142, 19.0804597701149,
45.7692307692308, 55.7777777777778, 15, 21.4285714285714), Type = structure(c(2L,
2L, 1L, 1L, 3L, 3L, 5L, 5L, 4L, 4L), .Label = c("A1. Others \nMore \nH",
"A2. Similar \nNorm", "A3. Others \nLess \nH", "B1. Others \nMore \nH",
"B2. Similar \nNorm or \nHigher"), class = "factor"), `Sample Selection` = c("Answers pr",
"Answers pu", "Answers pr", "Answers pu", "Answers pr",
"Answers pu", "Answers pr", "Answers pu", "Answers pr",
"Answers pu"), p_value = c(0.0610371842601616, 0.0610371842601616,
0.346302201593934, 0.346302201593934, 0.0472159407450147, 0.0472159407450147,
0.0018764377521242, 0.0018764377521242, 0.346302201593934, 0.346302201593934
), x = c(2, 2, 1, 1, 3, 3, 5.5, 5.5, 4.5, 4.5)), row.names = c(NA,
-10L), class = c("data.table", "data.frame"))
breaks_labels <- structure(list(Type = structure(c(2L, 1L, 3L, 5L, 4L), .Label = c("A1. Others \nMore \nH",
"A2. Similar \nNorm", "A3. Others \nLess \nH", "B1. Others \nMore \nH",
"B2. Similar \nNorm or \nHigher"), class = "factor"), x = c(2,
1, 3, 5.5, 4.5)), row.names = c(NA, -5L), class = c("data.table",
"data.frame"))
data %>%
ggplot(aes(x = x, y = ratio)) +
geom_col(aes(fill = `Sample Selection`), position = position_dodge(preserve = "single"), na.rm = TRUE) +
geom_text(position = position_dodge(width = .9), # move to center of bars
aes(label=sprintf("%.02f %%", round(ratio, digits = 1)), group = `Sample Selection`),
vjust = -1.5, # nudge above top of bar
size = 4,
na.rm = TRUE) +
# geom_text(position = position_dodge(width = .9), # move to center of bars
# aes(label= paste0("(", ifelse(variable == "Crime = 0", `Observation for Crime = 0`, `Observation for Crime = 1`), ")"), group = `Sample Selection`),
# vjust = -0.6, # nudge above top of bar
# size = 4,
# na.rm = TRUE) +
scale_fill_grey(start = 0.8, end = 0.5) +
scale_y_continuous(expand = expansion(mult = c(0, .1))) +
scale_x_continuous(breaks = breaks_labels$x, labels = breaks_labels$Type) +
theme_bw(base_size = 15) +
xlab("Norm group for corporate Hy") +
ylab("Percentage Compliant Decisions") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
geom_signif(annotation=c("p=0.35", "p=0.06", "p=0.05", "p=0.34", "p=0.00"), y_position = c(30, 40, 55 ,75, 90), xmin=c(0.75,1.75,2.75,3.75,4.75),
xmax=c(1.25,2.25,3.25,4.25,5.25))
For some reason, the last line causes the following error:
Error in f(...) :
Can only handle data with groups that are plotted on the x-axis
Since I am just putting in text and not referring to any variable, I don't really understand why this happens. Can anyone help me out? Without the last line it looks like this:
EDIT: Please note that I would like to keep the space between the third and the fourth column (which is apparently also what caused the problem, see Jared's answer).
Edit
Thanks for clarifying your expected outcome. Here is one way to include geom_signif() annotations without altering the original plot:
library(tidyverse)
library(ggsignif)
data <- structure(list(treatment = c(0, 1, 0, 1, 0, 1, 0, 1, 0, 1), New_Compare_Truth = c(57,
61, 12, 14, 141, 87, 104, 90, 12, 14), total_Hy = c(135,
168, 9, 15, 103, 83, 238, 251, 9, 15), total = c(285, 305, 60,
70, 705, 435, 520, 450, 60, 70), ratio = c(47.3684210526316,
55.0819672131148, 15, 21.4285714285714, 14.6099290780142, 19.0804597701149,
45.7692307692308, 55.7777777777778, 15, 21.4285714285714), Type = structure(c(2L,
2L, 1L, 1L, 3L, 3L, 5L, 5L, 4L, 4L), .Label = c("A1. Others \nMore \nH",
"A2. Similar \nNorm", "A3. Others \nLess \nH", "B1. Others \nMore \nH",
"B2. Similar \nNorm or \nHigher"), class = "factor"), `Sample Selection` = c("Answers pr",
"Answers pu", "Answers pr", "Answers pu", "Answers pr",
"Answers pu", "Answers pr", "Answers pu", "Answers pr",
"Answers pu"), p_value = c(0.0610371842601616, 0.0610371842601616,
0.346302201593934, 0.346302201593934, 0.0472159407450147, 0.0472159407450147,
0.0018764377521242, 0.0018764377521242, 0.346302201593934, 0.346302201593934
), x = c(2, 2, 1, 1, 3, 3, 5.5, 5.5, 4.5, 4.5)), row.names = c(NA,
-10L), class = c("data.table", "data.frame"))
breaks_labels <- structure(list(Type = structure(c(2L, 1L, 3L, 5L, 4L), .Label = c("A1. Others \nMore \nH",
"A2. Similar \nNorm", "A3. Others \nLess \nH", "B1. Others \nMore \nH",
"B2. Similar \nNorm or \nHigher"), class = "factor"), x = c(2,
1, 3, 5.5, 4.5)), row.names = c(NA, -5L), class = c("data.table",
"data.frame"))
annotation_df <- data.frame(signif = c("p=0.35", "p=0.06", "p=0.05", "p=0.34", "p=0.00"),
y_position = c(30, 40, 55 ,75, 90),
xmin = c(0.75,1.75,2.75,4.25,5.25),
xmax = c(1.25,2.25,3.25,4.75,5.75),
group = c(1,2,3,4,5))
data %>%
ggplot(aes(x = x, y = ratio, group = `Sample Selection`)) +
geom_col(aes(fill = `Sample Selection`),
position = position_dodge(preserve = "single"), na.rm = TRUE) +
geom_text(position = position_dodge(width = .9), # move to center of bars
aes(label=sprintf("%.02f %%", round(ratio, digits = 1))),
vjust = -1.5, # nudge above top of bar
size = 4,
na.rm = TRUE) +
scale_fill_grey(start = 0.8, end = 0.5) +
scale_y_continuous(expand = expansion(mult = c(0, .1))) +
scale_x_continuous(breaks = breaks_labels$x, labels = breaks_labels$Type) +
theme_bw(base_size = 15) +
xlab("Norm group for corporate Hy") +
ylab("Percentage Compliant Decisions") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
geom_signif(aes(xmin = xmin,
xmax = xmax,
y_position = y_position,
annotations = signif,
group = group),
data = annotation_df, manual = TRUE)
#> Warning: Ignoring unknown aesthetics: xmin, xmax, y_position, annotations
Created on 2021-07-20 by the reprex package (v2.0.0)
Previous answer
One potential solution to your problem is to plot "Type" on the x axis instead of "x", e.g.
data %>%
ggplot(aes(x = Type, y = ratio)) +
geom_col(aes(fill = `Sample Selection`),
position = position_dodge(preserve = "single"), na.rm = TRUE) +
geom_text(position = position_dodge(width = .9), # move to center of bars
aes(label=sprintf("%.02f %%", round(ratio, digits = 1)),
group = `Sample Selection`),
vjust = -1.5,
size = 4,
na.rm = TRUE) +
scale_fill_grey(start = 0.8, end = 0.5) +
scale_y_continuous(expand = expansion(mult = c(0, .1))) +
theme_bw(base_size = 15) +
xlab("Norm group for corporate Hy") +
ylab("Percentage Compliant Decisions") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
geom_signif(annotation=c("p=0.35", "p=0.06", "p=0.05", "p=0.34", "p=0.00"),
y_position = c(30, 40, 55 ,75, 90),
xmin=c(0.75,1.75,2.75,3.75,4.75),
xmax=c(1.25,2.25,3.25,4.25,5.25))

Create a heatmaps with average values on the very right column and bottom row

I want to create a heat map, where average values are depicted on the very right column and in the bottom row. I found a question that is very similar to mine -
heatmap with values and some additional features in R
Everything works well, however, I do not need highlighted cells for average values. Could you help me to unhighlight cells with average values?
library(ggplot2)
q1<-structure(list(hour = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24), E = c(-15.6,
-17.2, -13, -11.6, -9.8, -13.2, -16.8, -15.8, -13.8, -12.8, -26.4,
-42.2, -40.8, -38, -41.2, -48.2, -47.2, -42.4, -18.4, -35.4,
-47.2, -42, -26.6, -24.8), K = c(-15.8, -23, -22.4, -15.8, -15.6,
-18.5, -21.4, -24.4, -18.8, -15.4, -46.8, -53.4, -43.6, -42.4,
-48.6, -48, -57, -19.2, -14.8, -23.2, -39.2, -43.8, -28, -13),
L = c(-20.6, -19.8, -12.6, -7.4, -11, -23.8, -25.2, -21.4,
-20.4, -20.2, -17.6, -18, -20.2, -30.4, -22.6, -25.8, -31.8,
-35.6, -43, -37, -36.8, -44, -47.4, -33.2), N = c(-15.4,
-28.6, -16.6, -15.6, -18.4, -20.2, -25, -22.8, -14.2, -10.8,
-31.4, -37.6, -37.2, -32, -37.4, -40.4, -40.4, -23, -8.6,
-11, -23.2, -33, -26.2, -8.2), P = c(-17, -16.8, -22.6, -15.8,
-15, -23, -23.8, -21.8, -17.4, -17.4, -21.2, -18.2, -11,
-14.6, -26.6, -31, -27.4, -29.4, -27.4, -29.6, -33, -34.8,
-16, -17.4), R = c(-7, -22.4, -20, -12, -15.4, -18.8, -22.6,
-20.4, -10.2, -4.4, -21.2, -28, -30.6, -29.4, -26, -22.8,
-31.4, -20.4, -4.8, -18.6, -27.2, -35, -48.8, -32.6), T = c(-19.4,
-21.2, -16.2, -16.6, -16.8, -16.2, -21.8, -23.4, -13, -13,
-36.2, -42.2, -41, -38.4, -39.6, -47, -43.2, -42.4, -21.8,
-23.8, -40.2, -34.6, -23.8, -15), Average = c(-15.8285714285714,
-21.2857142857143, -17.6285714285714, -13.5428571428571,
-14.5714285714286, -19.1, -22.3714285714286, -21.4285714285714,
-15.4, -13.4285714285714, -28.6857142857143, -34.2285714285714,
-32.0571428571429, -32.1714285714286, -34.5714285714286,
-37.6, -39.7714285714286, -30.3428571428571, -19.8285714285714,
-25.5142857142857, -35.2571428571429, -38.1714285714286,
-30.9714285714286, -20.6)), row.names = c(NA, -24L), class = "data.frame")
q1$Average<-rowMeans(q1[,2:8])
dat2 <- stack(q1[-1])
dat2$hour <- q1$hour
dat2$ind <- factor(dat2$ind, levels=c("E","T","K","N","R","L","P", "Average"))
ggplot(mapping = aes(ind, hour)) +
geom_tile(aes(fill = values), subset(dat2, hour != "Average" & ind != "Sum")) +
geom_text(aes(label = round(values, 1)), dat2) +
scale_y_discrete(limits = c("Average", 24:1)) +
scale_x_discrete(limits = c("E","T","K","N","R","L","P", "Average"), position = "top") +
viridis::scale_fill_viridis() +
theme_minimal() + theme(axis.title = element_blank())
You could make use of an ifelse to replace the values mapped on fill to NA for your average column and row like so. The value to be used for the NA value could then be set via the na.value argument of scale_fill_xxx where I chose NA or transparent:
library(ggplot2)
ggplot(mapping = aes(ind, hour)) +
geom_tile(aes(fill = ifelse(!(ind == "Average" | hour == 1), values, NA)), subset(dat2, hour != "Average" & ind != "Sum")) +
geom_text(aes(label = round(values, 1)), dat2) +
scale_y_discrete(limits = c("Average", 24:1)) +
scale_x_discrete(limits = c("E","T","K","N","R","L","P", "Average"), position = "top") +
viridis::scale_fill_viridis(na.value = NA) +
theme_minimal() + theme(axis.title = element_blank()) +
labs(fill = "values")
You can try barplots instead of numbers.
library(ComplexHeatmap)
row_ha = rowAnnotation(Average = anno_barplot(q1$Average,axis_param = list(direction = "reverse")))
column_ha = HeatmapAnnotation(Average = anno_barplot(colMeans(q1[,2:8])))
ComplexHeatmap::Heatmap(as.matrix(q1[,-c(1, ncol(q1))]), right_annotation = row_ha, top_annotation = column_ha, col = viridis::viridis(10))
Or points with regression line and boxplots showing medians instead of means
row_ha = rowAnnotation(Average = anno_lines(q1$Average,axis_param = list(direction = "reverse"), smooth =T))
column_ha = HeatmapAnnotation(summary = anno_boxplot(as.matrix(q1[,-c(1, ncol(q1))])))
Without clustering on row and columns
Heatmap(name = "value", as.matrix(q1[,-c(1, ncol(q1))]), right_annotation = row_ha, top_annotation = column_ha,
col = viridis::viridis(10),cluster_rows = F, cluster_columns = F)
There are a couple of R-packages availabel for heatmaps. Thus, you can try the superheat package as well.
library(superheat)
superheat(as.matrix(q1[,-c(1, ncol(q1))]),
yr = q1$Average,
yr.plot.type = "scattersmooth",
yr.axis.name = "Average",
yt = colMeans(q1[,2:8]),
yt.plot.type = "bar",
yt.axis.name = "Average")

how to plot two graphs together while share the same scale of x-axis

I would like to plot two graph together which share the same x-axis. How can I do that?
My data can be build using codes:
df <-structure(list(SDTM_LabN = c("ALP", "AST", "ALT", "AST", "ALT",
"ALT", "ALP", "AST", "ALP", "AST", "ALP", "ALT", "ALP", "ALP",
"ALT", "AST", "ALT", "ALT", "ALT", "AST", "AST", "ALP", "AST",
"ALT", "ALP", "ALP", "AST"), ADY = structure(c(45, 15, 1, 1,
30, 58, 30, 45, 46, -6, 23, 46, -6, 15, 23, 46, 45, -6, 8, 30,
58, 58, 23, 15, 8, 1, 8), class = "difftime", units = "days"),
result = c(0.841269841269841, 0.578947368421053, 0.625, 0.552631578947368,
0.416666666666667, 0.3125, 0.936507936507937, 0.447368421052632,
0.634920634920635, 0.657894736842105, 0.873015873015873,
0.291666666666667, 0.73015873015873, 0.857142857142857, 0.5,
0.447368421052632, 0.479166666666667, 0.625, 0.604166666666667,
0.5, 0.526315789473684, 0.849206349206349, 0.526315789473684,
0.5, 1.00793650793651, 0.896825396825397, 0.894736842105263
)), row.names = c(NA, -27L), class = "data.frame")
df2<-structure(list(ID = c(101, 101, 101, 101, 101, 101), AEDECOD = c("Diarrhoea",
"Vitreous floaters", "Musculoskeletal pain", "Diarrhoea", "Decreased appetite",
"Fatigue"), AESTDY = structure(c(101, 74, 65, 2, 33, 27), class = "difftime", units = "days"),
AEENDY = structure(c(105, 99, NA, 5, NA, NA), class = "difftime", units = "days")), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))
My plots codes are:
ggplot(df, aes(colour=SDTM_LabN)) +
geom_line(aes(x=ADY,y=result))
ggplot(df2, aes(colour=AEDECOD)) +
geom_segment(aes(x=AESTDY, xend=AEENDY, y=AEDECOD, yend=AEDECOD),) +
xlab("Duration")
How can I get sth that looks like this:
You should first make sure to calculate common xmin-xmax to both series.
Then with patwhwork a suggested in comments or cowplot:
xmin <- min(df$ADY ,df2$AESTDY)
xmax <- max(df$ADY ,df2$AESTDY)
p1 <- ggplot(df, aes(colour=SDTM_LabN)) +
geom_line(aes(x=ADY,y=result)) +
coord_cartesian(xlim = c(xmin,xmax))
p2 <- ggplot(df2, aes(colour=AEDECOD)) +
geom_segment(aes(x=AESTDY, xend=AEENDY, y=AEDECOD, yend=AEDECOD),) +
xlab("Duration") +
coord_cartesian(xlim = c(xmin,xmax))
library(cowplot)
plot_grid(plotlist = list(p1,p2),align='v',ncol=1)

Annotate ggplot based on a second data frame

I have a faceted plot made with ggplot that is already working, it shows data about river altitude against years. I'm trying to add arrows based on a second dataframe which details when floods occurred.
Here's the current plot:
I would like to draw arrows in the top part of each graph based on date information in my second dataframe where each row corresponds to a flood and contains a date.
The link between the two dataframes is the Station_code column, each river has one or more stations which is indicated by this data (in this case only the Var river has two stations).
Here is the dput of the data frame used to create the original plot:
structure(list(River = c("Durance", "Durance", "Durance", "Durance",
"Roya", "Var"), Reach = c("La Brillanne", "Les Mées", "La Brillanne",
"Les Mées", "Basse vallée", "Basse vallée"), Area_km = c(465,
465, 465, 465, 465, 465), Type = c("restored", "target", "restored",
"target", "witness", "restored"), Year = c(2017, 2017, 2012,
2012, 2018, 2011), Restoration_year = c(2013, 2013, 2013, 2013,
NA, 2009), Station_code = c("X1130010", "X1130010", "X1130010",
"X1130010", "Y6624010", "Y6442015"), BRI_adi_moy_sstransect = c(0.00375820736746399,
0.00244752138003355, 0.00446807607783864, 0.0028792618981479,
0.00989200896930529, 0.00357247516596474), SD_sstransect = c(0.00165574247612667,
0.0010044634990875, 0.00220534492332107, 0.00102694633805149,
0.00788573233793128, 0.00308489160008849), min_BRI_sstransect = c(0.00108123849595469,
0.00111493913953216, 0.000555500340370182, 0.00100279590198288,
0, 0), max_BRI_sstransect = c(0.0127781240385231, 0.00700537285706352,
0.0210216858227621, 0.00815151653110584, 0.127734814926934, 0.0223738711013954
), Nb_sstr_unique_m = c(0.00623321576795815, 0.00259754717331206,
0.00117035034437559, 0.00209845092352825, 0.0458628969163946,
3.60620609570031), BRI_adi_moy_transect = c(0.00280232169999531,
0.00173868254527501, 0.00333818552810438, 0.00181398859573415,
0.00903651639185542, 0.00447856455432537), SD_transect = c(0.00128472161839638,
0.000477209421076879, 0.00204050725984513, 0.000472466654940182,
0.00780731734792112, 0.00310039904793707), min_BRI_transect = c(0.00108123849595469,
0.00106445386542223, 0.000901992689363725, 0.000855135344651009,
0.000944414463851629, 0.000162012161197014), max_BRI_transect = c(0.00709151795418251,
0.00434366293208643, 0.011717024999411, 0.0031991369873946, 0.127734814926934,
0.0187952134332499), Nb_tr_unique_m = c(0, 0, 0, 0, 0, 0), Error_reso = c(0.0011,
8e-04, 0.0018, 0.0011, 0.0028, 0.0031), W_BA = c(296.553323029366,
411.056574923547, 263.944186046512, 363.32874617737, 88.6420798065296,
158.66866970576), W_BA_sd = c(84.1498544481585, 65.3909073242282,
100.067554749308, 55.5534084807705, 35.2337070278364, 64.6978349498119
), W_BA_min = c(131, 206, 33, 223, 6, 45), W_BA_max = c(472,
564, 657, 513, 188, 381), W_norm = c(5.73271228619998, 7.9461900926133,
5.10234066090722, 7.02355699765464, 5.09378494746752, 4.81262001531126
), W_norm_sd = c(1.62671218635823, 1.2640804493236, 1.93441939783807,
1.07391043231191, 2.02469218788178, 1.96236658443141), W_norm_min = c(2.53237866910643,
3.98221378500706, 0.637927450996277, 4.31084307794454, 0.344787822572658,
1.36490651299098), W_norm_max = c(9.12429566273463, 10.9027600715727,
12.7005556152895, 9.91687219276031, 10.8033517739433, 11.5562084766569
)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
))
And here is the dput of the date frame containing the flooding date:
structure(list(Station_code = c("Y6042010", "Y6042010", "Y6042010",
"Y6042010", "Y6042010", "Y6042010"), Date = structure(c(12006,
12007, 12016, 12017, 13416, 13488), class = "Date"), Qm3s = c(156,
177, 104, 124, 125, 90.4), Qual = c(5, 5, 5, 5, 5, 5), Year = c(2002,
2002, 2002, 2002, 2006, 2006), Month = c(11, 11, 11, 11, 9, 12
), Station_river = c("Var#Entrevaux", "Var#Entrevaux", "Var#Entrevaux",
"Var#Entrevaux", "Var#Entrevaux", "Var#Entrevaux"), River = c("Var",
"Var", "Var", "Var", "Var", "Var"), Mod_inter = c(13.32, 13.32,
13.32, 13.32, 13.32, 13.32), Qm3s_norm = c(11.7117117117117,
13.2882882882883, 7.80780780780781, 9.30930930930931, 9.38438438438438,
6.78678678678679), File_name = c("Var#Entrevaux.dat", "Var#Entrevaux.dat",
"Var#Entrevaux.dat", "Var#Entrevaux.dat", "Var#Entrevaux.dat",
"Var#Entrevaux.dat"), Station_name = c("#Entrevaux", "#Entrevaux",
"#Entrevaux", "#Entrevaux", "#Entrevaux", "#Entrevaux"), Reach = c("Daluis",
"Daluis", "Daluis", "Daluis", "Daluis", "Daluis"), Restauration_year = c(2009,
2009, 2009, 2009, 2009, 2009), `Area_km[BH]` = c(676, 676, 676,
676, 676, 676), Starting_year = c(1920, 1920, 1920, 1920, 1920,
1920), Ending_year = c("NA", "NA", "NA", "NA", "NA", "NA"), Accuracy = c("good",
"good", "good", "good", "good", "good"), Q2 = c(86, 86, 86, 86,
86, 86), Q5 = c(120, 120, 120, 120, 120, 120), Q10 = c(150, 150,
150, 150, 150, 150), Q20 = c(170, 170, 170, 170, 170, 170), Q50 = c(200,
200, 200, 200, 200, 200), Data_producer = c("DREAL_PACA", "DREAL_PACA",
"DREAL_PACA", "DREAL_PACA", "DREAL_PACA", "DREAL_PACA"), Coord_X_L2e_Z32 = c(959313,
959313, 959313, 959313, 959313, 959313), Coord_Y_L2e_Z32 = c(1893321,
1893321, 1893321, 1893321, 1893321, 1893321), Coord_X_L93 = c(1005748.88,
1005748.88, 1005748.88, 1005748.88, 1005748.88, 1005748.88),
Coord_Y_L93 = c(6324083.97, 6324083.97, 6324083.97, 6324083.97,
6324083.97, 6324083.97), New_FN = c("Var#Entrevaux.csv",
"Var#Entrevaux.csv", "Var#Entrevaux.csv", "Var#Entrevaux.csv",
"Var#Entrevaux.csv", "Var#Entrevaux.csv"), NA_perc = c(14.92,
14.92, 14.92, 14.92, 14.92, 14.92), Q2_norm = c(6.45645645645646,
6.45645645645646, 6.45645645645646, 6.45645645645646, 6.45645645645646,
6.45645645645646), Q5_norm = c(9.00900900900901, 9.00900900900901,
9.00900900900901, 9.00900900900901, 9.00900900900901, 9.00900900900901
), Q10_norm = c(11.2612612612613, 11.2612612612613, 11.2612612612613,
11.2612612612613, 11.2612612612613, 11.2612612612613), Q20_norm = c(12.7627627627628,
12.7627627627628, 12.7627627627628, 12.7627627627628, 12.7627627627628,
12.7627627627628), Q50_norm = c(15.015015015015, 15.015015015015,
15.015015015015, 15.015015015015, 15.015015015015, 15.015015015015
)), row.names = c(NA, -6L), groups = structure(list(Station_code = "Y6042010",
.rows = structure(list(1:6), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = 1L, class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
EDIT
Here is an example of what I would like to do on the plot:
This is the code I use currently to do the plot:
ggplot(data = tst_formule[tst_formule$River != "Roya",], aes(x = Year, y = BRI_adi_moy_transect, shape = River, col = Type)) +
geom_point(size = 3) +
geom_errorbar(aes(ymin = BRI_adi_moy_transect - SD_transect, ymax = BRI_adi_moy_transect + SD_transect), size = 0.7, width = 0.3) +
geom_errorbar(aes(ymin = BRI_adi_moy_transect - Error_reso, ymax = BRI_adi_moy_transect + Error_reso, linetype = "Error due to resolution"), size = 0.3, width = 0.3, colour = "black") +
scale_linetype_manual(name = NULL, values = 2) +
scale_shape_manual(values = c(15, 18, 17, 16)) +
scale_colour_manual(values = c("chocolate1", "darkcyan")) +
new_scale("linetype") +
geom_vline(aes(xintercept = Restoration_year, linetype = "Restoration"), colour = "chocolate1") +
scale_linetype_manual(name = NULL, values = 5) +
new_scale("linetype") +
geom_hline(aes(yintercept = 0.004, linetype = "Threshold"), colour= 'black') +
scale_linetype_manual(name = NULL, values = 4) +
scale_y_continuous("BRI*", limits = c(min(tst_formule$BRI_adi_moy_transect - tst_formule$SD_transect, tst_formule$BRI_adi_moy_transect - tst_formule$Error_reso ), max(tst_formule$BRI_adi_moy_transect + tst_formule$SD_transect, tst_formule$BRI_adi_moy_transect + tst_formule$Error_reso))) +
scale_x_continuous(limits = c(min(tst_formule$Year - 1),max(tst_formule$Year + 1)), breaks = scales::breaks_pretty(n = 6)) +
theme_bw() +
facet_wrap(vars(River)) +
theme(legend.spacing.y = unit(-0.01, "cm")) +
guides(shape = guide_legend(order = 1),
colour = guide_legend(order = 2),
line = guide_legend(order = 3))
After tests and more research, I managed to do it by adding the second dataframe in geom_text():
new_scale("linetype") +
geom_segment(data = Flood_plot, aes(x = Date, xend = Date, y = 0.025, yend = 0.020, linetype = "Morphogenic flood"), arrow = arrow(length = unit(0.2, "cm")), inherit.aes = F, guide = guide_legend(order = 6)) +
scale_linetype_manual(name = NULL, values = 1) +
new_scale() creates a new linetype definition after the ones I created before, geom_segment() allows to draw arrows which I wanted but it works with geom_text() and scale_linetype_manual() draws the arrow in the legend without the mention "linetype" above. The second dataframe has the same column (River) as the 1st one to wrap and create the panels.

can't add labels to my graph

I have this graph:
I just need to add labels to each colored line.
I need to add to the blue one Forecast Sales and for the red one Historical Sales.
I tried to adapt these examples here but I have much error. Also, I can not plot the graph above just by using this code:
to make it reproductible :
dput(df1)
structure(list(Semaine = c(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), M = c(5649.96284329564, 7400.19639744335, 6948.61488673139,
5043.28209277238, 7171.29719525351, 7151.04746494067, 5492.96601941748,
6796.1160130719, 5532.95496473142, 7371.33061889251, 5462.73861171367,
7156.01570964247, 5558.63194819212, 9329.49289405685, 5770.02903225806,
7348.68497576737, 5261.26655896607, 8536.11304909561, 7463.97630586968,
6133.49774339136, 7252.69089929995, 6258.54674403611, 8167.67766497462,
5644.66612816371, 7512.5169628433, 5407.84275713516, 7795.63220247711,
5596.75282714055, 7264.37264404954, 5516.98492191707, 8188.80776699029
> dput(df2)
structure(list(Semaine = c(32, 33.2, 34.4, 35.6, 36.8, 38), M = c(5820.32304669441,
6296.32038834951, 7313.24757281553, 7589.714214588, 8992.35922330097,
9664.95469255663)), .Names = c("Semaine", "M"), row.names = c(NA,
-6L), class = "data.frame")
ggplot() + geom_line(data=df1, aes(x = Semaine, y = M),color = "red") +
stat_smooth(data=df2, aes(x = Semaine, y = M),color = "blue")+
scale_x_continuous(breaks = seq(0,40,1))
Thank you!
cols <- c("A"="red", "B"="blue")
ggplot() + geom_line(data=df1, aes(x = Semaine, y = M,color = "A")) +
stat_smooth(data=df2, aes(x = Semaine, y = M,color = "B"), method = 'loess')+
scale_x_continuous(breaks = seq(0,40,1)) +
scale_color_manual(name="Title", values=cols)

Resources