How can I plot Facets in R using my data? - r

I have the following data for a US state
d <- structure(list(Month = structure(c(5L, 4L, 9L, 1L, 8L, 7L, 6L,
2L, 12L, 11L, 10L, 3L), .Label = c("01-Abr-14", "01-Ago-14",
"01-Dez-14", "01-Fev-14", "01-Jan-14", "01-Jul-14", "01-Jun-14",
"01-Mai-14", "01-Mar-14", "01-Nov-14", "01-Out-14", "01-Set-14"
), class = "factor"), Ada = c(0.1, 0.14, 0.25, -0.06, -0.15,
0.3, 0.02, -0.01, 0.37, 0.08, 0.15, 0.17), Altus = c(0.06, 0.05,
0.1, -0.17, -0.02, 0.25, -0.02, 0.08, 0, 0.02, 0.02, 0.02), Antlers = c(-0.08,
0.02, 0.1, -0.38, -0.3, 0.27, -0.17, -0.1, 0.11, 0.11, 0.14,
0), Ardmore = c(-0.01, 0.09, 0.18, -0.24, 0.02, 0.21, -0.13,
-0.06, 0.18, 0.01, -0.02, 0.08), Bartlesville = c(-0.02, 0.09,
0.2, 0.16, -0.07, 0.08, 0.01, 0.04, -0.01, 0.11, 0.11, 0.15),
Beaver = c(0.01, -0.02, 0.16, -0.08, -0.04, 0.07, 0.07, 0.07,
-0.12, 0.05, 0, 0.05), Boise.City = c(0.02, -0.05, 0.09,
-0.1, -0.24, 0.05, -0.01, 0.21, -0.01, -0.06, 0, 0.03), Buffalo = c(-0.04,
0, 0.15, -0.05, -0.15, 0.16, -0.11, 0.12, -0.2, 0.03, 0,
0.01), Carnegie = c(0.02, 0.06, 0.15, -0.16, 0.09, 0.24,
-0.03, 0.09, -0.16, 0.06, -0.01, 0), Cherokee = c(0.06, 0.1,
0.26, -0.1, -0.06, 0.2, 0.06, -0.06, -0.06, -0.11, 0, 0.08
), Claremore = c(-0.02, 0.22, 0.18, 0.12, -0.09, 0.11, 0.16,
0.04, 0.46, 0.16, 0.25, 0.17), Durant = c(0.06, 0.05, 0.15,
-0.11, -0.12, 0.35, -0.21, 0, 0.4, 0.04, 0.26, 0.09), Enid = c(0.07,
0.08, 0.34, 0, 0.18, 0.34, 0.11, 0.09, -0.04, 0.17, 0.13,
0.1), Erick = c(0.05, 0.06, 0.14, -0.15, -0.09, 0.07, -0.05,
0.13, 0.01, -0.02, 0.04, 0.02), Geary = c(-0.01, 0.01, 0.15,
-0.19, -0.09, 0.14, -0.18, 0.14, 0.02, 0.05, -0.07, 0), Goodwell = c(0,
-0.05, 0.08, -0.08, -0.01, 0, -0.06, 0.03, -0.12, 0.02, -0.03,
0), Guthrie = c(0.06, 0.13, 0.23, -0.09, 0.06, 0.31, -0.03,
0.05, -0.01, 0.03, 0.09, 0.11), Hammon = c(0.02, 0.03, 0.14,
-0.2, -0.04, 0.1, -0.1, 0.23, -0.07, 0.05, 0.03, 0.03), Hennessey = c(0.02,
0.09, 0.22, -0.04, 0.1, 0.22, 0.13, 0.2, 0.08, -0.01, 0.03,
0.07), Hobartmuni = c(-0.03, -0.02, 0.1, -0.17, -0.29, -0.08,
0.02, 0.04, -0.05, -0.03, 0, 0), Holdenville = c(-0.04, 0.12,
0.26, 0.05, -0.16, 0.23, 0.04, 0.01, 0.27, 0.13, 0.12, 0.07
), Hooker = c(0.03, -0.03, 0.07, -0.08, -0.17, 0.01, -0.03,
-0.05, -0.14, -0.02, 0, 0.04), Jefferson = c(0.04, 0.05,
0.29, 0.09, 0.05, 0.21, 0.11, 0.07, -0.03, 0.05, 0.09, 0.08
), Kenton = c(0.02, -0.06, 0.05, -0.12, -0.15, 0, 0.27, 0.17,
-0.01, -0.04, -0.02, -0.01), Kingfisher = c(0.05, 0.09, 0.18,
-0.02, 0.19, 0.21, -0.03, 0.19, 0.1, -0.01, 0.02, 0.11),
Lawton = c(0.03, 0.06, 0.06, -0.17, -0.39, 0.11, -0.1, 0.06,
0, 0.06, 0.03, 0.03), Mangum = c(0.01, 0, 0.05, -0.31, -0.27,
0.13, 0.01, -0.01, -0.01, 0, 0, 0.01), Meeker = c(-0.03,
0.14, 0.22, -0.15, -0.03, 0.34, 0.05, -0.03, 0.22, 0.14,
0.02, 0.06), Miami = c(-0.03, 0.03, 0.17, 0.12, 0.15, -0.11,
-0.15, -0.29, 0.34, 0.11, 0.25, 0.09), Muskogee = c(0.08,
0.12, 0.14, -0.04, 0.27, 0.16, -0.09, -0.07, 0.36, -0.02,
0.23, 0.14), Mutual = c(0.04, 0.05, 0.16, -0.05, 0.2, 0.16,
0.06, -0.04, -0.1, 0.02, 0, 0.08), Newkirk = c(-0.04, 0.06,
0.19, 0.13, -0.09, 0.15, 0.24, -0.01, -0.1, 0.21, 0, 0.1),
Okeene = c(0.09, 0.19, 0.12, 0.06, 0.02, 0.41, 0.03, 0.03,
0.37, 0.08, 0.13, 0.17), Okemah = c(0, 0.04, 0.2, -0.08,
0.04, 0.12, 0.05, 0.04, -0.01, 0.1, 0, 0.06), Okmulgee = c(0.04,
0.21, 0.17, -0.02, 0.01, 0.23, 0.03, 0.06, 0.17, 0, 0.29,
0.1), Pauls_valley = c(0.17, 0.17, 0.36, -0.02, -0.11, 0.27,
-0.12, 0.06, 0.27, 0.22, 0.13, 0.21), Pawhuska = c(0.05,
0.11, 0.3, 0.29, 0.19, 0.13, 0.2, -0.02, 0.1, 0.12, 0.2,
0.15), Perry = c(0.04, 0.13, 0.25, -0.15, -0.06, 0.02, 0.2,
-0.15, -0.05, 0, -0.04, 0.07), Poteau = c(-0.03, 0.05, 0.21,
-0.11, -0.01, -0.07, -0.11, -0.15, 0.37, 0.39, 0.25, 0.2),
Stillwater = c(0.04, 0.12, 0.16, -0.2, -0.04, 0.2, 0.1, -0.01,
0.02, 0, 0, 0.05), Tahlequah = c(0.09, 0.17, 0.27, -0.02,
0.48, 0.06, 0.28, 0.05, 0.38, 0.11, 0.4, 0.21), Waurika = c(0.07,
0.04, 0.16, -0.09, -0.12, 0.08, -0.22, -0.01, 0.09, 0.09,
-0.03, 0.02), Weatherford = c(0.03, 0.05, 0.23, -0.24, -0.06,
0.25, -0.11, 0.3, -0.1, 0.04, -0.03, 0.01), Webbersfalls = c(-0.04,
0.1, 0.16, -0.09, 0.09, -0.01, -0.25, -0.18, 0.39, 0.01,
0.3, 0.11)), .Names = c("Month", "Ada", "Altus", "Antlers",
"Ardmore", "Bartlesville", "Beaver", "Boise.City", "Buffalo",
"Carnegie", "Cherokee", "Claremore", "Durant", "Enid", "Erick",
"Geary", "Goodwell", "Guthrie", "Hammon", "Hennessey", "Hobartmuni",
"Holdenville", "Hooker", "Jefferson", "Kenton", "Kingfisher",
"Lawton", "Mangum", "Meeker", "Miami", "Muskogee", "Mutual",
"Newkirk", "Okeene", "Okemah", "Okmulgee", "Pauls_valley", "Pawhuska",
"Perry", "Poteau", "Stillwater", "Tahlequah", "Waurika", "Weatherford",
"Webbersfalls"), class = "data.frame", row.names = c(NA, -12L
))
coordinates for each place to map
coords <- structure(list(place = structure(c(2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L,
22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L,
35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 1L, 44L), .Label = c(" Weatherford",
"Ada", "Altus", "Antlers", "Ardmore", "Bartlesville", "Beaver",
"Boise City", "Buffalo", "Carnegie", "Cherokee", "Claremore",
"Durant", "Enid", "Erick", "Geary", "Goodwell", "Guthrie", "Hammon",
"Hennessey", "Hobartmuni", "Holdenville", "Hooker", "Jefferson",
"Kenton", "Kingfisher", "Lawton", "Mangum", "Meeker", "Miami",
"Muskogee", "Mutual", "Newkirk", "Okeene", "Okemah", "Okmulgee",
"Pauls_valley", "Pawhuska", "Perry", "Poteau", "Stillwater",
"Tahlequah", "Waurika", "Webbersfalls"), class = "factor"), Lat = c(34.7864,
34.5903, 34.2208, 34.1714, 36.7683, 36.8125, 36.7236, 36.8003,
35.1756, 36.7747, 36.3225, 34.0003, 36.4194, 35.2164, 35.6267,
36.5914, 35.8161, 35.585, 36.0942, 34.9894, 35.0567, 36.8589,
36.7222, 36.9031, 35.8583, 34.6097, 34.8911, 35.505, 36.8833,
35.7781, 36.2283, 36.8914, 36.1217, 35.4253, 35.6239, 34.7253,
36.6692, 36.2886, 35.0539, 36.1175, 35.9369, 34.1747, 35.52,
35.4814), Long = c(-96.685, -99.3344, -95.615, -97.1294, -96.0261,
-100.5308, -102.4806, -99.6403, -98.5794, -98.3583, -95.5808,
-96.3686, -97.8747, -99.8628, -98.3225, -101.6181, -97.395, -99.3953,
-97.835, -99.0525, -96.3861, -101.2172, -97.7903, -102.965, -97.9294,
-98.4572, -99.5017, -96.9767, -94.8833, -95.3339, -99.17, -97.0586,
-98.315, -96.3033, -96.025, -97.2814, -96.3472, -97.2897, -94.6264,
-97.095, -94.9644, -97.9964, -98.6986, -95.2039)), .Names = c("place",
"Lat", "Long"), class = "data.frame", row.names = c(NA, -44L))
So basically I want to take the values, find out which one is positive and which one is negative and plot a figure like below.
There will be three different tickers for +,- and zero
12 facets for 12 months
I have the lat/longs for the 44 places, so I can plot them on the state map.
How can I do this using ggplot2? or some other utility.
Till now, I have plotted the points using the following code
ggplot() +
geom_polygon(data=m, aes(x=long, y=lat,group=group),colour="black", fill="white" )+
geom_point(data=stations,aes(x=long,y=lat),,colour="red",)+
size=0.5,hjust=0,alpha=.5),size=3)+
xlab('Longitude')+
ylab('Latitude')+
coord_fixed()

Try this
Load lackages, download when necessary
kpacks <- c('raster', 'ggplot2', 'reshape2', 'lubridate')
new.packs <- kpacks[!(kpacks %in% installed.packages()[,"Package"])]
if(length(new.packs)) install.packages(new.packs)
lapply(kpacks, require, character.only=T)
remove(kpacks, new.packs)
d <- read.table(text=readClipboard(), sep = '\t', header=TRUE)
# or copy and paste the data from above
coords <- read.table(text=readClipboard(), sep = '\t', header=TRUE, quote = '')
# or copy and paste the data from above
#coordinates(coords) <- ~Long+Lat #not necessary for this approach
Get data from GADM
usa <- raster::getData(country = 'USA', level = 1)
okl <- usa[usa#data$NAME_1 == 'Oklahoma', ]
#plot(okl)
#plot(coords, add = T, cols = d)
d1 <- melt(d) # reshape it
d1$Month <- lubridate::dmy(d1$Month) # I've used abrev names in Portuguese. Change accordingly
create a factor variable with levels = positive/zero/negative
d1$val <- cut(d1$value, breaks= c(min(d1$value), 0.00, 0.001, max(d1$value)),
labels = c('negative', 'zero', 'positive'),
right = F, include.lowest = T, dig.lab = 3)
d2 <- merge(d1, coords, by.x = 'variable', by.y = 'place', all.x = T)
head(d2)
variable Month value val Lat Long
1 Ada Jul 0.02 positive 34.7864 -96.685
2 Ada May -0.15 negative 34.7864 -96.685
3 Ada Jun 0.30 positive 34.7864 -96.685
4 Ada Jan 0.10 positive 34.7864 -96.685
5 Ada Feb 0.14 positive 34.7864 -96.685
6 Ada Mar 0.25 positive 34.7864 -96.685
okl_df <- fortify(okl) # spdf to data.frame
head(okl_df)
long lat order hole piece group id
1 -95.52363 37.00093 1 FALSE 1 36.1 36
2 -95.40672 37.00047 2 FALSE 1 36.1 36
3 -95.40027 37.00053 3 FALSE 1 36.1 36
4 -95.07227 36.99872 4 FALSE 1 36.1 36
5 -95.03362 36.99859 5 FALSE 1 36.1 36
6 -95.03309 36.99920 6 FALSE 1 36.1 36
p <- ggplot(data = okl_df, aes(x = long, y = lat, group = group)) +
geom_polygon(fill = NA, colour = 'black') +
geom_point(inherit.aes = F, data = d2, aes(x=Long, y = Lat, colour = val)) +
facet_wrap(~ Month, ncol = 3) +
theme_minimal() +
coord_map()+
scale_colour_manual('class', values = c('negative'= 'grey80', 'zero' = 'grey60',
'positive' = 'black'))
p
EDIT
For mapping the variable to shapes instead of colours one can map it to aesthetics
p1 <- ggplot(data = okl_df, aes(x = long, y = lat, group = group)) +
geom_polygon(fill = NA, colour = 'black') +
geom_point(inherit.aes = F, data = d2, aes(x=Long, y = Lat, shape = val)) +
facet_wrap(~ Month, ncol = 3) +
theme_minimal() +
coord_map()
p1
Or map both colour and shape to aesthetics
p2 <- ggplot(data = okl_df, aes(x = long, y = lat, group = group)) +
geom_polygon(fill = NA, colour = 'black') +
geom_point(inherit.aes = F, data = d2, aes(x=Long, y = Lat, colour = val,
shape = val)) +
facet_wrap(~ Month, ncol = 3) +
theme_minimal() +
coord_map()
p2
You can export it as png, manipulating resolution was needed
ggsave(file = file.path(tempdir(),'map1.png'),
p
, width=16, height=16, units = "cm", dpi = 150
)

Add facet_wrap(~ month) or facet_grid(month ~ .) at the end of your plotting code. You can also specify the number of columns in facet_wrap: facet_wrap(~ month, ncol = 3)
I've rearranged your data:
transposed the data
named the first column place
The resulting dataframe:
oklahoma <- structure(list(place = structure(1:44, .Label = c("Ada", "Altus", "Antlers", "Ardmore", "Bartlesville", "Beaver", "Boise City", "Buffalo", "Carnegie", "Cherokee", "Claremore", "Durant", "Enid", "Erick", "Geary", "Goodwell", "Guthrie", "Hammon", "Hennessey", "Hobartmuni", "Holdenville", "Hooker", "Jefferson", "Kenton", "Kingfisher", "Lawton", "Mangum", "Meeker", "Miami", "Muskogee", "Mutual", "Newkirk", "Okeene", "Okemah", "Okmulgee", "Pauls valley", "Pawhuska", "Perry", "Poteau", "Stillwater", "Tahlequah", "Waurika", "Weatherford", "Webbersfalls"), class = "factor"), Jan = structure(c(16L, 12L, 5L, 1L, 2L, 7L, 8L, 4L, 8L, 12L, 2L, 12L, 16L, 11L, 1L, 6L, 12L, 8L, 8L, 3L, 4L, 9L, 10L, 8L, 11L, 9L, 7L, 3L, 3L, 14L, 10L, 4L, 15L, 6L, 10L, 17L, 11L, 10L, 3L, 10L, 15L, 13L, 9L, 4L), .Label = c("-0,01", "-0,02", "-0,03", "-0,04", "-0,08", "0", "0,01", "0,02", "0,03", "0,04", "0,05", "0,06", "0,07", "0,08", "0,09", "0,1", "0,17"), class = "factor"), Feb = structure(c(17L, 10L, 7L, 12L, 12L, 1L, 3L, 5L, 11L, 13L, 21L, 10L, 13L, 11L, 6L, 3L, 16L, 8L, 12L, 1L, 15L, 2L, 10L, 4L, 12L, 11L, 5L, 17L, 8L, 15L, 10L, 11L, 19L, 9L, 20L, 18L, 14L, 16L, 10L, 15L, 18L, 9L, 10L, 13L), .Label = c("-0,02", "-0,03", "-0,05", "-0,06", "0", "0,01", "0,02", "0,03", "0,04", "0,05", "0,06", "0,09", "0,1", "0,11", "0,12", "0,13", "0,14", "0,17", "0,19", "0,21", "0,22"), class = "factor"), Mar = structure(c(18L, 6L, 6L, 12L, 14L, 10L, 5L, 9L, 9L, 19L, 12L, 9L, 22L, 8L, 9L, 4L, 17L, 8L, 16L, 6L, 19L, 3L, 21L, 1L, 12L, 2L, 1L, 16L, 11L, 8L, 10L, 13L, 7L, 14L, 11L, 23L, 22L, 18L, 15L, 10L, 20L, 10L, 17L, 10L), .Label = c("0,05", "0,06", "0,07", "0,08", "0,09", "0,1", "0,12", "0,14", "0,15", "0,16", "0,17", "0,18", "0,19", "0,2", "0,21", "0,22", "0,23", "0,25", "0,26", "0,27", "0,29", "0,3", "0,36"), class = "factor"), Apr = structure(c(4L, 14L, 17L, 15L, 24L, 5L, 7L, 3L, 11L, 7L, 22L, 8L, 18L, 14L, 13L, 5L, 6L, 14L, 2L, 12L, 19L, 5L, 21L, 9L, 1L, 12L, 16L, 10L, 22L, 2L, 3L, 23L, 20L, 5L, 1L, 1L, 25L, 14L, 8L, 14L, 1L, 6L, 15L, 6L), .Label = c("-0,02", "-0,04", "-0,05", "-0,06", "-0,08", "-0,09", "-0,1", "-0,11", "-0,12", "-0,15", "-0,16", "-0,17", "-0,19", "-0,2", "-0,24", "-0,31", "-0,38", "0", "0,05", "0,06", "0,09", "0,12", "0,13", "0,16", "0,29"), class = "factor"), May = structure(c(10L, 18L, 16L, 20L, 5L, 3L, 13L, 10L, 24L, 4L, 6L, 9L, 28L, 7L, 6L, 1L, 23L, 3L, 25L, 15L, 11L, 12L, 22L, 10L, 27L, 17L, 14L, 2L, 26L, 29L, 28L, 6L, 20L, 21L, 19L, 8L, 27L, 7L, 1L, 3L, 30L, 9L, 4L, 24L), .Label = c("-0,01", "-0,03", "-0,04", "-0,06", "-0,07", "-0,09", "-0,1", "-0,11", "-0,12", "-0,15", "-0,16", "-0,17", "-0,24", "-0,27", "-0,29", "-0,3", "-0,39", "0", "0,01", "0,02", "0,04", "0,05", "0,06", "0,09", "0,1", "0,15", "0,19", "0,2", "0,27", "0,48"), class = "factor"), Jun = structure(c(26L, 24L, 25L, 20L, 11L, 10L, 8L, 18L, 23L, 19L, 13L, 29L, 26L, 10L, 16L, 5L, 27L, 12L, 21L, 3L, 22L, 6L, 20L, 5L, 20L, 13L, 15L, 28L, 4L, 18L, 18L, 17L, 30L, 14L, 22L, 25L, 15L, 7L, 2L, 19L, 9L, 11L, 24L, 1L), .Label = c("-0,01", "-0,07", "-0,08", "-0,11", "0", "0,01", "0,02", "0,05", "0,06", "0,07", "0,08", "0,1", "0,11", "0,12", "0,13", "0,14", "0,15", "0,16", "0,2", "0,21", "0,22", "0,23", "0,24", "0,25", "0,27", "0,3", "0,31", "0,34", "0,35", "0,41"), class = "factor"), Jul = structure(c(17L, 15L, 10L, 8L, 16L, 22L, 1L, 6L, 2L, 21L, 26L, 12L, 23L, 5L, 11L, 3L, 2L, 5L, 25L, 17L, 19L, 2L, 24L, 29L, 2L, 5L, 16L, 20L, 9L, 4L, 21L, 28L, 18L, 20L, 18L, 7L, 27L, 27L, 6L, 23L, 30L, 13L, 6L, 14L), .Label = c("-0,01", "-0,03", "-0,06", "-0,09", "-0,1", "-0,11", "-0,12", "-0,13", "-0,15", "-0,17", "-0,18", "-0,21", "-0,22", "-0,25", "0", "0,01", "0,02", "0,03", "0,04", "0,05", "0,06", "0,07", "0,1", "0,11", "0,13", "0,16", "0,2", "0,24", "0,27", "0,28"), class = "factor"), Aug = structure(c(1L, 20L, 8L, 6L, 16L, 19L, 29L, 23L, 21L, 6L, 16L, 13L, 22L, 24L, 25L, 15L, 17L, 30L, 28L, 16L, 14L, 5L, 19L, 26L, 27L, 18L, 1L, 3L, 12L, 7L, 4L, 1L, 15L, 16L, 18L, 18L, 2L, 11L, 9L, 1L, 17L, 1L, 31L, 10L), .Label = c("-0,01", "-0,02", "-0,03", "-0,04", "-0,05", "-0,06", "-0,07", "-0,1", "-0,15", "-0,18", "-0,2", "-0,29", "0", "0,01", "0,03", "0,04", "0,05", "0,06", "0,07", "0,08", "0,09", "0,1", "0,12", "0,13", "0,14", "0,17", "0,19", "0,2", "0,21", "0,23", "0,3"), class = "factor"), Sep = structure(c(24L, 11L, 17L, 19L, 1L, 7L, 1L, 10L, 9L, 4L, 28L, 27L, 11L, 12L, 13L, 7L, 1L, 5L, 14L, 3L, 21L, 8L, 2L, 1L, 16L, 11L, 1L, 20L, 22L, 23L, 6L, 6L, 24L, 1L, 18L, 21L, 16L, 6L, 24L, 13L, 25L, 15L, 6L, 26L), .Label = c("-0,01", "-0,03", "-0,05", "-0,06", "-0,07", "-0,1", "-0,12", "-0,14", "-0,16", "-0,2", "0", "0,01", "0,02", "0,08", "0,09", "0,1", "0,11", "0,17", "0,18", "0,22", "0,27", "0,34", "0,36", "0,37", "0,38", "0,39", "0,4", "0,46"), class = "factor"), Oct = structure(c(14L, 9L, 17L, 8L, 17L, 12L, 5L, 10L, 13L, 6L, 21L, 11L, 22L, 7L, 12L, 9L, 10L, 12L, 1L, 3L, 19L, 2L, 12L, 4L, 1L, 13L, 7L, 20L, 17L, 2L, 9L, 23L, 14L, 16L, 7L, 24L, 18L, 7L, 25L, 7L, 17L, 15L, 11L, 8L), .Label = c("-0,01", "-0,02", "-0,03", "-0,04", "-0,06", "-0,11", "0", "0,01", "0,02", "0,03", "0,04", "0,05", "0,06", "0,08", "0,09", "0,1", "0,11", "0,12", "0,13", "0,14", "0,16", "0,2", "0,21", "0,22", "0,39"), class = "factor"), Nov = structure(c(15L, 6L, 14L, 2L, 11L, 5L, 5L, 5L, 1L, 5L, 18L, 19L, 10L, 8L, 4L, 3L, 9L, 7L, 7L, 5L, 12L, 5L, 9L, 2L, 6L, 7L, 5L, 6L, 18L, 17L, 5L, 5L, 13L, 5L, 20L, 13L, 16L, 5L, 18L, 5L, 22L, 3L, 3L, 21L), .Label = c("-0,01", "-0,02", "-0,03", "-0,07", "0", "0,02", "0,03", "0,04", "0,09", "0,1", "0,11", "0,12", "0,13", "0,14", "0,15", "0,2", "0,23", "0,25", "0,26", "0,29", "0,3", "0,4"), class = "factor"), Dec = structure(c(16L, 4L, 2L, 10L, 15L, 7L, 5L, 3L, 2L, 10L, 16L, 11L, 12L, 4L, 2L, 2L, 13L, 5L, 9L, 2L, 9L, 6L, 10L, 1L, 13L, 5L, 3L, 8L, 11L, 14L, 10L, 12L, 16L, 8L, 12L, 18L, 15L, 9L, 17L, 7L, 18L, 4L, 3L, 13L), .Label = c("-0,01", "0", "0,01", "0,02", "0,03", "0,04", "0,05", "0,06", "0,07", "0,08", "0,09", "0,1", "0,11", "0,14", "0,15", "0,17", "0,2", "0,21"), class = "factor"), Lat = structure(c(8L, 5L, 4L, 2L, 37L, 40L, 36L, 39L, 13L, 38L, 32L, 1L, 26L, 14L, 21L, 33L, 23L, 19L, 27L, 10L, 12L, 41L, 35L, 44L, 24L, 6L, 9L, 17L, 42L, 22L, 30L, 43L, 29L, 15L, 20L, 7L, 34L, 31L, 11L, 28L, 25L, 3L, 18L, 16L), .Label = c("34", "34,1714", "34,1747", "34,221", "34,6", "34,61", "34,7253", "34,786", "34,8911", "34,9894", "35,054", "35,0567", "35,1756", "35,2", "35,4253", "35,4814", "35,505", "35,52", "35,585", "35,6239", "35,63", "35,7781", "35,816", "35,8583", "35,9369", "36", "36,0942", "36,1175", "36,122", "36,228", "36,3", "36,3225", "36,5914", "36,6692", "36,7222", "36,7236", "36,7683", "36,7747", "36,8", "36,813", "36,859", "36,88", "36,8914", "36,903"), class = "factor"), Long = structure(c(20L, 39L, 13L, 25L, 15L, 2L, 5L, 44L, 37L, 35L, 12L, 18L, 32L, 1L, 33L, 4L, 27L, 42L, 29L, 40L, 19L, 3L, 28L, 6L, 30L, 36L, 43L, 21L, 8L, 11L, 41L, 23L, 34L, 16L, 14L, 26L, 17L, 22L, 7L, 24L, 9L, 31L, 38L, 10L), .Label = c("-100", "-100,5", "-101,2", "-101,618", "-102,481", "-103", "-94,63", "-94,9", "-94,9644", "-95,2039", "-95,3339", "-95,5808", "-95,62", "-96,025", "-96,0261", "-96,303", "-96,3472", "-96,37", "-96,3861", "-96,69", "-96,98", "-97", "-97,059", "-97,095", "-97,129", "-97,2814", "-97,4", "-97,7903", "-97,835", "-97,9294", "-97,996", "-98", "-98,3", "-98,32", "-98,3583", "-98,46", "-98,579", "-98,6986", "-99", "-99,0525", "-99,17", "-99,3953", "-99,502", "-99,64"), class = "factor")), .Names = c("place", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Lat", "Long"), class = "data.frame", row.names = c(NA, -44L))
After that reshape the dataframe:
require(reshape2)
stations <- melt(oklahoma, id=c("place","Lat","Long"))
stations$value <- gsub(',', '.', stations$value)
stations$value <- as.numeric(stations$value)
The create the plot:
ggplot() +
# geom_polygon(data=m, aes(x=Long, y=Lat,group=group), colour="black", fill="white") +
geom_point(data=stations, aes(x=Long,y=Lat), colour="red")+
xlab('Longitude') +
ylab('Latitude') +
coord_fixed() +
facet_wrap(~ variable, ncol = 3)
As you can see, I commented out the geom_polygon as I don't have that data.

Related

Aesthethics do not work for individual geom_line's in R's ggplot

I am trying to construct a plot with thin, transparent individual lines for individual people (indicated by the id column) and place a thick, solid average-across-people line on top of that.
My ggplot call looks like this:
ggplot(data, aes(x = card_self, y = percent)) +
geom_line(aes(group=id, color=id, size=1, alpha=0.8)) +
geom_line(data = data_averaged, aes(size=10, alpha=0.9)) +
guides(color='none', alpha='none', size='none')
The result is this:
The problem is that changing the size/alpha aesthetics does not work as expected. I've tried setting them outside the aes argument and inside and the results are as random as it gets.
My full code to reproduce the plot is here:
library("tidyverse")
library("ggplot")
data <- structure(list(id = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 17L, 17L, 17L,
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 19L,
19L, 19L, 19L, 19L, 19L, 19L, 19L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L,
22L, 22L, 22L, 22L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L,
23L, 23L, 23L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L,
24L, 24L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L,
25L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L, 26L,
27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 28L,
28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 29L, 29L,
29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 30L, 30L, 30L,
30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L), .Label = c("P001",
"P002", "P003", "P004", "P005", "P006", "P007", "P008", "P009",
"P010", "P011", "P012", "P013", "P014", "P015", "P016", "P017",
"P018", "P019", "P020", "P021", "P022", "P023", "P024", "P025",
"P026", "P027", "P028", "P029", "P030"), class = "factor"), card_self = structure(c(1L,
2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L,
5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L,
9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L,
11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L,
13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 1L,
2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L,
5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L,
9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L,
11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L,
13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 1L,
2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L,
5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L,
9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L,
11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L,
13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 1L,
2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L,
5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L,
9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L,
11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L,
13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 1L,
2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L,
5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L,
9L, 10L, 11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L,
11L, 12L, 13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L,
13L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 13L), .Label = c("ace",
"two", "three", "four", "five", "six", "seven", "eight", "nine",
"ten", "jack", "queen", "king"), class = "factor"), percent = c(1,
1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0.89, 1, 0.67, 0, 0,
0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
0.67, 0.22, 0.11, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0.89, 0, 0, 0,
0, 0, 1, 1, 0.89, 1, 1, 0.89, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0.89,
0.56, 0, 0.22, 0, 0, 0, 0, 0.56, 0.89, 0.67, 0.89, 0.67, 0.89,
0.56, 0.67, 0.44, 0.89, 0.56, 0.56, 1, 1, 1, 1, 0.44, 0.56, 0.11,
0.22, 0.22, 0, 0, 0, 1, 1, 1, 1, 0.44, 0.33, 0.67, 0.33, 0.11,
0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0.89, 1, 0.89,
0.78, 0.22, 0.56, 0.33, 0.22, 0.11, 0, 1, 1, 1, 1, 0.44, 0.11,
0, 0, 0, 0, 0, 0, 1, 0.78, 0.89, 0.56, 0.67, 0.67, 0.44, 0.44,
0.44, 0.67, 0.22, 0, 1, 1, 1, 1, 1, 0.78, 0.44, 0, 0.78, 0.44,
0, 0, 1, 1, 1, 1, 1, 1, 0.11, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
0, 0.22, 0, 0, 0, 0, 0.67, 0.44, 0.33, 0.56, 0.11, 0.33, 0.44,
0.56, 0.44, 0.33, 0.22, 0.33, 1, 1, 1, 1, 1, 1, 0.11, 0.11, 0.11,
0, 0, 0, 1, 1, 1, 1, 1, 0.67, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
0.44, 0, 0.33, 0.22, 0, 0, 0, 1, 0.56, 0.67, 0.89, 0.89, 0.78,
0.67, 0.44, 0.33, 0, 0.33, 0, 1, 1, 0.89, 0.78, 0.67, 0.44, 0.33,
0, 0.22, 0, 0, 0, 1, 1, 1, 1, 1, 0.78, 0, 0, 0, 0, 0, 0, 1, 1,
1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0.67, 0.44, 0.56, 0.22, 0.33, 0.44,
0.56, 0.56, 0.22, 0.44, 0.44, 0.33, 1, 1, 1, 1, 1, 1, 0.56, 0.11,
0, 0, 0, 0, 1, 1, 1, 1, 0.44, 0.44, 0.33, 0, 0, 0, 0, 0, 1, 1,
0.89, 0.67, 0.33, 0.33, 0.56, 0.22, 0.11, 0.11, 0, 0, 1, 1, 1,
1, 1, 0.78, 0.22, 0, 0, 0, 0, 0)), row.names = c(NA, -360L), groups = structure(list(
id = structure(1:30, .Label = c("P001", "P002", "P003", "P004",
"P005", "P006", "P007", "P008", "P009", "P010", "P011", "P012",
"P013", "P014", "P015", "P016", "P017", "P018", "P019", "P020",
"P021", "P022", "P023", "P024", "P025", "P026", "P027", "P028",
"P029", "P030"), class = "factor"), .rows = structure(list(
1:12, 13:24, 25:36, 37:48, 49:60, 61:72, 73:84, 85:96,
97:108, 109:120, 121:132, 133:144, 145:156, 157:168,
169:180, 181:192, 193:204, 205:216, 217:228, 229:240,
241:252, 253:264, 265:276, 277:288, 289:300, 301:312,
313:324, 325:336, 337:348, 349:360), 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"))
data_averaged <- data %>%
group_by(card_self) %>%
summarise(percent = mean(percent))
ggplot(data, aes(x = card_self, y = percent)) +
geom_line(aes(group=id, color=id, size=1, alpha=0.8)) +
geom_line(data = data_averaged, aes(size=10, alpha=0.9)) +
guides(color='none', alpha='none', size='none')
Update: Important comment from teunbrand:
"In addition, I'd like to add that the reason the group aesthetic should be added is because discrete x-axes automatically group observations"
Just add group=1 to ggplot()
The issue is that the second geom_line is not grouped. The data points must be grouped to connect correctly.
ggplot(data, aes(x = card_self, y = percent, group=1)) +
geom_line(aes(group=id, color=id, size=1, alpha=0.8)) +
geom_line(data = data_averaged, aes(size=10, alpha=0.9)) +
guides(color='none', alpha='none', size='none')
# also works:
ggplot(data, aes(x = card_self, y = percent)) +
geom_line(aes(group=id, color=id, size=1, alpha=0.8)) +
geom_line(data = data_averaged, aes(size=10, alpha=0.9, group=1)) +
guides(color='none', alpha='none', size='none')

R ggplot2 line graph adding fill when I don't want it

I've got a dataset with some velocity data. I have an extra column in this dataset called followtime that corresponds to certain values in velocity, and highlights them as a factor (ie, certain values in trial 1 will be highlighted in followtime with 1 and the rest 0, certain values in trial 2 will be highlighted with 2 and the rest 0, etc.). See below (this example has random velocities, but followtime is looks pretty much like it does in my own dataset).
trial <- c(rep(1,25), rep(2, 25), rep(3, 25))
minitime <- c(1:25)
time <- c(rep(minitime, 3))
totalsmooth_velocity <- runif(75, min=-3, max=2)
followtime <- c(rep(0, 10), rep(1, 10), rep(0,5), rep(0, 5), rep(2, 5), rep(0, 15), rep(0, 15), rep(3, 10))
df <- cbind(trial, time, totalsmooth_velocity, followtime)
df <- as.data.frame(df)
df$time <- as.integer(df$time)
I'd like to make a line graph with ggplot2 that color-codes each followtime in a different color. Here's my data graphed with a scatter plot (which for some reason has no trouble with this):
With corresponding code:
stim1bfollows<- ggplot()+
geom_point(data=df, aes(x=time, y=totalsmooth_velocity, color = as.factor(followtime)), size = 1.0)+
geom_hline(yintercept=c(0, -0.16))
stim1bfollows
When I try to code this as a line graph, however, it looks like this:
With corresponding code:
stim1bfollows<- ggplot()+
geom_line(data=df, aes(x=time, y=totalsmooth_velocity, color = as.factor(followtime)), size = 1.0)+
geom_hline(yintercept=c(0, -0.16))
stim1bfollows
I don't want that fill! I'm not sure what's going wrong, I've tried a couple of changes with 'dodge' and treated color as both a factor and as numerically but if anybody could point me in the right direction I'd be extremely grateful. Thank you!
EDIT
Having this issue even with group! Image:
stim1bfollows<- ggplot()+
geom_line(data=follows, aes(x=time, y=totalsmooth_velocity, group = as.factor(followtime), color = as.factor(followtime)), size = 1.0)+
geom_hline(yintercept=c(0, -0.16))
stim1bfollows
Also for reference, my str(follows) of the original dataset:
str(follows)
'data.frame': 750 obs. of 13 variables:
$ bartrial : int 9 9 9 9 9 9 9 9 9 9 ...
$ trial : int 1 1 1 1 1 1 1 1 1 1 ...
$ time : int 17026 17027 17028 17029 17030 17031 17032 17033 17034 17035 ...
$ X : num 158 158 158 158 158 ...
$ Y : num 64.5 64.6 64.6 64.5 64.5 ...
$ velocity : num 0.05766 -0.0266 -0.05106 -0.00543 0.04506 ...
$ barvelocity : num -0.16 -0.16 -0.16 -0.16 -0.16 -0.16 -0.16 -0.16 -0.16 -0.16 ...
$ index : num -0.3604 0.1663 0.3191 0.0339 -0.2816 ...
$ veldiff : num 0.218 0.133 0.109 0.155 0.205 ...
$ direction : logi TRUE TRUE TRUE TRUE TRUE TRUE ...
$ response : int 0 1 1 1 0 0 0 0 1 0 ...
$ totalsmooth_velocity: num 0.0173 0.0185 0.0202 0.0233 0.0272 ...
$ followtime : num 0 0 0 0 0 0 0 0 0 0 ...
And the head:
head(follows)
bartrial trial time X Y velocity barvelocity index veldiff direction response
2001 9 1 17026 158.2507 64.52043 0.057657143 -0.16 -0.36035714 0.2176571 TRUE 0
2002 9 1 17027 158.1855 64.57809 -0.026600000 -0.16 0.16625000 0.1334000 TRUE 1
2003 9 1 17028 158.2674 64.55149 -0.051057143 -0.16 0.31910714 0.1089429 TRUE 1
2004 9 1 17029 158.2733 64.50043 -0.005428571 -0.16 0.03392857 0.1545714 TRUE 1
2005 9 1 17030 158.2763 64.49500 0.045057143 -0.16 -0.28160714 0.2050571 TRUE 0
2006 9 1 17031 158.2363 64.54006 0.028971429 -0.16 -0.18107143 0.1889714 TRUE 0
totalsmooth_velocity followtime
2001 0.01732903 0
2002 0.01852428 0
2003 0.02024635 0
2004 0.02326663 0
2005 0.02719260 0
2006 0.03045590 0
Adding a small subset:
dput(subset(follows, time %in% 17100:17130))
structure(list(bartrial = c(9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L), trial = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L), time = c(17100L,
17101L, 17102L, 17103L, 17104L, 17105L, 17106L, 17107L, 17108L,
17109L, 17110L, 17111L, 17112L, 17113L, 17114L, 17115L, 17116L,
17117L, 17118L, 17119L, 17120L, 17121L, 17122L, 17123L, 17124L,
17125L, 17126L, 17127L, 17128L, 17129L, 17130L, 17100L, 17101L,
17102L, 17103L, 17104L, 17105L, 17106L, 17107L, 17108L, 17109L,
17110L, 17111L, 17112L, 17113L, 17114L, 17115L, 17116L, 17117L,
17118L, 17119L, 17120L, 17121L, 17122L, 17123L, 17124L, 17125L,
17126L, 17127L, 17128L, 17129L, 17130L, 17100L, 17101L, 17102L,
17103L, 17104L, 17105L, 17106L, 17107L, 17108L, 17109L, 17110L,
17111L, 17112L, 17113L, 17114L, 17115L, 17116L, 17117L, 17118L,
17119L, 17120L, 17121L, 17122L, 17123L, 17124L, 17125L, 17126L,
17127L, 17128L, 17129L, 17130L), X = c(158.554971428571, 158.561857142857,
158.545942857143, 158.442742857143, 158.463457142857, 158.447628571429,
158.4628, 158.426028571429, 158.3998, 158.355114285714, 158.339971428571,
158.318457142857, 158.339657142857, 158.298142857143, 158.241714285714,
158.2846, 158.311114285714, 158.324142857143, 158.307228571429,
158.255428571429, 158.271914285714, 158.308028571429, 158.2894,
158.301342857143, 158.308428571429, 158.227228571429, 158.250057142857,
158.226771428571, 158.174914285714, 158.200485714286, 158.213085714286,
98.11471429, 99.706, 101.0531143, 102.1066286, 103.2292, 103.9274,
104.7769429, 105.7868571, 106.5872857, 107.3484286, 108.6168571,
109.3342286, 110.2153714, 111.2181714, 112.8689429, 114.7111143,
116.7568571, 118.523, 119.8732857, 121.4064, 122.5118286, 123.6406286,
124.6844, 125.5278286, 126.3410286, 128.1753143, 129.8935429,
131.1022857, 132.2688286, 133.3624571, 133.9324286, 96.6617714285714,
96.6587142857143, 96.1991428571429, 94.7394285714286, 92.9670285714286,
90.7313428571429, 88.4762, 85.5486571428571, 82.0275142857143,
79.3814, 77.4777428571429, 75.5628857142857, 72.7176, 69.8095142857143,
66.4666571428571, 61.6254, 57.966, 54.3682571428571, 51.6569428571429,
50.0204, 48.7530285714286, 47.9830571428571, 46.7885142857143,
45.3995428571429, 43.5878, 41.7556285714286, 39.6544571428571,
37.6700285714286, 35.0251714285714, 32.6265714285714, 29.3777142857143
), Y = c(57.2500571428571, 57.0420857142857, 56.7889714285714,
56.6496857142857, 56.5398571428571, 56.4711714285714, 56.2698285714286,
56.1793428571429, 56.0277714285714, 56.0177142857143, 55.7782857142857,
55.6382, 55.7419714285714, 55.6086857142857, 55.5973142857143,
55.6697428571429, 55.5047714285714, 55.4622571428571, 55.1674285714286,
55.1252857142857, 54.8758857142857, 54.8422571428571, 54.7050857142857,
54.6246571428571, 54.6242285714286, 54.5318, 54.3272285714286,
54.1329428571429, 54.1315428571429, 54.0057142857143, 53.9648857142857,
189.6266286, 189.5072571, 189.4862571, 189.3138, 189.2692571,
189.2216857, 189.1862857, 189.1338, 189.0529143, 188.9495429,
188.8440286, 188.7655143, 188.7224286, 188.5904857, 188.5451429,
188.5732286, 188.3531429, 188.1283429, 187.9746571, 187.5346,
187.2002857, 186.9196571, 186.6167429, 186.5036857, 186.2668,
185.873, 185.4187714, 185.1869429, 184.8553429, 184.6551429,
184.5514857, 10.31026, 10.3998028571429, 9.98941714285714, 9.93686571428571,
10.0085457142857, 10.1963542857143, 10.3918028571429, 10.4927542857143,
10.7132285714286, 10.9808628571429, 11.2396571428571, 11.2958885714286,
11.4141742857143, 11.5568771428571, 11.7057142857143, 12.0326514285714,
12.2709742857143, 12.49016, 12.7785485714286, 13.0098257142857,
13.2676714285714, 13.3456028571429, 13.3960371428571, 13.4590771428571,
13.46362, 13.5725371428571, 13.756, 13.8612657142857, 13.9868371428571,
14.1618342857143, 14.4414857142857), velocity = c(-0.207971428571433,
-0.253114285714283, -0.139285714285712, -0.109828571428572, -0.0686857142857136,
-0.201342857142862, -0.0904857142857125, -0.15157142857143, -0.0100571428571357,
-0.239428571428583, -0.140085714285711, 0.103771428571434, -0.133285714285712,
-0.0113714285714366, 0.0724285714285742, -0.164971428571434,
-0.0425142857142831, -0.294828571428567, -0.0421428571428564,
-0.249400000000009, -0.0336285714285651, -0.137171428571428,
-0.0804285714285768, -0.000428571428571445, -0.0924285714285702,
-0.204571428571427, -0.194285714285712, -0.00140000000000384,
-0.125828571428563, -0.0408285714285697, -0.104885714285722,
-0.119371429, -0.021, -0.172457143, -0.044542857, -0.047571429,
-0.0354, -0.052485714, -0.080885714, -0.103371429, -0.105514286,
-0.078514286, -0.043085714, -0.131942857, -0.045342857, 0.028085714,
-0.220085714, -0.2248, -0.153685714, -0.440057143, -0.334314286,
-0.280628571, -0.302914286, -0.113057143, -0.236885714, -0.3938,
-0.454228571, -0.231828571, -0.3316, -0.2002, -0.103657143, -0.136857143,
0.0895428571428578, -0.410385714285713, -0.0525514285714301,
0.0716800000000006, 0.187808571428572, 0.195448571428571, 0.100951428571427,
0.220474285714285, 0.267634285714285, 0.258794285714286, 0.0562314285714294,
0.118285714285713, 0.142702857142858, 0.148837142857142, 0.326937142857144,
0.238322857142858, 0.219185714285715, 0.28838857142857, 0.231277142857143,
0.257845714285715, 0.0779314285714268, 0.050434285714287, 0.0630399999999991,
0.00454285714285874, 0.108917142857143, 0.183462857142857, 0.105265714285713,
0.12557142857143, 0.174997142857144, 0.279651428571428, 0.17196
), barvelocity = c(-0.16, -0.16, -0.16, -0.16, -0.16, -0.16,
-0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16,
-0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16,
-0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16,
-0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16,
-0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16,
-0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16,
-0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16,
-0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16,
-0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16, -0.16,
-0.16, -0.16, -0.16, -0.16, -0.16, -0.16), index = c(1.29982142857146,
1.58196428571427, 0.870535714285703, 0.686428571428577, 0.42928571428571,
1.25839285714289, 0.565535714285703, 0.947321428571435, 0.0628571428570979,
1.49642857142864, 0.875535714285691, -0.648571428571465, 0.833035714285701,
0.0710714285714786, -0.452678571428589, 1.03107142857146, 0.265714285714269,
1.84267857142855, 0.263392857142852, 1.55875000000005, 0.210178571428532,
0.857321428571423, 0.502678571428605, 0.00267857142857153, 0.577678571428564,
1.27857142857142, 1.2142857142857, 0.00875000000002402, 0.786428571428521,
0.25517857142856, 0.65553571428576, 0.746071429, 0.13125, 1.077857143,
0.278392857, 0.297321429, 0.22125, 0.328035714, 0.505535714,
0.646071429, 0.659464286, 0.490714286, 0.269285714, 0.824642857,
0.283392857, -0.175535714, 1.375535714, 1.405, 0.960535714, 2.750357143,
2.089464286, 1.753928571, 1.893214286, 0.706607143, 1.480535714,
2.46125, 2.838928571, 1.448928571, 2.0725, 1.25125, 0.647857143,
0.855357143, -0.559642857142861, 2.56491071428571, 0.328446428571438,
-0.448000000000004, -1.17380357142858, -1.22155357142857, -0.630946428571422,
-1.37796428571428, -1.67271428571428, -1.61746428571429, -0.351446428571434,
-0.739285714285709, -0.891892857142862, -0.930232142857135, -2.04335714285715,
-1.48951785714286, -1.36991071428572, -1.80242857142856, -1.44548214285715,
-1.61153571428572, -0.487071428571417, -0.315214285714294, -0.393999999999994,
-0.0283928571428671, -0.680732142857143, -1.14664285714285, -0.657910714285703,
-0.784821428571436, -1.09373214285715, -1.74782142857143, -1.07475
), veldiff = c(-0.0479714285714331, -0.0931142857142825, 0.0207142857142875,
0.0501714285714277, 0.0913142857142864, -0.0413428571428619,
0.0695142857142875, 0.00842857142857042, 0.149942857142864, -0.0794285714285829,
0.0199142857142894, 0.263771428571434, 0.0267142857142878, 0.148628571428563,
0.232428571428574, -0.00497142857143387, 0.117485714285717, -0.134828571428567,
0.117857142857144, -0.0894000000000085, 0.126371428571435, 0.0228285714285724,
0.0795714285714232, 0.159571428571429, 0.0675714285714298, -0.0445714285714268,
-0.0342857142857122, 0.158599999999996, 0.0341714285714366, 0.11917142857143,
0.0551142857142784, 0.040628571, 0.139, -0.012457143, 0.115457143,
0.112428571, 0.1246, 0.107514286, 0.079114286, 0.056628571, 0.054485714,
0.081485714, 0.116914286, 0.028057143, 0.114657143, 0.188085714,
-0.060085714, -0.0648, 0.006314286, -0.280057143, -0.174314286,
-0.120628571, -0.142914286, 0.046942857, -0.076885714, -0.2338,
-0.294228571, -0.071828571, -0.1716, -0.0402, 0.056342857, 0.023142857,
0.249542857142858, -0.250385714285713, 0.10744857142857, 0.231680000000001,
0.347808571428572, 0.355448571428571, 0.260951428571428, 0.380474285714285,
0.427634285714285, 0.418794285714286, 0.216231428571429, 0.278285714285713,
0.302702857142858, 0.308837142857142, 0.486937142857144, 0.398322857142858,
0.379185714285715, 0.44838857142857, 0.391277142857143, 0.417845714285715,
0.237931428571427, 0.210434285714287, 0.223039999999999, 0.164542857142859,
0.268917142857143, 0.343462857142857, 0.265265714285713, 0.28557142857143,
0.334997142857144, 0.439651428571428, 0.33196), direction = c(TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE), response = c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L), totalsmooth_velocity = c(-0.185715482616494,
-0.169799554542362, -0.159003977144789, -0.148207077837678, -0.137191691887319,
-0.128149664364088, -0.119572837388017, -0.109953053079138, -0.0972978435598013,
-0.0831080759384155, -0.071115594814103, -0.0650522447859859,
-0.0666755590279073, -0.0730178458107374, -0.0807231123791393,
-0.0864353659777761, -0.093731176054967, -0.104374785327469,
-0.112291710942601, -0.11532488840979, -0.115998870083849, -0.115261598380177,
-0.114061015714174, -0.112121981406158, -0.109279350709846, -0.106646059940738,
-0.105335045414329, -0.10502031419553, -0.104660283980211, -0.104294507030314,
-0.103962535607781, -0.0544997541294192, -0.0759672933472838,
-0.0928318384423578, -0.0995235408007862, -0.0921496441281631,
-0.0808943187101956, -0.075941734832591, -0.0721387042961063,
-0.0644417616889991, -0.0631996614782276, -0.0701231877757797,
-0.080159555991031, -0.0935121675370214, -0.110384423826791,
-0.134341886011727, -0.164771274930442, -0.19560773152681, -0.220786396744707,
-0.24470800956836, -0.271709665272387, -0.295631039236247, -0.3103118068394,
-0.311136008145884, -0.303141587685218, -0.294649077535086, -0.287800911512628,
-0.278977437979406, -0.26785606082973, -0.254114183957905, -0.228672234833075,
-0.193945588974598, -0.0248176049007914, -0.0280154922261918,
-0.00509363044425982, 0.0359756870492962, 0.0802930373308052,
0.112958997476596, 0.13498042401608, 0.156286940578198, 0.175363274888579,
0.190694154672854, 0.197511866511148, 0.197482141367721, 0.198739873544398,
0.206075849950098, 0.215615753800187, 0.222496823856169, 0.221856298879546,
0.209154962712217, 0.189688538371653, 0.173346673382715, 0.154502814473438,
0.127758414088336, 0.103345128326991, 0.091494613288984, 0.0964662157108718,
0.11027334789746, 0.124778764300703, 0.146992697502439, 0.180840414940991,
0.213557223353035, 0.232378429475247), followtime = c(0, 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, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3)), row.names = c("2075", "2076", "2077", "2078",
"2079", "2080", "2081", "2082", "2083", "2084", "2085", "2086",
"2087", "2088", "2089", "2090", "2091", "2092", "2093", "2094",
"2095", "2096", "2097", "2098", "2099", "2100", "2101", "2102",
"2103", "2104", "2105", "20751", "20761", "20771", "20781", "20791",
"20801", "20811", "20821", "20831", "20841", "20851", "20861",
"20871", "20881", "20891", "20901", "20911", "20921", "20931",
"20941", "20951", "20961", "20971", "20981", "20991", "21001",
"21011", "21021", "21031", "21041", "21051", "20752", "20762",
"20772", "20782", "20792", "20802", "20812", "20822", "20832",
"20842", "20852", "20862", "20872", "20882", "20892", "20902",
"20912", "20922", "20932", "20942", "20952", "20962", "20972",
"20982", "20992", "21002", "21012", "21022", "21032", "21042",
"21052"), class = "data.frame")
You need to use groupin your aes for geom_line:
ggplot(data=df, aes(x=time, y=totalsmooth_velocity, group = as.factor(followtime), color = as.factor(followtime)))+
geom_point( size = 1.0)+
geom_hline(yintercept=c(0, -0.16), color = "black")+
geom_line()
Ok, after several iterations, I did need to include an aes(group) value. However, in this case, I grouped by the original trials, instead of the followtimes. Ggplot2 was treating all followtimes=0 as the same group. New code:
stim1bfollows<- ggplot(data=follows, aes(x=time, y=totalsmooth_velocity, group=as.factor(trial), color=as.factor(followtime)))+
geom_line(size = 1.0)+
geom_point(size=1.5)+
geom_hline(yintercept=c(0,-0.16), color = c("black", "red"))+scale_color_manual(values = c("black", "coral", "aquamarine4", "violetred"))
stim1bfollows
And new graph (keeping the geom_point overlay over geom_line, makes the figure look really slick):
Thanks for the help.

Why do geom_rect() add many multiple layers when used with facet_wrap()?

I am adding a color bar using geom_rect() in combination with facet_wrap(), but for some reason 30 layers are added, which means the bar is completely dark even though I use alpha = 0.2.
I can export to powerpoint and delete all the extra layes manually, but that is a huuuge pain. Is there a way to fix this problem?
I have tried to restart the terminal and to only load the package needed, thinking maybe the error occured do to a loaded function, but no, it does not seem to be the case.
dat <- structure(list(variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L,
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L,
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 19L, 19L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 22L, 22L, 22L, 22L, 22L,
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L,
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L,
22L, 22L, 22L, 22L), .Label = c("alat", "asat", "chol", "cl",
"fhb", "ft3", "ft4", "ggt", "gluk", "hdlch", "hrs", "k", "kreae",
"ldh", "ldlch", "lipa", "mg", "na", "phos", "prot", "trig", "tsh"
), class = "factor"), diff_A = c(0.03, -0.02, -0.01, 0, -0.03,
-0.01, -0.01, 0.03, 0.02, 0, 0.01, 0.0099999999999999, 0, 0.02,
0.01, 0, -0.03, -0.02, -0.01, 0.02, 0, -0.01, 0.02, 0.02, 0,
0.03, -0.02, -0.01, 0, 0.00999999999999995, 0, 0, 0.01, 0, -0.0299999999999999,
0.04, 0.03, -0.04, 0, 0.02, 0.03, -0.03, -0.01, -0.05, -0.07,
-0.01, -0.00999999999999995, 0.02, 0, 0.0099999999999999, 0.01,
0.06, -0.04, 0.01, 0.06, -0.03, 0.01, 0.03, 0.02, 0, 0.02, 0,
0.00999999999999995, 0.00999999999999995, -0.01, 0.01, -0.02,
0.02, 0.02, 0.13, 0.0999999999999996, 0.2, 0, 0, 0.3, 0, -0.2,
0.100000000000001, 0, -0.2, 0.0999999999999996, -0.1, 0, -0.300000000000001,
-0.0999999999999996, 0, 0, -0.2, 0.100000000000001, -0.100000000000001,
-0.3, -0.0999999999999996, -0.0999999999999996, 0.3, 0, 0.2,
0, 0, 0.100000000000001, -0.2, 0, 0, -0.199999999999999, 0.0999999999999996,
-0.0999999999999996, 1, -1, 0, 0, 3, -1, 0, 0, -1, -2, 0, -2,
0, -1, 1, 0, -2, -2, 0, 1, -1, 1, -1, 3, -2, 0, 0, -1, -1, 1,
0, 0, -1, 0, 0, 1, 0, 2, 1, -1, 1, 0, 3, 2, -3, 4, 1, -2, 2,
1, 3, 0, 2, 2, 4, -2, -1, 1, 1, 1, 1, 1, 3, 5, 0, 1.1, -1, 1,
1, 1, 0.23, -0.71, 0.21, 0.0599999999999996, -0.4, 0.59, -0.0299999999999994,
0.0899999999999999, 0.15, -0.0700000000000003, -0.04, -0.0999999999999996,
0.13, -0.79, -0.27, -0.18, -0.0600000000000001, -0.26, 0.24,
0.63, -0.0500000000000007, -0.28, -0.31, 0.43, -0.2, -0.0499999999999998,
0.149999999999999, -0.319999999999999, 0.0999999999999996, 0.34,
0.0499999999999998, -0.1, 0.3, 0.0699999999999998, 0.0600000000000001,
0.699999999999999, 0.6, 0, 0.300000000000001, -0.199999999999999,
-0.0299999999999994, -0.299999999999999, -0.0999999999999996,
-0.199999999999999, 0.0999999999999996, 0, 0.0999999999999979,
0.0999999999999996, -0.0999999999999996, -0.200000000000001,
-0.0299999999999994, -0.300000000000001, -0.9, -0.0999999999999996,
0.5, -0.5, 0.0999999999999996, -0.0999999999999996, 0.4, -0.200000000000001,
0.300000000000001, 0, -0.199999999999999, -0.4, 0.4, -0.0999999999999996,
0.5, 0.800000000000001, -0.100000000000001, 0.5, 0.02, 0.01,
-0.02, -0.01, 0.05, 0, 0.02, 0, -0.00999999999999995, 0, -0.01,
0.0599999999999999, -0.01, 0.03, 0.01, 0.04, 0.07, 0.05, -0.01,
-0.06, 0.03, -0.03, 0, -0.03, 0.04, 0.01, -0.01, 0, 0.02, -0.03,
0.02, 0.03, 0.03, -0.02, 0, -0.0999999999999996, 0.100000000000001,
0.0999999999999996, -0.199999999999999, -0.4, -0.6, -0.0999999999999996,
0.2, 0, 0.1, -0.0999999999999996, 0.0999999999999996, -0.1, 0.0999999999999996,
-0.100000000000001, 0.0999999999999996, -0.7, -0.2, 0.4, 0.399999999999999,
-0.0999999999999996, -0.0999999999999996, -0.100000000000001,
-0.2, -0.100000000000001, 0.100000000000001, -0.0999999999999996,
-0.1, 0.100000000000001, -0.3, 0, 0, -0.300000000000001, -0.1,
0.3, 0.01, -0.02, 0.01, -0.0900000000000001, 0.11, 0.00999999999999979,
-0.01, -0.01, 0.04, -0.0699999999999998, -0.04, -0.03, 0.03,
-0.0399999999999998, 0.1, 0, 0.03, -0.0700000000000001, -0.0599999999999998,
0.04, 0.03, 0.12, -0.0900000000000001, 0.1, -0.0600000000000001,
0.0700000000000001, 0.02, 0, -0.0399999999999998, 0.0900000000000003,
-0.02, -0.03, 0.03, 0.11, 0, 1, 2, 0, 1, 20, 8, 4, 9, -12, -23,
1, -13, -2, 2, -10, 0, 2, 2, 2, 2, 7, 9, -7, 6, 1, -9, -3, 0,
-12, 12, -2, 1, 14, -3, 4, 0, 0, 0.1, 0, 0.0999999999999996,
-0.0999999999999996, 0, 0.1, 0, -0.1, 0, 0, -0.0999999999999996,
0, 0, 0, -0.3, -0.0999999999999996, 0.1, 0.1, 0, -0.1, 0, 0,
0.1, -0.2, 0.1, 0, 0, 0, 0, 0, 0, 0, 0.2, -3, 1, 4, 0, -2, 0,
-1, 3, 1, -3, -5, -2, -1, -4, -2, -1, -3, -8, 4, 0, -14, 6, 1,
16, -14, 1, 5, 1, -2, 7, 0, -8, 3, -2, -2, 0.0800000000000001,
0.12, 0.04, 0.0600000000000001, 0.0499999999999998, -0.26, 0.0600000000000001,
0.0499999999999998, 0.42, 0.02, 0.1, 0.3, 0, 0.32, 0.02, 0.11,
0.0900000000000003, 0.0600000000000001, -0.2, 0.26, -0.14, -0.32,
0.27, -0.24, 0.0300000000000002, 0.0799999999999996, 0.14, 0.59,
0.25, 0.02, 0.11, 0.0500000000000003, 0.13, 0.27, 0.14, 0, 0.0100000000000002,
-0.02, 0, -0.11, -0.12, -0.02, -0.13, -0.02, 0.1, 0, 0.17, 0.11,
-0.14, 0.0500000000000003, 0.00999999999999979, 0.02, -0.0900000000000003,
-0.0599999999999998, 0.04, -0.0899999999999999, -0.0899999999999999,
0.13, 0.32, -0.22, 0.14, 0.00999999999999979, 0.04, -0.11, -0.01,
0.0299999999999998, 0.0800000000000001, -0.34, 0.04, -0.2, 0.04,
0.0799999999999996, 0.0299999999999998, 0.0499999999999998, 0.19,
-0.0100000000000007, 0, 0.17, -0.0800000000000001, -0.12, 0.15,
0.00999999999999979, 0.15, 0.1, -0.0299999999999998, 0.04, -0.15,
-0.22, 0.17, 0.0899999999999999, -0.26, -0.2, 0.1, 0.2, -0.46,
0.02, 0.13, -0.0100000000000002, -0.01, 0.0299999999999998, -0.1,
-0.18, -0.11, -0.0899999999999999, -0.11, 0.01, -0.01, 0.02,
0.0199999999999999, -0.01, 0.03, -0.01, 0.03, 0, -0.02, 0, 0,
0.02, -0.04, 0.05, 0, 0.0299999999999999, 0.01, 0.0399999999999999,
0.0499999999999999, -0.0599999999999999, -0.01, -0.01, 0.03,
-0.0299999999999999, -0.01, -0.03, -0.01, 0.02, 0.01, -0.03,
0, 0.0499999999999999, -0.05, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, -3,
0, -1, 0, -2, 2, 0, 1, 0, 0, 1, -1, 1, 1, 0, 0, 0, 2, -1, -1,
1, -1, -1, -1, 0, 0, 0.01, 0.05, -0.02, 0.01, 0.02, -0.0299999999999998,
-0.02, 0.03, 0.03, 0.01, 0.04, 0.21, -0.03, -0.0700000000000001,
0.01, -0.0299999999999999, -0.08, -0.0600000000000001, 0.02,
0, -0.0699999999999998, -0.03, 0.03, -0.02, 0, -0.03, 0, -0.05,
0.02, 0.01, -0.0399999999999999, 0, 0.03, 0.01, 0.01, 1, 0, 0,
0, 2, -1, -2, 3, -1, -1, 1, -1, 0, 0, 0, -2, 1, 2, -3, -1, 2,
1, 1, 1, -1, 2, 1, 1, 1, 2, 0, -1, 3, -2, -1, 0.01, 0, -0.02,
0.04, 0, -0.04, 0.03, -0.0299999999999999, -0.01, -0.01, 0.01,
0.01, 0.01, 0.02, 0.03, -0.09, 0.04, -0.0600000000000001, 0.05,
0.05, -0.0499999999999998, -0.0199999999999999, 0.01, 0.05, -0.0599999999999999,
0.0699999999999998, 0, 0.02, -0.01, -0.05, -0.02, 0.02, 0, 0.0399999999999998,
-0.0399999999999998, 0.01, -0.03, -0.02, -0.01, 0.02, 0.0600000000000001,
-0.05, 0, -0.12, -0.13, -0.03, 0, -0.0600000000000001, 0.03,
-0.01, 0, 0.02, 0.04, -0.0600000000000001, 0.035, -0.02, 0.0309999999999999,
0.0599999999999998, 0.01, 0.03, 0.0500000000000003, -0.0399999999999996,
0.0499999999999998, 0, 0.00800000000000001, 0, -0.00900000000000001,
0.14, 0, -0.025), MD_Fuss = c(0.06, 0.06, 0.06, 0.06, 0.06, 0.06,
0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06,
0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06,
0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.09, 0.09, 0.09, 0.09,
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09,
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09,
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.54, 0.54,
0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54,
0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54,
0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54, 0.54,
6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49,
6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49,
6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49, 6.49,
6.49, 6.49, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66,
0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66,
0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.66,
0.66, 0.66, 0.66, 0.66, 0.66, 0.66, 0.81, 0.81, 0.81, 0.81, 0.81,
0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81,
0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81,
0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, 0.81, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.35,
0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35,
0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35,
0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35,
0.35, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16,
0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16,
0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16,
0.16, 0.16, 0.16, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84,
12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84,
12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84,
12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84, 12.84,
12.84, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17,
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17,
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17,
0.17, 0.17, 0.17, 0.17, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79,
16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79,
16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79,
16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79, 16.79,
16.79, 16.79, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36,
0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36,
0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36, 0.36,
0.36, 0.36, 0.36, 0.36, 0.36, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29,
0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29,
0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29,
0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.29, 0.35, 0.35, 0.35, 0.35,
0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35,
0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35,
0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.35, 0.06, 0.06,
0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06,
0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06,
0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06, 0.06,
5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21,
5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21,
5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21, 5.21,
5.21, 5.21, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16,
0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16,
0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16,
0.16, 0.16, 0.16, 0.16, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19,
3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19,
3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 3.19,
3.19, 3.19, 3.19, 3.19, 3.19, 3.19, 0.11, 0.11, 0.11, 0.11, 0.11,
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11,
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11,
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.16, 0.16, 0.16,
0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16,
0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16,
0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16)), class = "data.frame", row.names = c(NA,
-770L))
and this is the plot:
p <- ggplot(data=dat, aes(y = diff_A))+
geom_boxplot(outlier.shape = 1)+
geom_rect(aes(ymin = -MD_Fuss, ymax = MD_Fuss), xmin = -Inf, xmax =Inf, alpha = 0.2)+
theme_bw()+ theme(panel.grid = element_blank())+
xlab('')+ ylab('[mmol/L]') +
scale_y_continuous(expand = c(0.5, 0))+
facet_wrap(.~variable, scales = 'free')
p
geom_rect() draws a rectangle for each row in your data. To get only one rectangle per facet, you need to pass it a data set that contains only one row per faceting variable. Since MD_Fuss seems to be constant within a variable, you can create that data set with unique(dat[, c("variable", "MD_Fuss")]), and then pass it as the data argument to geom_rect():
library(ggplot2)
p <- ggplot(data = dat) +
geom_boxplot(aes(y = diff_A), outlier.shape = 1) +
geom_rect(
data = unique(dat[, c("variable", "MD_Fuss")]),
aes(ymin = -MD_Fuss, ymax = MD_Fuss),
xmin = -Inf, xmax = Inf, alpha = 0.2,
) +
theme_bw() + theme(panel.grid = element_blank()) +
xlab("") + ylab("[mmol/L]") +
scale_y_continuous(expand = c(0.5, 0)) +
facet_wrap(. ~ variable, scales = "free")
p
#> Warning: Removed 2 rows containing missing values (geom_rect).
Created on 2019-07-19 by the reprex package (v0.3.0.9000)

correlation plots for consecutive study sites within a loop: how can i add site names as main headers?

edited to include dput(Nitrate_set) output:
structure(list(SITE.NO = structure(c(41L, 42L, 41L, 3L, 35L,
42L, 40L, 40L, 40L, 1L, 40L, 1L, 40L, 40L, 16L, 40L, 42L, 40L,
40L, 42L, 1L, 42L, 40L, 1L, 40L, 1L, 40L, 4L, 42L, 21L, 15L,
5L, 36L, 40L, 40L, 2L, 41L, 6L, 2L, 12L, 42L, 19L, 2L, 1L, 1L,
1L, 2L, 42L, 1L, 2L, 7L, 1L, 39L, 2L, 2L, 2L, 2L, 39L, 11L, 37L,
2L, 1L, 1L, 1L, 1L, 32L, 1L, 41L, 2L, 2L, 1L, 2L, 1L, 1L, 2L,
1L, 2L, 40L, 42L, 1L, 2L, 38L, 1L, 1L, 2L, 2L, 2L, 41L, 38L,
1L, 8L, 42L, 42L, 1L, 2L, 2L, 33L, 1L, 2L, 18L, 2L, 1L, 2L, 2L,
1L, 41L, 1L, 2L, 1L, 40L, 1L, 2L, 2L, 2L, 1L, 2L, 41L, 10L, 14L,
2L, 2L, 2L, 2L, 42L, 9L, 1L, 1L, 2L, 2L, 2L, 38L, 1L, 1L, 2L,
2L, 38L, 42L, 38L, 40L, 42L, 41L, 1L, 1L, 2L, 38L, 38L, 1L, 38L,
2L, 1L, 2L, 2L, 2L, 17L, 2L, 42L, 41L, 2L, 1L, 1L, 2L, 1L, 38L,
1L, 1L, 38L, 38L, 38L, 38L, 2L, 1L, 2L, 26L, 38L, 38L, 1L, 2L,
38L, 38L, 2L, 1L, 1L, 1L, 38L, 38L, 1L, 31L, 2L, 2L, 1L, 1L,
34L, 24L, 38L, 2L, 28L, 38L, 25L, 38L, 38L, 38L, 38L, 39L, 1L,
38L, 1L, 29L, 38L, 38L, 1L, 38L, 2L, 2L, 22L, 1L, 27L, 1L, 30L,
38L, 38L, 1L, 2L, 2L, 38L, 2L, 38L, 38L, 38L, 13L, 38L, 1L, 38L,
1L, 23L, 2L, 39L, 20L, 38L, 38L, 2L, 38L, 38L, 38L, 38L, 38L,
38L, 38L, 38L, 38L, 1L, 38L, 38L, 41L, 38L, 1L, 38L, 38L, 2L,
38L, 38L, 38L, 38L, 1L, 38L, 1L, 38L, 2L, 2L, 38L, 1L, 40L, 2L,
42L, 41L, 42L, 38L, 42L, 38L, 41L, 2L, 42L, 1L, 38L, 38L, 42L,
2L, 42L, 38L, 38L, 2L, 2L, 2L, 39L, 40L, 2L, 42L, 1L, 1L, 38L,
40L, 41L, 2L, 41L, 41L, 1L, 2L, 41L, 1L, 39L, 41L, 1L, 41L, 40L,
1L, 39L, 39L, 2L, 40L, 38L, 1L, 2L, 2L, 38L, 39L, 1L, 2L, 38L,
41L, 38L, 38L, 2L, 38L, 38L, 41L, 2L, 39L, 43L, 39L, 41L, 43L,
43L, 43L, 43L, 41L, 39L, 39L, 41L, 43L, 41L, 39L, 39L, 39L, 39L,
39L, 39L, 43L, 43L, 43L, 43L, 43L, 41L), .Label = c("1110056",
"1111019", "1120054", "1120055", "1120060", "1120061", "1120062",
"1120065", "1120066", "1120067", "1120068", "1120069", "1120070",
"1120071", "1120073", "1120074", "1120075", "1120076", "1120077",
"1120078", "1120079", "1120080", "1120081", "1120082", "1120083",
"1120084", "1120085", "1120086", "1120087", "1120088", "1120089",
"1120090", "1120091", "1120092", "1120094", "1120095", "1120096",
"113006A", "122013A", "125013A", "126001A", "1260122", "141010A"
), class = "factor"), SITE.NAME = structure(c(8L, 5L, 8L, 2L,
2L, 5L, 4L, 4L, 4L, 3L, 4L, 3L, 4L, 4L, 2L, 4L, 5L, 4L, 4L, 5L,
3L, 5L, 4L, 3L, 4L, 3L, 4L, 2L, 5L, 2L, 2L, 2L, 2L, 4L, 4L, 7L,
8L, 2L, 7L, 2L, 5L, 2L, 7L, 3L, 3L, 3L, 7L, 5L, 3L, 7L, 2L, 3L,
6L, 7L, 7L, 7L, 7L, 6L, 2L, 2L, 7L, 3L, 3L, 3L, 3L, 2L, 3L, 8L,
7L, 7L, 3L, 7L, 3L, 3L, 7L, 3L, 7L, 4L, 5L, 3L, 7L, 9L, 3L, 3L,
7L, 7L, 7L, 8L, 9L, 3L, 2L, 5L, 5L, 3L, 7L, 7L, 2L, 3L, 7L, 2L,
7L, 3L, 7L, 7L, 3L, 8L, 3L, 7L, 3L, 4L, 3L, 7L, 7L, 7L, 3L, 7L,
8L, 2L, 2L, 7L, 7L, 7L, 7L, 5L, 2L, 3L, 3L, 7L, 7L, 7L, 9L, 3L,
3L, 7L, 7L, 9L, 5L, 9L, 4L, 5L, 8L, 3L, 3L, 7L, 9L, 9L, 3L, 9L,
7L, 3L, 7L, 7L, 7L, 2L, 7L, 5L, 8L, 7L, 3L, 3L, 7L, 3L, 9L, 3L,
3L, 9L, 9L, 9L, 9L, 7L, 3L, 7L, 2L, 9L, 9L, 3L, 7L, 9L, 9L, 7L,
3L, 3L, 3L, 9L, 9L, 3L, 2L, 7L, 7L, 3L, 3L, 2L, 2L, 9L, 7L, 2L,
9L, 2L, 9L, 9L, 9L, 9L, 6L, 3L, 9L, 3L, 2L, 9L, 9L, 3L, 9L, 7L,
7L, 2L, 3L, 2L, 3L, 2L, 9L, 9L, 3L, 7L, 7L, 9L, 7L, 9L, 9L, 9L,
2L, 9L, 3L, 9L, 3L, 2L, 7L, 6L, 2L, 9L, 9L, 7L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 3L, 9L, 9L, 8L, 9L, 3L, 9L, 9L, 7L, 9L, 9L,
9L, 9L, 3L, 9L, 3L, 9L, 7L, 7L, 9L, 3L, 4L, 7L, 5L, 8L, 5L, 9L,
5L, 9L, 8L, 7L, 5L, 3L, 9L, 9L, 5L, 7L, 5L, 9L, 9L, 7L, 7L, 7L,
6L, 4L, 7L, 5L, 3L, 3L, 9L, 4L, 8L, 7L, 8L, 8L, 3L, 7L, 8L, 3L,
6L, 8L, 3L, 8L, 4L, 3L, 6L, 6L, 7L, 4L, 9L, 3L, 7L, 7L, 9L, 6L,
3L, 7L, 9L, 8L, 9L, 9L, 7L, 9L, 9L, 8L, 7L, 6L, 1L, 6L, 8L, 1L,
1L, 1L, 1L, 8L, 6L, 6L, 8L, 1L, 8L, 6L, 6L, 6L, 6L, 6L, 6L, 1L,
1L, 1L, 1L, 1L, 8L), .Label = c("Coochin Creek at Mawsons Road",
"Johnstone River at Coquette Point Gbr_Jri_Wq", "Mulgrave River at Deeral",
"Pioneer River at Dumbleton Weir Headwater", "Plane Creek at Sucrogen Weir",
"Proserpine River at Glen Isla", "Russell River at East Russell",
"Sandy Creek at Homebush", "Tully River at Euramo"), class = "factor"),
Date = structure(c(64L, 62L, 48L, 63L, 42L, 8L, 54L, 75L,
75L, 48L, 12L, 73L, 3L, 37L, 40L, 17L, 69L, 69L, 47L, 4L,
27L, 4L, 48L, 64L, 74L, 79L, 74L, 79L, 54L, 49L, 40L, 80L,
61L, 62L, 64L, 53L, 3L, 80L, 53L, 4L, 81L, 45L, 53L, 53L,
50L, 53L, 50L, 31L, 50L, 50L, 81L, 53L, 17L, 53L, 50L, 70L,
46L, 43L, 4L, 6L, 70L, 53L, 13L, 65L, 13L, 2L, 56L, 37L,
25L, 53L, 65L, 68L, 56L, 46L, 2L, 70L, 13L, 12L, 81L, 40L,
6L, 24L, 16L, 16L, 46L, 68L, 2L, 80L, 52L, 68L, 81L, 81L,
14L, 46L, 30L, 60L, 30L, 70L, 56L, 45L, 61L, 16L, 65L, 56L,
16L, 81L, 55L, 5L, 32L, 10L, 13L, 13L, 46L, 72L, 32L, 49L,
14L, 4L, 8L, 42L, 35L, 1L, 41L, 37L, 81L, 63L, 52L, 76L,
1L, 32L, 53L, 40L, 55L, 52L, 58L, 53L, 10L, 53L, 20L, 48L,
80L, 19L, 49L, 13L, 56L, 53L, 19L, 50L, 21L, 46L, 35L, 21L,
19L, 45L, 16L, 64L, 81L, 19L, 32L, 25L, 52L, 72L, 38L, 49L,
49L, 26L, 38L, 38L, 38L, 32L, 80L, 16L, 56L, 46L, 50L, 1L,
32L, 41L, 41L, 32L, 58L, 58L, 41L, 56L, 13L, 23L, 68L, 55L,
49L, 32L, 15L, 5L, 55L, 2L, 40L, 65L, 65L, 53L, 58L, 25L,
13L, 5L, 37L, 61L, 11L, 35L, 65L, 60L, 11L, 1L, 52L, 45L,
58L, 52L, 6L, 56L, 49L, 68L, 13L, 68L, 35L, 49L, 55L, 49L,
45L, 30L, 55L, 19L, 8L, 67L, 22L, 70L, 5L, 52L, 45L, 34L,
49L, 45L, 32L, 80L, 16L, 13L, 70L, 49L, 67L, 16L, 19L, 44L,
67L, 42L, 21L, 21L, 80L, 1L, 45L, 45L, 59L, 4L, 59L, 29L,
29L, 21L, 45L, 71L, 45L, 45L, 80L, 4L, 40L, 40L, 8L, 4L,
15L, 81L, 18L, 18L, 15L, 20L, 54L, 81L, 12L, 76L, 57L, 18L,
12L, 81L, 20L, 80L, 80L, 15L, 81L, 81L, 31L, 20L, 8L, 28L,
18L, 20L, 18L, 20L, 80L, 4L, 47L, 69L, 18L, 18L, 62L, 18L,
20L, 10L, 4L, 10L, 4L, 9L, 10L, 12L, 18L, 28L, 9L, 8L, 18L,
18L, 9L, 12L, 8L, 9L, 4L, 28L, 4L, 8L, 8L, 8L, 8L, 10L, 8L,
66L, 39L, 8L, 20L, 77L, 36L, 36L, 36L, 20L, 4L, 74L, 12L,
51L, 20L, 4L, 78L, 78L, 81L, 81L, 4L, 7L, 7L, 7L, 7L, 33L,
12L), .Label = c("1/01/2019", "1/02/2019", "1/11/2018", "10/12/2018",
"11/02/2019", "11/03/2019", "11/04/2019", "11/12/2018", "12/12/2018",
"13/12/2018", "14/01/2019", "14/12/2018", "15/01/2019", "15/11/2018",
"15/12/2018", "16/01/2019", "16/11/2018", "16/12/2018", "17/01/2019",
"17/12/2018", "18/01/2019", "18/02/2019", "18/03/2019", "18/09/2018",
"19/01/2019", "19/02/2019", "19/11/2018", "19/12/2018", "2/01/2019",
"2/02/2019", "2/11/2018", "20/01/2019", "20/04/2019", "20/12/2018",
"21/01/2019", "21/04/2019", "22/11/2018", "24/01/2019", "24/04/2019",
"24/12/2018", "25/01/2019", "25/02/2019", "25/09/2018", "25/10/2018",
"25/12/2018", "26/01/2019", "26/09/2018", "26/11/2018", "26/12/2018",
"27/01/2019", "27/03/2019", "27/12/2018", "28/01/2019", "28/08/2018",
"28/12/2018", "29/01/2019", "29/11/2018", "29/12/2018", "3/01/2019",
"3/02/2019", "3/03/2019", "3/07/2018", "3/09/2018", "3/12/2018",
"30/01/2019", "30/11/2018", "30/12/2018", "31/01/2019", "31/07/2018",
"31/12/2018", "4/01/2019", "4/02/2019", "5/11/2018", "5/12/2018",
"6/12/2018", "7/01/2019", "7/03/2019", "7/12/2018", "8/10/2018",
"8/12/2018", "9/12/2018"), class = "factor"), Trios.nitrate..mg.L. = c(0.01,
0.01, 0.01, 0.01, 0.02, 0.02, 0.02, 0.02, 0.02, 0.03, 0.03,
0.03, 0.03, 0.03, 0.03, 0.03, 0.03, 0.04, 0.04, 0.04, 0.04,
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.06, 0.06,
0.07, 0.07, 0.07, 0.08, 0.08, 0.08, 0.08, 0.08, 0.08, 0.09,
0.09, 0.09, 0.09, 0.09, 0.09, 0.09, 0.1, 0.1, 0.1, 0.1, 0.1,
0.1, 0.1, 0.1, 0.1, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.11,
0.11, 0.11, 0.11, 0.11, 0.11, 0.11, 0.12, 0.12, 0.12, 0.12,
0.12, 0.12, 0.12, 0.12, 0.12, 0.13, 0.13, 0.13, 0.13, 0.13,
0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13,
0.13, 0.13, 0.13, 0.13, 0.13, 0.14, 0.14, 0.14, 0.14, 0.14,
0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14, 0.14,
0.14, 0.14, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15,
0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.16, 0.16, 0.16,
0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.16, 0.17,
0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.17, 0.18, 0.18, 0.18,
0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.18, 0.19, 0.19,
0.19, 0.19, 0.19, 0.19, 0.19, 0.19, 0.19, 0.19, 0.19, 0.19,
0.19, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2,
0.2, 0.2, 0.2, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21, 0.21,
0.21, 0.21, 0.21, 0.21, 0.21, 0.22, 0.22, 0.22, 0.22, 0.22,
0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22, 0.22,
0.22, 0.22, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23,
0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23, 0.23,
0.23, 0.23, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24, 0.24,
0.24, 0.24, 0.24, 0.25, 0.25, 0.25, 0.26, 0.26, 0.26, 0.26,
0.26, 0.26, 0.26, 0.26, 0.26, 0.26, 0.26, 0.26, 0.26, 0.27,
0.27, 0.27, 0.28, 0.28, 0.3, 0.3, 0.3, 0.31, 0.31, 0.31,
0.31, 0.31, 0.32, 0.32, 0.33, 0.34, 0.34, 0.34, 0.35, 0.35,
0.36, 0.36, 0.37, 0.37, 0.37, 0.38, 0.38, 0.38, 0.38, 0.38,
0.39, 0.39, 0.4, 0.42, 0.42, 0.42, 0.43, 0.44, 0.48, 0.49,
0.49, 0.51, 0.51, 0.53, 0.56, 0.57, 0.58, 0.58, 0.58, 0.58,
0.58, 0.61, 0.64, 0.73, 0.76, 0.76, 0.81, 0.82, 0.85, 0.85,
0.85, 0.86, 0.88, 0.92, 0.94, 0.96, 1, 1.04, 1.04, 1.06,
1.14, 1.18, 1.34, 1.37, 1.45, 1.48, 1.48, 1.48, 1.54, 1.58,
1.73, 1.76, 1.82, 1.85, 1.95, 1.95, 1.96, 2.15, 2.18, 2.26,
2.3, 2.3, 2.3, 2.3, 2.69, 3.12), Chem.centre.Nox..mg.L. = c(5e-04,
0.001, 0.086, 0.048, 0.036, 0.015, 0.001, 0.088, 0.305, 0.012,
0.078, 0.023, 0.047, 0.002, 0.08, 0.004, 0.002, 0.003, 0.002,
0.029, 0.009, 0.045, 0.062, 0.006, 0.149, 0.087, 0.311, 0.008,
5e-04, 0.152, 0.03, 0.339, 0.103, 0.048, 0.003, 0.039, 0.159,
0.156, 0.043, 0.209, 0.091, 0.135, 0.039, 0.042, 0.041, 0.049,
0.045, 0.021, 0.045, 0.044, 0.235, 0.051, 0.03, 0.063, 0.046,
0.075, 0.058, 0.303, 0.227, 0.092, 0.094, 0.066, 0.068, 0.055,
0.073, 0.186, 0.079, 5e-04, 0.073, 0.086, 0.074, 0.097, 0.083,
0.062, 0.086, 0.084, 0.088, 0.098, 0.033, 0.093, 0.122, 0.104,
0.09, 0.093, 0.078, 0.095, 0.086, 0.178, 0.155, 0.098, 0.123,
0.134, 0.005, 0.065, 0.105, 0.109, 0.175, 0.098, 0.093, 0.081,
0.116, 0.116, 0.097, 0.098, 0.114, 0.159, 0.083, 0.126, 0.119,
0.092, 0.104, 0.116, 0.093, 0.114, 0.12, 0.118, 5e-04, 0.185,
0.265, 0.14, 0.168, 0.133, 0.116, 0.006, 0.057, 0.149, 0.086,
0.143, 0.137, 0.134, 0.164, 0.237, 0.114, 0.106, 0.123, 0.121,
0.086, 0.117, 0.184, 5e-04, 0.198, 0.148, 0.103, 0.159, 0.128,
0.134, 0.156, 0.134, 0.172, 0.082, 0.168, 0.176, 0.168, 0.153,
0.167, 0.001, 0.162, 0.171, 0.165, 0.152, 0.156, 0.17, 0.126,
0.124, 0.105, 0.188, 0.159, 0.17, 0.147, 0.174, 0.099, 0.176,
0.15, 0.141, 0.213, 0.166, 0.173, 0.181, 0.188, 0.192, 0.156,
0.062, 0.157, 0.167, 0.149, 0.234, 0.13, 0.172, 0.154, 0.194,
0.389, 0.171, 0.163, 0.181, 0.2, 0.156, 0.186, 0.17, 0.19,
0.196, 0.156, 0.218, 0.298, 0.218, 0.184, 0.206, 0.154, 0.193,
0.18, 0.192, 0.192, 0.145, 0.196, 0.158, 0.23, 0.172, 0.171,
0.154, 0.162, 0.209, 0.215, 0.179, 0.196, 0.183, 0.161, 0.208,
0.194, 0.208, 0.22, 0.178, 0.274, 0.184, 0.214, 0.222, 0.144,
0.169, 0.168, 0.183, 0.223, 0.184, 0.195, 0.181, 0.2, 0.202,
0.218, 0.191, 0.223, 0.057, 0.206, 0.237, 0.231, 0.244, 0.295,
0.217, 0.153, 0.214, 0.256, 0.17, 0.246, 0.25, 0.232, 0.247,
0.168, 0.247, 0.156, 0.214, 0.196, 0.183, 0.24, 0.205, 0.519,
0.197, 0.119, 0.306, 0.143, 0.257, 0.118, 0.269, 0.352, 0.256,
0.13, 0.307, 0.066, 0.272, 0.19, 0.261, 0.172, 0.222, 0.232,
0.236, 0.29, 0.276, 0.316, 0.217, 0.257, 0.206, 0.247, 0.28,
0.322, 0.304, 0.412, 0.36, 0.377, 0.421, 0.326, 0.33, 0.464,
0.331, 0.212, 0.456, 0.434, 0.486, 0.253, 0.48, 0.337, 0.549,
0.45, 0.611, 0.579, 0.594, 0.613, 0.672, 0.663, 0.389, 0.694,
0.718, 0.71, 0.692, 0.754, 0.816, 0.77, 0.863, 0.878, 0.388,
0.914, 0.42, 0.918, 1.06, 0.859, 1.58, 1.39, 0.922, 0.633,
1.22, 1.31, 0.399, 1.46, 1.71, 1.51, 1.58, 0.271, 0.44, 4.24,
3.65, 2.06, 2.61, 5e-04, 0.001, 0.065, 1.38, 2.91)), class = "data.frame", row.names = c(NA,
-361L))
>
The input file (Nitrate_set) includes 9 study sites. I have been able to produce a correlation plot between two variables of interest consecutively for each site within a loop, but am unable to add to appropriate site name as a header for each plot. Here's an example of the data:
SITE.NAME COR
1 Coochin Creek at Mawsons Road -0.1122249
2 Johnstone River at Coquette Point Gbr_Jri_Wq 0.3614868
3 Mulgrave River at Deeral 0.9338604
4 Pioneer River at Dumbleton Weir Headwater 0.7270477
5 Plane Creek at Sucrogen Weir 0.8337472
6 Proserpine River at Glen Isla 0.6695578
7 Russell River at East Russell 0.9879924
8 Sandy Creek at Homebush 0.9756037
9 Tully River at Euramo 0.9751152
I would like each correlation plot to display a header appropriate to it's site name. I'm not sure where to put that code within the loop.
This is the first time I am trying to produce plots within a loop and I'm a novice coder. I don't know what to try next..
#Creating function func to calculate correlation for single site
func <- function(Nitrate_set){ return(data.frame(COR = cor(Nitrate_set$Trios.nitrate..mg.L., Nitrate_set$Chem.centre.Nox..mg.L.))) }
#Calling func Function for each site name and storing the results in cor_result. cor_result will show correlation for each site as a list.
cor_result <- ddply(Nitrate_set, .(SITE.NAME), func)
cor_result
#Creating function to display correlation plot for every site
funcPlot <- function(Nitrate_set){
chart.Correlation(Nitrate_set[,c(4,5)], histogram=TRUE, pch=19)
}
#Calling the function to display the plots. This will return nine plots serially. At this point we need to title site name for each site.
ddply(Nitrate_set, .(SITE.NAME), funcPlot)
I expect each plot to be displayed with a header but am unable to work out how and where to put that section of code within the loop

How to differentiate Bars in geom_bar without color: ggplot

Note: A similar question is present at link, but I posed it a separate question due to: 1) only a hack is provided to the previos question which I thought would make code unnecessary complex 2) I thought after 2013 a fix might have been suggested for this
I am using following code to draw bars/stacks
ggplot(finaldataframe,aes(day,score))+
geom_bar(aes(fill=identify),stat="identity",position = "dodge",width = .7, show.legend = TRUE)+
labs(x= "Day of the Month", y="Anomaly Score") +
scale_fill_discrete(name="Method", labels=c("Mean","Maximum","Cumulative \n sum"))+
theme(axis.text= element_text(color="Black"))+ scale_x_continuous(breaks=seq(1,31,5))
A portion of output is as
The problem with this figure is that once I print this via black and white printer It gets hard to differentiate between different stacks. Is there any way to make the stacks differentiable on a black and white print. I am looking for some what like this:
For reproduction, Here is the dput of dataframe:
> dput(finaldataframe)
finaldataframe = structure(list(day = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L,
24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L,
29L, 30L, 31L), score = c(0, 0.02, 0.01, 0, 0.02, 0.01, 0.01,
0.02, 0.02, 0.28, 0.24, 0.01, 0.94, 0.22, 0.25, 0.01, 0.31, 0.22,
0.24, 0.83, 0.4, 0.44, 0.06, 0.02, 0.37, 0.07, 0.12, 0.06, 0.1,
0.06, 0.1, 0, 0.05, 0.04, 0.02, 0.05, 0.01, 0.02, 0.03, 0.04,
0.37, 0.36, 0.04, 1, 0.28, 0.34, 0.03, 0.55, 0.35, 0.32, 1, 0.71,
1, 0.13, 0.04, 0.47, 0.12, 0.17, 0.1, 0.18, 0.1, 0.14, 0, 0.02,
0.01, 0, 0.02, 0.01, 0.01, 0.02, 0.02, 0.3, 0.25, 0.01, 1, 0.23,
0.27, 0, 0.33, 0.24, 0.26, 0.89, 0.42, 0.47, 0.06, 0.02, 0.4,
0.07, 0.13, 0.06, 0.11, 0.06, 0.1), identify = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Mean",
"Maximum", "Cummulative Sum"), class = "factor")), .Names = c("day",
"score", "identify"), row.names = c(NA, 93L), class = "data.frame")

Resources