I have searched several topics in this community related to my topic and none of the solutions have worked for me. They have either created odd replicates of the Legend items or added the glyph to each legend item.
I was wondering how I could get my vertical line and its color represented in my ggplot. This repex() is a bit long as I wanted a decent representative dataset to create the plot.
``` r
library(ggplot2)
library(gghalves)
library(ggdist)
library(ggtext)
library(colorspace)
library(tidyverse)
plot.means.test <- data.frame(
stringsAsFactors = FALSE,
HumanInfluence = c("None","None","None","None",
"None","None","None","None",
"None","None","None","None","None",
"None","None","None","None","None",
"None","None","None","None","None",
"None","None","None","None",
"None","None","None","None","None",
"None","None","None","None","None",
"None","None","None","None","None",
"None","None","None","None",
"None","None","None","None","None",
"None","None","None","None","None",
"None","None","None","None","None",
"None","None","None","None",
"None","None","None","None","None",
"None","None","None","None","None",
"None","None","None","None","None",
"None","None","None","None",
"None","None","None","None","None",
"None","None","None","None","None",
"None","None","None","None","None",
"None"),
EvaluationID = c("BS-LS-1001_2017-08-02",
"BS-LS-1001_2017-08-02",
"BS-LS-1010_2018-08-03","BS-LS-1010_2018-08-03",
"BS-LS-1018_2019-08-07",
"BS-LS-1018_2019-08-07","BS-LS-1023_2017-08-29",
"BS-LS-1023_2017-08-29",
"BS-LS-1024_2017-08-03","BS-LS-1024_2017-08-03",
"BS-LS-1033_2017-08-03",
"BS-LS-1033_2017-08-03","BS-LS-1035_2017-08-27",
"BS-LS-1035_2017-08-27","BS-LS-1038_2019-08-10",
"BS-LS-1038_2019-08-10",
"BS-LS-1046_2017-08-27","BS-LS-1046_2017-08-27",
"BS-LS-1047_2017-08-11",
"BS-LS-1047_2017-08-11","BS-LS-1049_2017-08-09",
"BS-LS-1049_2017-08-09",
"BS-LS-1054_2018-08-02","BS-LS-1054_2018-08-02",
"BS-LS-1067_2018-08-01",
"BS-LS-1067_2018-08-01","BS-LS-1069_2019-08-11",
"BS-LS-1069_2019-08-11",
"BS-RV-1013_2018-07-31","BS-RV-1013_2018-07-31",
"BS-RV-1021_2017-08-08",
"BS-RV-1021_2017-08-08","BS-RV-1044_2017-08-07",
"BS-RV-1044_2017-08-07",
"BS-RV-1053_2017-08-10","BS-RV-1053_2017-08-10",
"BS-SS-1003_2019-08-08",
"BS-SS-1003_2019-08-08","BS-SS-1017_2017-08-09",
"BS-SS-1017_2017-08-09",
"BS-SS-1022_2018-08-03","BS-SS-1022_2018-08-03",
"BS-SS-1025_2017-08-10",
"BS-SS-1025_2017-08-10","BS-SS-1039_2017-08-29",
"BS-SS-1039_2017-08-29",
"BS-SS-1050_2017-08-08","BS-SS-1050_2017-08-08",
"BS-SS-1052_2017-08-04",
"BS-SS-1052_2017-08-04","BS-SS-1058_2019-08-06",
"BS-SS-1058_2019-08-06","BS-SS-1071_2017-08-28",
"BS-SS-1071_2017-08-28",
"BS-SS-1076_2019-08-09","BS-SS-1076_2019-08-09",
"BS-SS-1091_2019-08-10",
"BS-SS-1091_2019-08-10","BS-SS-1095_2019-08-12",
"BS-SS-1095_2019-08-12",
"BS-SS-1098_2019-08-14","BS-SS-1098_2019-08-14",
"BS-SS-1107_2019-08-13",
"BS-SS-1107_2019-08-13","CBN-LS-1014_2019-07-23",
"CBN-LS-1014_2019-07-23",
"CBN-SS-1003_2019-07-22","CBN-SS-1003_2019-07-22",
"CBN-SS-1015_2019-07-22",
"CBN-SS-1015_2019-07-22",
"CBS-LS-1002_2019-07-25","CBS-LS-1002_2019-07-25",
"CBS-LS-1007_2019-07-29",
"CBS-LS-1007_2019-07-29","CBS-LS-1014_2019-07-30",
"CBS-LS-1014_2019-07-30",
"CBS-SS-1001_2019-07-21","CBS-SS-1001_2019-07-21",
"CBS-SS-1006_2019-07-29",
"CBS-SS-1006_2019-07-29","CY-LS-1001_2016-07-01",
"CY-LS-1001_2016-07-01",
"CY-LS-1004_2016-07-07","CY-LS-1004_2016-07-07",
"CY-LS-1005_2017-07-29",
"CY-LS-1005_2017-07-29","CY-LS-1006_2016-07-08",
"CY-LS-1006_2016-07-08",
"CY-LS-1020_2017-07-30","CY-LS-1020_2017-07-30",
"CY-LS-1023_2016-07-01",
"CY-LS-1023_2016-07-01","CY-LS-1041_2017-07-31",
"CY-LS-1041_2017-07-31",
"CY-LS-1046_2017-07-27","CY-LS-1046_2017-07-27",
"CY-LS-1056_2016-07-02",
"CY-LS-1056_2016-07-02","CY-LS-1057_2016-07-09",
"CY-LS-1057_2016-07-09"),
StreamOrder = c("2","2","2","2","3","3",
"3","3","3","3","2","2","3","3",
"3","3","2","2","2","2","2",
"2","2","2","2","2","3","3","4",
"4","4","4","4","4","5","5","1",
"1","1","1","1","1","1","1",
"1","1","1","1","1","1","1","1",
"1","1","1","1","1","1","1",
"1","1","1","1","1","2","2","1",
"1","1","1","3","3","3","3","2",
"2","1","1","1","1","2","2",
"2","2","2","2","3","3","2","2",
"3","3","2","2","2","2","3",
"3","2","2"),
EcoregionStreamSize = c("Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Bottomlands_SmallWadeable",
"Interior Bottomlands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Subarctic Coastal Plains_SmallWadeable",
"Subarctic Coastal Plains_SmallWadeable",
"Interior Bottomlands_SmallWadeable",
"Interior Bottomlands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable","Interior Highlands_SmallWadeable",
"Interior Highlands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Ahklun and Kilbuck Mountains_LargeWadeable",
"Ahklun and Kilbuck Mountains_LargeWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_LargeWadeable",
"Interior Forested Lowlands and Uplands_LargeWadeable",
"Subarctic Coastal Plains_SmallWadeable",
"Subarctic Coastal Plains_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Bottomlands_SmallWadeable",
"Interior Bottomlands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Bottomlands_SmallWadeable",
"Interior Bottomlands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Brooks Range_LargeWadeable",
"Brooks Range_LargeWadeable","Brooks Range_LargeWadeable",
"Brooks Range_LargeWadeable",
"Brooks Range_SmallWadeable",
"Brooks Range_SmallWadeable",
"Brooks Range_SmallWadeable","Brooks Range_SmallWadeable",
"Brooks Range_LargeWadeable",
"Brooks Range_LargeWadeable",
"Brooks Range_SmallWadeable",
"Brooks Range_SmallWadeable","Brooks Range_SmallWadeable",
"Brooks Range_SmallWadeable",
"Brooks Range_SmallWadeable",
"Brooks Range_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Highlands_SmallWadeable",
"Interior Highlands_SmallWadeable",
"Interior Highlands_SmallWadeable","Interior Highlands_SmallWadeable",
"Interior Highlands_",
"Interior Highlands_",
"Interior Highlands_LargeWadeable",
"Interior Highlands_LargeWadeable",
"Interior Highlands_SmallWadeable","Interior Highlands_SmallWadeable",
"Interior Highlands_SmallWadeable",
"Interior Highlands_SmallWadeable",
"Brooks Range_LargeWadeable",
"Brooks Range_LargeWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable",
"Interior Forested Lowlands and Uplands_SmallWadeable"),
ind = c("PctPools","PctBankStable",
"PctPools","PctBankStable","PctPools",
"PctBankStable","PctPools",
"PctBankStable","PctPools","PctBankStable",
"PctPools","PctBankStable",
"PctPools","PctBankStable","PctPools",
"PctBankStable","PctPools","PctBankStable",
"PctPools","PctBankStable",
"PctPools","PctBankStable","PctPools",
"PctBankStable","PctPools",
"PctBankStable","PctPools","PctBankStable",
"PctPools","PctBankStable","PctPools",
"PctBankStable","PctPools",
"PctBankStable","PctPools","PctBankStable",
"PctPools","PctBankStable","PctPools",
"PctBankStable","PctPools",
"PctBankStable","PctPools","PctBankStable",
"PctPools","PctBankStable","PctPools",
"PctBankStable","PctPools",
"PctBankStable","PctPools","PctBankStable",
"PctPools","PctBankStable",
"PctPools","PctBankStable","PctPools",
"PctBankStable","PctPools",
"PctBankStable","PctPools","PctBankStable",
"PctPools","PctBankStable","PctPools",
"PctBankStable","PctPools",
"PctBankStable","PctPools","PctBankStable",
"PctPools","PctBankStable","PctPools",
"PctBankStable","PctPools",
"PctBankStable","PctPools","PctBankStable",
"PctPools","PctBankStable","PctPools",
"PctBankStable","PctPools",
"PctBankStable","PctPools","PctBankStable",
"PctPools","PctBankStable",
"PctPools","PctBankStable","PctPools",
"PctBankStable","PctPools","PctBankStable",
"PctPools","PctBankStable",
"PctPools","PctBankStable","PctPools",
"PctBankStable"),
value = c(9.73,100,15.3,100,33.83,7,
77.67,90,0,98,0,100,0,95,
14.15,2,0,100,0,88,0,93,0,100,0,
100,0,33,17.43,100,9,100,19.18,
95,41.47,100,10.04,100,0,100,
6.5,100,2.2,100,3.07,100,0,98,
0,98,0,100,12.57,100,38.08,100,
3.88,100,0,100,0,100,12.39,100,
18.38,90,21.59,98,27.79,95,
31.83,83,NA,77,1.7,52,22.28,100,
9.5,100,NA,40,NA,97,NA,100,NA,
NA,NA,100,NA,86,NA,98,0,76,NA,
95,NA,100),
ref.mean = c(17.6431967213115,
92.5441176470588,17.6431967213115,
92.5441176470588,17.6431967213115,92.5441176470588,
17.6431967213115,92.5441176470588,
17.6431967213115,92.5441176470588,
17.6431967213115,92.5441176470588,
17.6431967213115,92.5441176470588,
17.6431967213115,92.5441176470588,
17.6431967213115,92.5441176470588,
17.6431967213115,92.5441176470588,
17.6431967213115,92.5441176470588,
17.6431967213115,92.5441176470588,17.6431967213115,
92.5441176470588,17.6431967213115,
92.5441176470588,17.6431967213115,
92.5441176470588,17.6431967213115,
92.5441176470588,17.6431967213115,
92.5441176470588,17.6431967213115,
92.5441176470588,17.6431967213115,
92.5441176470588,17.6431967213115,
92.5441176470588,17.6431967213115,
92.5441176470588,17.6431967213115,92.5441176470588,
17.6431967213115,92.5441176470588,
17.6431967213115,92.5441176470588,
17.6431967213115,92.5441176470588,
17.6431967213115,92.5441176470588,
17.6431967213115,92.5441176470588,
17.6431967213115,92.5441176470588,
17.6431967213115,92.5441176470588,
17.6431967213115,92.5441176470588,
17.6431967213115,92.5441176470588,17.6431967213115,
92.5441176470588,17.6431967213115,
92.5441176470588,17.6431967213115,
92.5441176470588,17.6431967213115,
92.5441176470588,17.6431967213115,
92.5441176470588,17.6431967213115,
92.5441176470588,17.6431967213115,
92.5441176470588,17.6431967213115,
92.5441176470588,17.6431967213115,
92.5441176470588,17.6431967213115,92.5441176470588,
17.6431967213115,92.5441176470588,
17.6431967213115,92.5441176470588,
17.6431967213115,92.5441176470588,
17.6431967213115,92.5441176470588,
17.6431967213115,92.5441176470588,
17.6431967213115,92.5441176470588,
17.6431967213115,92.5441176470588,
17.6431967213115,92.5441176470588,
17.6431967213115,92.5441176470588),
ref.median = c(12.935,100,12.935,100,
12.935,100,12.935,100,12.935,100,
12.935,100,12.935,100,12.935,100,
12.935,100,12.935,100,12.935,100,
12.935,100,12.935,100,12.935,100,
12.935,100,12.935,100,12.935,100,
12.935,100,12.935,100,12.935,100,
12.935,100,12.935,100,12.935,100,
12.935,100,12.935,100,12.935,
100,12.935,100,12.935,100,12.935,
100,12.935,100,12.935,100,12.935,
100,12.935,100,12.935,100,12.935,
100,12.935,100,12.935,100,12.935,
100,12.935,100,12.935,100,
12.935,100,12.935,100,12.935,100,
12.935,100,12.935,100,12.935,100,
12.935,100,12.935,100,12.935,100,
12.935,100),
tar.mean = c(11.7748148148148,
79.2142857142857,11.7748148148148,
79.2142857142857,11.7748148148148,79.2142857142857,
11.7748148148148,79.2142857142857,
11.7748148148148,79.2142857142857,
11.7748148148148,79.2142857142857,
11.7748148148148,79.2142857142857,
11.7748148148148,79.2142857142857,
11.7748148148148,79.2142857142857,
11.7748148148148,79.2142857142857,
11.7748148148148,79.2142857142857,
11.7748148148148,79.2142857142857,11.7748148148148,
79.2142857142857,11.7748148148148,
79.2142857142857,11.7748148148148,
79.2142857142857,11.7748148148148,
79.2142857142857,11.7748148148148,
79.2142857142857,11.7748148148148,
79.2142857142857,11.7748148148148,
79.2142857142857,11.7748148148148,
79.2142857142857,11.7748148148148,
79.2142857142857,11.7748148148148,79.2142857142857,
11.7748148148148,79.2142857142857,
11.7748148148148,79.2142857142857,
11.7748148148148,79.2142857142857,
11.7748148148148,79.2142857142857,
11.7748148148148,79.2142857142857,
11.7748148148148,79.2142857142857,
11.7748148148148,79.2142857142857,
11.7748148148148,79.2142857142857,
11.7748148148148,79.2142857142857,11.7748148148148,
79.2142857142857,11.7748148148148,
79.2142857142857,11.7748148148148,
79.2142857142857,11.7748148148148,
79.2142857142857,11.7748148148148,
79.2142857142857,11.7748148148148,
79.2142857142857,11.7748148148148,
79.2142857142857,11.7748148148148,
79.2142857142857,11.7748148148148,
79.2142857142857,11.7748148148148,79.2142857142857,
11.7748148148148,79.2142857142857,
11.7748148148148,79.2142857142857,
11.7748148148148,79.2142857142857,
11.7748148148148,79.2142857142857,
11.7748148148148,79.2142857142857,
11.7748148148148,79.2142857142857,
11.7748148148148,79.2142857142857,
11.7748148148148,79.2142857142857,
11.7748148148148,79.2142857142857),
tar.median = c(2.68,87,2.68,87,2.68,87,
2.68,87,2.68,87,2.68,87,2.68,87,
2.68,87,2.68,87,2.68,87,2.68,
87,2.68,87,2.68,87,2.68,87,2.68,
87,2.68,87,2.68,87,2.68,87,
2.68,87,2.68,87,2.68,87,2.68,87,
2.68,87,2.68,87,2.68,87,2.68,87,
2.68,87,2.68,87,2.68,87,2.68,
87,2.68,87,2.68,87,2.68,87,2.68,
87,2.68,87,2.68,87,2.68,87,
2.68,87,2.68,87,2.68,87,2.68,87,
2.68,87,2.68,87,2.68,87,2.68,87,
2.68,87,2.68,87,2.68,87,2.68,
87,2.68,87),
mining.mean = c(2.644,77.9090909090909,2.644,
77.9090909090909,2.644,
77.9090909090909,2.644,77.9090909090909,2.644,
77.9090909090909,2.644,
77.9090909090909,2.644,77.9090909090909,2.644,
77.9090909090909,2.644,
77.9090909090909,2.644,77.9090909090909,2.644,
77.9090909090909,2.644,
77.9090909090909,2.644,77.9090909090909,2.644,
77.9090909090909,2.644,77.9090909090909,
2.644,77.9090909090909,2.644,
77.9090909090909,2.644,77.9090909090909,
2.644,77.9090909090909,2.644,
77.9090909090909,2.644,77.9090909090909,
2.644,77.9090909090909,2.644,
77.9090909090909,2.644,77.9090909090909,
2.644,77.9090909090909,2.644,
77.9090909090909,2.644,77.9090909090909,
2.644,77.9090909090909,2.644,
77.9090909090909,2.644,77.9090909090909,
2.644,77.9090909090909,2.644,
77.9090909090909,2.644,77.9090909090909,
2.644,77.9090909090909,2.644,
77.9090909090909,2.644,77.9090909090909,2.644,
77.9090909090909,2.644,
77.9090909090909,2.644,77.9090909090909,2.644,
77.9090909090909,2.644,
77.9090909090909,2.644,77.9090909090909,2.644,
77.9090909090909,2.644,
77.9090909090909,2.644,77.9090909090909,2.644,
77.9090909090909,2.644,
77.9090909090909,2.644,77.9090909090909,2.644,
77.9090909090909,2.644,
77.9090909090909),
mining.median = c(1.075,84.5,1.075,84.5,1.075,
84.5,1.075,84.5,1.075,84.5,
1.075,84.5,1.075,84.5,1.075,84.5,
1.075,84.5,1.075,84.5,1.075,84.5,
1.075,84.5,1.075,84.5,1.075,84.5,
1.075,84.5,1.075,84.5,1.075,84.5,
1.075,84.5,1.075,84.5,1.075,84.5,
1.075,84.5,1.075,84.5,1.075,84.5,
1.075,84.5,1.075,84.5,1.075,
84.5,1.075,84.5,1.075,84.5,1.075,
84.5,1.075,84.5,1.075,84.5,1.075,
84.5,1.075,84.5,1.075,84.5,1.075,
84.5,1.075,84.5,1.075,84.5,1.075,
84.5,1.075,84.5,1.075,84.5,1.075,
84.5,1.075,84.5,1.075,84.5,
1.075,84.5,1.075,84.5,1.075,84.5,
1.075,84.5,1.075,84.5,1.075,84.5,
1.075,84.5)
)
test.metric = "PctPools"
test_plot <- ggplot(data = plot.means.test[plot.means.test$ind %in% test.metric,], aes(x = as.numeric(value), y = factor(1), group = 1)) +
geom_boxplot(
fill = "#8FB7FF", col = "dodgerblue4",
width = .3, size = 1.5, outlier.shape = NA
) +
ggdist::stat_halfeye(
position = position_nudge(y = .6),
fill = "#8FB7FF", col = "dodgerblue4", slab_colour = "#1874CD",
slab_size = .5, point_alpha = 0, .width = 0, adjust = .5
) +
ggforce::geom_sina(
size = 1.5, color = "white", seed = 1, maxwidth = .8#was 4
) +
ggforce::geom_sina(
aes(col = 'Regional Data', shape = 'Regional Data'),#was 3
size = 1, alpha = .7, seed = 1, maxwidth = .8
) +
stat_summary(
fun = "mean", geom = "point",
shape = 17, size = 4, color = "white" #5
) +
stat_summary(
aes(col = "Average", shape = "Average"), fun = "mean", geom = "point",
size = 2.5, show.legend = FALSE #3
) +
geom_vline(
aes(xintercept = tar.median),
col = "orange", lwd = 1.5, linetype = "23", lineend = "round", show.legend = FALSE
) +
guides(color = guide_legend(override.aes = list(size = 4)))+
scale_x_continuous(labels = scales::label_comma(), expand = expansion(mult = .02)) +
scale_color_manual(values = c("Regional Data" = "grey30", "Average" = "dodgerblue", "Targeted" = "orange")) +
scale_shape_manual(values = c("Regional Data" = 16, "Average" = 17, "Targeted" = 108)) +
labs(title = test.metric, x = NULL, y = NULL, col = NULL, fill = NULL, shape = NULL) +
theme_bw(base_size = 20, base_family = "Roboto") +
theme(
plot.title = element_text(size = rel(2), hjust = 0.5),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
panel.background = element_rect(fill='transparent'),
plot.background = element_rect(fill='transparent', color=NA),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = "bottom"
)
test_plot
Thanks for any feedback!!
Created on 2024-02-09 with reprex v2.1.0