Creating a continuous heat map in R - r

I have a series of x and y coordinates that each have a distance attached to them. I would like to create a heat map that displays the average distance for every point within the x and y ranges as a heat map. Since the points are not spaced evenly from each other in a lattice-like shape, the method would require some kind of smoothing function that clusters data and calculates the average for each point the vicinity and then representing that average with a color.
So far, using ggplot2, I can only find methods like stat_density2d and geom_tile, which only work for displaying point density and representing evenly spaced points (as far as I can tell).
Ideally it would follow the same principle as this image:
in which colors were assigned based on the given points in the vicinity even though the density and placement of the points was not uniform.
I do not want to create a heat map in matrix form like this image:
in which a table is color-coded. Instead, I would like to create a continuous heat map using non-uniformly distributed x and y coordinates that, in effect, displays the limit in which the data is broken into infinitely many rectangles. This may not be the actual method used by the function, but it provides a general idea as to what I'm looking for.
Here is some sample data:
data=data.frame(x=c(1,1,2,2,3,4,5,6,7,7,8,9),
y=c(2,4,5,1,3,8,4,8,1,1,6,9),
distance=c(66,84,93,76,104,29,70,19,60,50,46,36))
How can I make a heat map with distance as the color scale that covers the entire range of numbers, like the plot in the first link provided?
Any help is greatly appreciated!

In order to generate a continuous map with irregularly-spaced coordinates you need first to intrapolate a regular grid (here using function interp of package akima):
require(akima)
data <- data.frame(x=c(1,1,2,2,3,4,5,6,7,7,8,9),
y=c(2,4,5,1,3,8,4,8,1,1,6,9),
distance=c(66,84,93,76,104,29,70,19,60,50,46,36))
resolution <- 0.1 # you can increase the resolution by decreasing this number (warning: the resulting dataframe size increase very quickly)
a <- interp(x=data$x, y=data$y, z=data$distance,
xo=seq(min(data$x),max(data$x),by=resolution),
yo=seq(min(data$y),max(data$y),by=resolution), duplicate="mean")
image(a) #you can of course modify the color palette and the color categories. See ?image for more explanation
Or you can use, for the plotting itself, function filled.contour:
filled.contour(a, color.palette=heat.colors)

There is a user-written function here that produces heatmaps using ggplot2:
http://www.r-bloggers.com/ggheat-a-ggplot2-style-heatmap-function/
And their example image:
If what you want is a topo map as in your example, there are plenty of tools for that (just search under "topo map".
And finally, there's the isarithmic map, which just goes to show that you need to make clear exactly what you want done if you want some smoothing incorporated:
http://dsparks.wordpress.com/2011/10/24/isarithmic-maps-of-public-opinion-data/

using the akima::interp solution suggested by #plannapus, you can convert it to a ggplot2 heatmap.
Advantage of this ggplot2 solution is that you can easily add initial points with geom_point() or density curves with geom_density2d() (although here density will be unreliable with the 12 points you have).
library(akima)
library(tidyverse)
data <- data.frame(x=c(1,1,2,2,3,4,5,6,7,7,8,9),
y=c(2,4,5,1,3,8,4,8,1,1,6,9),
distance=c(66,84,93,76,104,29,70,19,60,50,46,36))
resolution <- 0.1 # you can increase the resolution by decreasing this number (warning: the resulting dataframe size increase very quickly)
a <- interp(x=data$x, y=data$y, z=data$distance,
xo=seq(min(data$x),max(data$x),by=resolution),
yo=seq(min(data$y),max(data$y),by=resolution), duplicate="mean")
res <- a$z %>%
magrittr::set_colnames(a$y) %>%
as_tibble() %>%
mutate(x=a$x) %>%
gather(y, z, -x, convert=TRUE)
res %>%
ggplot(aes(x, y)) +
geom_tile(aes(fill=z)) +
geom_point(data=data) +
scale_fill_viridis_c()
Created on 2020-01-29 by the reprex package (v0.3.0.9001)

ggplot2::ggfluctuation(data, type="colour")
I can't give out all this data but the head is below in the dput structure.
structure(list(X1 = 236:241, HomeTeam = structure(c(8L, 19L,
37L, 4L, 6L, 15L), .Label = c("Arizona Cardinals", "Atlanta Falcons",
"Baltimore Ravens", "Buffalo Bills", "Carolina Panthers", "Chicago Bears",
"Cincinnati Bengals", "Cleveland Browns", "Dallas Cowboys", "Denver Broncos",
"Detroit Lions", "Green Bay Packers", "Houston Oilers", "Houston Texans",
"Indianapolis Colts", "Jacksonville Jaguars", "Kansas City Chiefs",
"Los Angeles Raiders", "Los Angeles Rams", "Miami Dolphins",
"Minnesota Vikings", "New England Patriots", "New Orleans Saints",
"New York Giants", "New York Jets", "Oakland Raiders", "Philadelphia Eagles",
"Phoenix Cardinals", "Pittsburgh Steelers", "San Diego Chargers",
"San Francisco 49ers", "Seattle Seahawks", "St. Louis Rams",
"Tampa Bay Buccaneers", "Tennessee Oilers", "Tennessee Titans",
"Washington Redskins"), class = "factor"), AwayTeam = structure(c(9L,
28L, 11L, 20L, 21L, 22L), .Label = c("Arizona Cardinals", "Atlanta Falcons",
"Baltimore Ravens", "Buffalo Bills", "Carolina Panthers", "Chicago Bears",
"Cincinnati Bengals", "Cleveland Browns", "Dallas Cowboys", "Denver Broncos",
"Detroit Lions", "Green Bay Packers", "Houston Oilers", "Houston Texans",
"Indianapolis Colts", "Jacksonville Jaguars", "Kansas City Chiefs",
"Los Angeles Raiders", "Los Angeles Rams", "Miami Dolphins",
"Minnesota Vikings", "New England Patriots", "New Orleans Saints",
"New York Giants", "New York Jets", "Oakland Raiders", "Philadelphia Eagles",
"Phoenix Cardinals", "Pittsburgh Steelers", "San Diego Chargers",
"San Francisco 49ers", "Seattle Seahawks", "St. Louis Rams",
"Tampa Bay Buccaneers", "Tennessee Oilers", "Tennessee Titans",
"Washington Redskins"), class = "factor"), Date = structure(c(45L,
45L, 45L, 45L, 45L, 45L), .Label = c("1990-09-09", "1990-09-10",
"1990-09-16", "1990-09-17", "1990-09-23", "1990-09-24", "1990-09-30",
"1990-10-01", "1990-10-07", "1990-10-08", "1990-10-14", "1990-10-15",
"1990-10-18", "1990-10-21", "1990-10-22", "1990-10-28", "1990-10-29",
"1990-11-04", "1990-11-05", "1990-11-11", "1990-11-12", "1990-11-18",
"1990-11-19", "1990-11-22", "1990-11-25", "1990-11-26", "1990-12-02",
"1990-12-03", "1990-12-09", "1990-12-10", "1990-12-15", "1990-12-16",
"1990-12-17", "1990-12-22", "1990-12-23", "1990-12-29", "1990-12-30",
"1990-12-31", "1991-01-05", "1991-01-06", "1991-01-12", "1991-01-13",
"1991-01-20", "1991-01-27", "1991-09-01", "1991-09-02", "1991-09-08",
"1991-09-09", "1991-09-15", "1991-09-16", "1991-09-22", "1991-09-23",
"1991-09-29", "1991-09-30", "1991-10-06", "1991-10-07", "1991-10-13",
"1991-10-14", "1991-10-17", "1991-10-20", "1991-10-21", "1991-10-27",
"1991-10-28", "1991-11-03", "1991-11-04", "1991-11-10", "1991-11-11",
"1991-11-17", "1991-11-18", "1991-11-24", "1991-11-25", "1991-11-28",
"1991-12-01", "1991-12-02", "1991-12-08", "1991-12-09", "1991-12-14",
"1991-12-15", "1991-12-16", "1991-12-21", "1991-12-22", "1991-12-23",
"1991-12-28", "1991-12-29", "1992-01-04", "1992-01-05", "1992-01-12",
"1992-01-26", "1992-09-06", "1992-09-07", "1992-09-13", "1992-09-14",
"1992-09-20", "1992-09-21", "1992-09-27", "1992-09-28", "1992-10-04",
"1992-10-05", "1992-10-11", "1992-10-12", "1992-10-15", "1992-10-18",
"1992-10-19", "1992-10-25", "1992-10-26", "1992-11-01", "1992-11-02",
"1992-11-08", "1992-11-09", "1992-11-15", "1992-11-16", "1992-11-22",
"1992-11-23", "1992-11-26", "1992-11-29", "1992-11-30", "1992-12-03",
"1992-12-06", "1992-12-07", "1992-12-12", "1992-12-13", "1992-12-14",
"1992-12-19", "1992-12-20", "1992-12-21", "1992-12-26", "1992-12-27",
"1992-12-28", "1993-01-02", "1993-01-03", "1993-01-09", "1993-01-10",
"1993-01-17", "1993-01-31", "1993-09-05", "1993-09-06", "1993-09-12",
"1993-09-13", "1993-09-19", "1993-09-20", "1993-09-26", "1993-09-27",
"1993-10-03", "1993-10-04", "1993-10-10", "1993-10-11", "1993-10-14",
"1993-10-17", "1993-10-18", "1993-10-24", "1993-10-25", "1993-10-31",
"1993-11-01", "1993-11-07", "1993-11-08", "1993-11-14", "1993-11-15",
"1993-11-21", "1993-11-22", "1993-11-25", "1993-11-28", "1993-11-29",
"1993-12-05", "1993-12-06", "1993-12-11", "1993-12-12", "1993-12-13",
"1993-12-18", "1993-12-19", "1993-12-20", "1993-12-25", "1993-12-26",
"1993-12-27", "1993-12-31", "1994-01-02", "1994-01-03", "1994-01-08",
"1994-01-09", "1994-01-15", "1994-01-16", "1994-01-23", "1994-01-30",
"1994-09-04", "1994-09-05", "1994-09-11", "1994-09-12", "1994-09-18",
"1994-09-19", "1994-09-25", "1994-09-26", "1994-10-02", "1994-10-03",
"1994-10-09", "1994-10-10", "1994-10-13", "1994-10-16", "1994-10-17",
"1994-10-20", "1994-10-23", "1994-10-24", "1994-10-30", "1994-10-31",
"1994-11-06", "1994-11-07", "1994-11-13", "1994-11-14", "1994-11-20",
"1994-11-21", "1994-11-24", "1994-11-27", "1994-11-28", "1994-12-01",
"1994-12-04", "1994-12-05", "1994-12-10", "1994-12-11", "1994-12-12",
"1994-12-17", "1994-12-18", "1994-12-19", "1994-12-24", "1994-12-25",
"1994-12-26", "1994-12-31", "1995-01-01", "1995-01-07", "1995-01-08",
"1995-01-15", "1995-01-29", "1995-09-03", "1995-09-04", "1995-09-10",
"1995-09-11", "1995-09-17", "1995-09-18", "1995-09-24", "1995-09-25",
"1995-10-01", "1995-10-02", "1995-10-08", "1995-10-09", "1995-10-12",
"1995-10-15", "1995-10-16", "1995-10-19", "1995-10-22", "1995-10-23",
"1995-10-29", "1995-10-30", "1995-11-05", "1995-11-06", "1995-11-12",
"1995-11-13", "1995-11-19", "1995-11-20", "1995-11-23", "1995-11-26",
"1995-11-27", "1995-11-30", "1995-12-03", "1995-12-04", "1995-12-09",
"1995-12-10", "1995-12-11", "1995-12-16", "1995-12-17", "1995-12-18",
"1995-12-23", "1995-12-24", "1995-12-25", "1995-12-30", "1995-12-31",
"1996-01-06", "1996-01-07", "1996-01-14", "1996-01-28", "1996-09-01",
"1996-09-02", "1996-09-08", "1996-09-09", "1996-09-15", "1996-09-16",
"1996-09-22", "1996-09-23", "1996-09-29", "1996-09-30", "1996-10-06",
"1996-10-07", "1996-10-13", "1996-10-14", "1996-10-17", "1996-10-20",
"1996-10-21", "1996-10-27", "1996-10-28", "1996-11-03", "1996-11-04",
"1996-11-10", "1996-11-11", "1996-11-17", "1996-11-18", "1996-11-24",
"1996-11-25", "1996-11-28", "1996-12-01", "1996-12-02", "1996-12-05",
"1996-12-08", "1996-12-09", "1996-12-14", "1996-12-15", "1996-12-16",
"1996-12-21", "1996-12-22", "1996-12-23", "1996-12-28", "1996-12-29",
"1997-01-04", "1997-01-05", "1997-01-12", "1997-01-26", "1997-08-31",
"1997-09-01", "1997-09-07", "1997-09-08", "1997-09-14", "1997-09-15",
"1997-09-21", "1997-09-22", "1997-09-28", "1997-09-29", "1997-10-05",
"1997-10-06", "1997-10-12", "1997-10-13", "1997-10-16", "1997-10-19",
"1997-10-20", "1997-10-26", "1997-10-27", "1997-11-02", "1997-11-03",
"1997-11-09", "1997-11-10", "1997-11-16", "1997-11-17", "1997-11-23",
"1997-11-24", "1997-11-27", "1997-11-30", "1997-12-01", "1997-12-04",
"1997-12-07", "1997-12-08", "1997-12-13", "1997-12-14", "1997-12-15",
"1997-12-20", "1997-12-21", "1997-12-22", "1997-12-27", "1997-12-28",
"1998-01-03", "1998-01-04", "1998-01-11", "1998-01-25", "1998-09-06",
"1998-09-07", "1998-09-13", "1998-09-14", "1998-09-20", "1998-09-21",
"1998-09-27", "1998-09-28", "1998-10-04", "1998-10-05", "1998-10-11",
"1998-10-12", "1998-10-15", "1998-10-18", "1998-10-19", "1998-10-25",
"1998-10-26", "1998-11-01", "1998-11-02", "1998-11-08", "1998-11-09",
"1998-11-15", "1998-11-16", "1998-11-22", "1998-11-23", "1998-11-26",
"1998-11-29", "1998-11-30", "1998-12-03", "1998-12-06", "1998-12-07",
"1998-12-13", "1998-12-14", "1998-12-19", "1998-12-20", "1998-12-21",
"1998-12-26", "1998-12-27", "1998-12-28", "1999-01-02", "1999-01-03",
"1999-01-09", "1999-01-10", "1999-01-17", "1999-01-31", "1999-09-12",
"1999-09-13", "1999-09-19", "1999-09-20", "1999-09-26", "1999-09-27",
"1999-10-03", "1999-10-04", "1999-10-10", "1999-10-11", "1999-10-17",
"1999-10-18", "1999-10-21", "1999-10-24", "1999-10-25", "1999-10-31",
"1999-11-01", "1999-11-07", "1999-11-08", "1999-11-14", "1999-11-15",
"1999-11-21", "1999-11-22", "1999-11-25", "1999-11-28", "1999-11-29",
"1999-12-02", "1999-12-05", "1999-12-06", "1999-12-09", "1999-12-12",
"1999-12-13", "1999-12-18", "1999-12-19", "1999-12-20", "1999-12-24",
"1999-12-25", "1999-12-26", "1999-12-27", "2000-01-02", "2000-01-03",
"2000-01-08", "2000-01-09", "2000-01-15", "2000-01-16", "2000-01-23",
"2000-01-30", "2000-09-03", "2000-09-04", "2000-09-10", "2000-09-11",
"2000-09-17", "2000-09-18", "2000-09-24", "2000-09-25", "2000-10-01",
"2000-10-02", "2000-10-08", "2000-10-09", "2000-10-15", "2000-10-16",
"2000-10-19", "2000-10-22", "2000-10-23", "2000-10-29", "2000-10-30",
"2000-11-05", "2000-11-06", "2000-11-12", "2000-11-13", "2000-11-19",
"2000-11-20", "2000-11-23", "2000-11-26", "2000-11-27", "2000-11-30",
"2000-12-03", "2000-12-04", "2000-12-10", "2000-12-11", "2000-12-16",
"2000-12-17", "2000-12-18", "2000-12-23", "2000-12-24", "2000-12-25",
"2000-12-30", "2000-12-31", "2001-01-06", "2001-01-07", "2001-01-14",
"2001-01-28", "2001-09-09", "2001-09-10", "2001-09-23", "2001-09-24",
"2001-09-30", "2001-10-01", "2001-10-07", "2001-10-08", "2001-10-14",
"2001-10-15", "2001-10-18", "2001-10-21", "2001-10-22", "2001-10-25",
"2001-10-28", "2001-10-29", "2001-11-04", "2001-11-05", "2001-11-11",
"2001-11-12", "2001-11-18", "2001-11-19", "2001-11-22", "2001-11-25",
"2001-11-26", "2001-11-29", "2001-12-02", "2001-12-03", "2001-12-09",
"2001-12-10", "2001-12-15", "2001-12-16", "2001-12-17", "2001-12-22",
"2001-12-23", "2001-12-29", "2001-12-30", "2002-01-06", "2002-01-07",
"2002-01-12", "2002-01-13", "2002-01-19", "2002-01-20", "2002-01-27",
"2002-02-03", "2002-09-05", "2002-09-08", "2002-09-09", "2002-09-15",
"2002-09-16", "2002-09-22", "2002-09-23", "2002-09-29", "2002-09-30",
"2002-10-06", "2002-10-07", "2002-10-13", "2002-10-14", "2002-10-20",
"2002-10-21", "2002-10-27", "2002-10-28", "2002-11-03", "2002-11-04",
"2002-11-10", "2002-11-11", "2002-11-17", "2002-11-18", "2002-11-24",
"2002-11-25", "2002-11-28", "2002-12-01", "2002-12-02", "2002-12-08",
"2002-12-09", "2002-12-15", "2002-12-16", "2002-12-21", "2002-12-22",
"2002-12-23", "2002-12-28", "2002-12-29", "2002-12-30", "2003-01-04",
"2003-01-05", "2003-01-11", "2003-01-12", "2003-01-19", "2003-01-26",
"2003-09-04", "2003-09-07", "2003-09-08", "2003-09-14", "2003-09-15",
"2003-09-21", "2003-09-22", "2003-09-28", "2003-09-29", "2003-10-05",
"2003-10-06", "2003-10-12", "2003-10-13", "2003-10-19", "2003-10-20",
"2003-10-26", "2003-10-27", "2003-11-02", "2003-11-03", "2003-11-09",
"2003-11-10", "2003-11-16", "2003-11-17", "2003-11-23", "2003-11-24",
"2003-11-27", "2003-11-30", "2003-12-01", "2003-12-07", "2003-12-08",
"2003-12-14", "2003-12-15", "2003-12-20", "2003-12-21", "2003-12-22",
"2003-12-27", "2003-12-28", "2004-01-03", "2004-01-04", "2004-01-10",
"2004-01-11", "2004-01-18", "2004-02-01", "2004-09-09", "2004-09-11",
"2004-09-12", "2004-09-13", "2004-09-19", "2004-09-20", "2004-09-26",
"2004-09-27", "2004-10-03", "2004-10-04", "2004-10-10", "2004-10-11",
"2004-10-17", "2004-10-18", "2004-10-24", "2004-10-25", "2004-10-31",
"2004-11-01", "2004-11-07", "2004-11-08", "2004-11-14", "2004-11-15",
"2004-11-21", "2004-11-22", "2004-11-25", "2004-11-28", "2004-11-29",
"2004-12-05", "2004-12-06", "2004-12-12", "2004-12-13", "2004-12-18",
"2004-12-19", "2004-12-20", "2004-12-24", "2004-12-25", "2004-12-26",
"2004-12-27", "2005-01-02", "2005-01-08", "2005-01-09", "2005-01-15",
"2005-01-16", "2005-01-23", "2005-02-06", "2005-09-08", "2005-09-11",
"2005-09-12", "2005-09-18", "2005-09-19", "2005-09-25", "2005-09-26",
"2005-10-02", "2005-10-03", "2005-10-09", "2005-10-10", "2005-10-16",
"2005-10-17", "2005-10-21", "2005-10-23", "2005-10-24", "2005-10-30",
"2005-10-31", "2005-11-06", "2005-11-07", "2005-11-13", "2005-11-14",
"2005-11-20", "2005-11-21", "2005-11-24", "2005-11-27", "2005-11-28",
"2005-12-04", "2005-12-05", "2005-12-11", "2005-12-12", "2005-12-17",
"2005-12-18", "2005-12-19", "2005-12-24", "2005-12-25", "2005-12-26",
"2005-12-31", "2006-01-01", "2006-01-07", "2006-01-08", "2006-01-14",
"2006-01-15", "2006-01-22", "2006-02-05", "2006-09-07", "2006-09-10",
"2006-09-11", "2006-09-17", "2006-09-18", "2006-09-24", "2006-09-25",
"2006-10-01", "2006-10-02", "2006-10-08", "2006-10-09", "2006-10-15",
"2006-10-16", "2006-10-22", "2006-10-23", "2006-10-29", "2006-10-30",
"2006-11-05", "2006-11-06", "2006-11-12", "2006-11-13", "2006-11-19",
"2006-11-20", "2006-11-23", "2006-11-26", "2006-11-27", "2006-11-30",
"2006-12-03", "2006-12-04", "2006-12-07", "2006-12-10", "2006-12-11",
"2006-12-14", "2006-12-16", "2006-12-17", "2006-12-18", "2006-12-21",
"2006-12-23", "2006-12-24", "2006-12-25", "2006-12-30", "2006-12-31",
"2007-01-06", "2007-01-07", "2007-01-13", "2007-01-14", "2007-01-21",
"2007-02-04", "2007-09-06", "2007-09-09", "2007-09-10", "2007-09-16",
"2007-09-17", "2007-09-23", "2007-09-24", "2007-09-30", "2007-10-01",
"2007-10-07", "2007-10-08", "2007-10-14", "2007-10-15", "2007-10-21",
"2007-10-22", "2007-10-28", "2007-10-29", "2007-11-04", "2007-11-05",
"2007-11-11", "2007-11-12", "2007-11-18", "2007-11-19", "2007-11-22",
"2007-11-25", "2007-11-26", "2007-11-29", "2007-12-02", "2007-12-03",
"2007-12-06", "2007-12-09", "2007-12-10", "2007-12-13", "2007-12-15",
"2007-12-16", "2007-12-17", "2007-12-20", "2007-12-22", "2007-12-23",
"2007-12-24", "2007-12-29", "2007-12-30", "2008-01-05", "2008-01-06",
"2008-01-12", "2008-01-13", "2008-01-20", "2008-02-03", "2008-09-04",
"2008-09-07", "2008-09-08", "2008-09-14", "2008-09-15", "2008-09-21",
"2008-09-22", "2008-09-28", "2008-09-29", "2008-10-05", "2008-10-06",
"2008-10-12", "2008-10-13", "2008-10-19", "2008-10-20", "2008-10-26",
"2008-10-27", "2008-11-02", "2008-11-03", "2008-11-06", "2008-11-09",
"2008-11-10", "2008-11-13", "2008-11-16", "2008-11-17", "2008-11-20",
"2008-11-23", "2008-11-24", "2008-11-27", "2008-11-30", "2008-12-01",
"2008-12-04", "2008-12-07", "2008-12-08", "2008-12-11", "2008-12-14",
"2008-12-15", "2008-12-18", "2008-12-20", "2008-12-21", "2008-12-22",
"2008-12-28", "2009-01-03", "2009-01-04", "2009-01-10", "2009-01-11",
"2009-01-18", "2009-02-01", "2009-09-10", "2009-09-13", "2009-09-14",
"2009-09-20", "2009-09-21", "2009-09-27", "2009-09-28", "2009-10-04",
"2009-10-05", "2009-10-11", "2009-10-12", "2009-10-18", "2009-10-19",
"2009-10-25", "2009-10-26", "2009-11-01", "2009-11-02", "2009-11-08",
"2009-11-09", "2009-11-12", "2009-11-15", "2009-11-16", "2009-11-19",
"2009-11-22", "2009-11-23", "2009-11-26", "2009-11-29", "2009-11-30",
"2009-12-03", "2009-12-06", "2009-12-07", "2009-12-10", "2009-12-13",
"2009-12-14", "2009-12-17", "2009-12-19", "2009-12-20", "2009-12-21",
"2009-12-25", "2009-12-27", "2009-12-28", "2010-01-03", "2010-01-09",
"2010-01-10", "2010-01-16", "2010-01-17", "2010-01-24", "2010-02-07",
"2010-09-09", "2010-09-12", "2010-09-13", "2010-09-19", "2010-09-20",
"2010-09-26", "2010-09-27", "2010-10-03", "2010-10-04", "2010-10-10",
"2010-10-11", "2010-10-17", "2010-10-18", "2010-10-24", "2010-10-25",
"2010-10-31", "2010-11-01", "2010-11-07", "2010-11-08", "2010-11-11",
"2010-11-14", "2010-11-15", "2010-11-18", "2010-11-21", "2010-11-22",
"2010-11-25", "2010-11-28", "2010-11-29", "2010-12-02", "2010-12-05",
"2010-12-06", "2010-12-09", "2010-12-12", "2010-12-13", "2010-12-16",
"2010-12-19", "2010-12-20", "2010-12-23", "2010-12-25", "2010-12-26",
"2010-12-27", "2010-12-28", "2011-01-02", "2011-01-08", "2011-01-09",
"2011-01-15", "2011-01-16", "2011-01-23", "2011-02-06"), class = "factor"),
Season = c(1991, 1991, 1991, 1991, 1991, 1991), HomeRecord = structure(c(1L,
1L, 17L, 17L, 17L, 1L), .Label = c("(0-1-0)", "(0-10-0)",
"(0-11-0)", "(0-12-0)", "(0-13-0)", "(0-14-0)", "(0-15-0)",
"(0-16-0)", "(0-2-0)", "(0-3-0)", "(0-4-0)", "(0-5-0)", "(0-6-0)",
"(0-7-0)", "(0-8-0)", "(0-9-0)", "(1-0-0)", "(1-1-0)", "(1-10-0)",
"(1-10-1)", "(1-11-0)", "(1-11-1)", "(1-12-0)", "(1-13-0)",
"(1-14-0)", "(1-15-0)", "(1-2-0)", "(1-3-0)", "(1-4-0)",
"(1-5-0)", "(1-6-0)", "(1-7-0)", "(1-8-0)", "(1-8-1)", "(1-9-0)",
"(1-9-1)", "(10-0-0)", "(10-1-0)", "(10-2-0)", "(10-3-0)",
"(10-4-0)", "(10-5-0)", "(10-5-1)", "(10-6-0)", "(10-6-1)",
"(10-7-0)", "(10-7-1)", "(10-8-0)", "(11-0-0)", "(11-1-0)",
"(11-2-0)", "(11-3-0)", "(11-4-0)", "(11-5-0)", "(11-5-1)",
"(11-6-0)", "(11-6-1)", "(11-7-0)", "(11-7-1)", "(11-8-0)",
"(12-0-0)", "(12-1-0)", "(12-2-0)", "(12-3-0)", "(12-4-0)",
"(12-5-0)", "(12-6-0)", "(12-7-0)", "(12-8-0)", "(13-0-0)",
"(13-1-0)", "(13-2-0)", "(13-3-0)", "(13-4-0)", "(13-5-0)",
"(13-6-0)", "(14-0-0)", "(14-1-0)", "(14-2-0)", "(14-3-0)",
"(14-4-0)", "(14-5-0)", "(14-6-0)", "(15-0-0)", "(15-1-0)",
"(15-2-0)", "(15-3-0)", "(15-4-0)", "(15-5-0)", "(16-0-0)",
"(16-1-0)", "(16-2-0)", "(16-3-0)", "(16-4-0)", "(17-0-0)",
"(17-2-0)", "(18-0-0)", "(18-1-0)", "(2-0-0)", "(2-1-0)",
"(2-10-0)", "(2-11-0)", "(2-11-1)", "(2-12-0)", "(2-13-0)",
"(2-14-0)", "(2-2-0)", "(2-3-0)", "(2-4-0)", "(2-5-0)", "(2-6-0)",
"(2-7-0)", "(2-8-0)", "(2-9-0)", "(3-0-0)", "(3-1-0)", "(3-10-0)",
"(3-11-0)", "(3-11-1)", "(3-12-0)", "(3-13-0)", "(3-2-0)",
"(3-3-0)", "(3-4-0)", "(3-5-0)", "(3-6-0)", "(3-7-0)", "(3-8-0)",
"(3-9-0)", "(4-0-0)", "(4-1-0)", "(4-10-0)", "(4-11-0)",
"(4-11-1)", "(4-12-0)", "(4-2-0)", "(4-3-0)", "(4-4-0)",
"(4-5-0)", "(4-6-0)", "(4-6-1)", "(4-7-0)", "(4-7-1)", "(4-8-0)",
"(4-8-1)", "(4-9-0)", "(5-0-0)", "(5-1-0)", "(5-10-0)", "(5-11-0)",
"(5-2-0)", "(5-3-0)", "(5-3-1)", "(5-4-0)", "(5-4-1)", "(5-5-0)",
"(5-5-1)", "(5-6-0)", "(5-6-1)", "(5-7-0)", "(5-8-0)", "(5-8-1)",
"(5-9-0)", "(6-0-0)", "(6-1-0)", "(6-10-0)", "(6-2-0)", "(6-3-0)",
"(6-3-1)", "(6-4-0)", "(6-4-1)", "(6-5-0)", "(6-5-1)", "(6-6-0)",
"(6-6-1)", "(6-7-0)", "(6-7-1)", "(6-8-0)", "(6-8-1)", "(6-9-0)",
"(6-9-1)", "(7-0-0)", "(7-1-0)", "(7-2-0)", "(7-3-0)", "(7-3-1)",
"(7-4-0)", "(7-4-1)", "(7-5-0)", "(7-5-1)", "(7-6-0)", "(7-6-1)",
"(7-7-0)", "(7-7-1)", "(7-8-0)", "(7-9-0)", "(8-0-0)", "(8-1-0)",
"(8-10-0)", "(8-2-0)", "(8-3-0)", "(8-3-1)", "(8-4-0)", "(8-4-1)",
"(8-5-0)", "(8-5-1)", "(8-6-0)", "(8-6-1)", "(8-7-0)", "(8-7-1)",
"(8-8-0)", "(8-9-0)", "(9-0-0)", "(9-1-0)", "(9-2-0)", "(9-3-0)",
"(9-4-0)", "(9-5-0)", "(9-5-1)", "(9-6-0)", "(9-6-1)", "(9-7-0)",
"(9-8-0)", "(9-9-0)"), class = "factor"), AwayRecord = structure(c(17L,
17L, 1L, 1L, 1L, 17L), .Label = c("(0-1-0)", "(0-10-0)",
"(0-11-0)", "(0-12-0)", "(0-13-0)", "(0-14-0)", "(0-15-0)",
"(0-16-0)", "(0-2-0)", "(0-3-0)", "(0-4-0)", "(0-5-0)", "(0-6-0)",
"(0-7-0)", "(0-8-0)", "(0-9-0)", "(1-0-0)", "(1-1-0)", "(1-10-0)",
"(1-10-1)", "(1-11-0)", "(1-11-1)", "(1-12-0)", "(1-13-0)",
"(1-14-0)", "(1-15-0)", "(1-2-0)", "(1-3-0)", "(1-4-0)",
"(1-5-0)", "(1-6-0)", "(1-7-0)", "(1-8-0)", "(1-8-1)", "(1-9-0)",
"(1-9-1)", "(10-0-0)", "(10-1-0)", "(10-2-0)", "(10-3-0)",
"(10-4-0)", "(10-5-0)", "(10-5-1)", "(10-6-0)", "(10-6-1)",
"(10-7-0)", "(10-7-1)", "(10-8-0)", "(11-0-0)", "(11-1-0)",
"(11-2-0)", "(11-3-0)", "(11-4-0)", "(11-5-0)", "(11-5-1)",
"(11-6-0)", "(11-6-1)", "(11-7-0)", "(11-7-1)", "(11-8-0)",
"(12-0-0)", "(12-1-0)", "(12-2-0)", "(12-3-0)", "(12-4-0)",
"(12-5-0)", "(12-6-0)", "(12-7-0)", "(12-8-0)", "(13-0-0)",
"(13-1-0)", "(13-2-0)", "(13-3-0)", "(13-4-0)", "(13-5-0)",
"(13-6-0)", "(14-0-0)", "(14-1-0)", "(14-2-0)", "(14-3-0)",
"(14-4-0)", "(14-5-0)", "(14-6-0)", "(15-0-0)", "(15-1-0)",
"(15-2-0)", "(15-3-0)", "(15-4-0)", "(15-5-0)", "(16-0-0)",
"(16-1-0)", "(16-2-0)", "(16-3-0)", "(16-4-0)", "(17-0-0)",
"(17-2-0)", "(18-0-0)", "(18-1-0)", "(2-0-0)", "(2-1-0)",
"(2-10-0)", "(2-11-0)", "(2-11-1)", "(2-12-0)", "(2-13-0)",
"(2-14-0)", "(2-2-0)", "(2-3-0)", "(2-4-0)", "(2-5-0)", "(2-6-0)",
"(2-7-0)", "(2-8-0)", "(2-9-0)", "(3-0-0)", "(3-1-0)", "(3-10-0)",
"(3-11-0)", "(3-11-1)", "(3-12-0)", "(3-13-0)", "(3-2-0)",
"(3-3-0)", "(3-4-0)", "(3-5-0)", "(3-6-0)", "(3-7-0)", "(3-8-0)",
"(3-9-0)", "(4-0-0)", "(4-1-0)", "(4-10-0)", "(4-11-0)",
"(4-11-1)", "(4-12-0)", "(4-2-0)", "(4-3-0)", "(4-4-0)",
"(4-5-0)", "(4-6-0)", "(4-6-1)", "(4-7-0)", "(4-7-1)", "(4-8-0)",
"(4-8-1)", "(4-9-0)", "(5-0-0)", "(5-1-0)", "(5-10-0)", "(5-11-0)",
"(5-2-0)", "(5-3-0)", "(5-3-1)", "(5-4-0)", "(5-4-1)", "(5-5-0)",
"(5-5-1)", "(5-6-0)", "(5-6-1)", "(5-7-0)", "(5-8-0)", "(5-8-1)",
"(5-9-0)", "(6-0-0)", "(6-1-0)", "(6-10-0)", "(6-2-0)", "(6-3-0)",
"(6-3-1)", "(6-4-0)", "(6-4-1)", "(6-5-0)", "(6-5-1)", "(6-6-0)",
"(6-6-1)", "(6-7-0)", "(6-7-1)", "(6-8-0)", "(6-8-1)", "(6-9-0)",
"(6-9-1)", "(7-0-0)", "(7-1-0)", "(7-2-0)", "(7-3-0)", "(7-3-1)",
"(7-4-0)", "(7-4-1)", "(7-5-0)", "(7-5-1)", "(7-6-0)", "(7-6-1)",
"(7-7-0)", "(7-7-1)", "(7-8-0)", "(7-9-0)", "(8-0-0)", "(8-1-0)",
"(8-10-0)", "(8-2-0)", "(8-3-0)", "(8-3-1)", "(8-4-0)", "(8-4-1)",
"(8-5-0)", "(8-5-1)", "(8-6-0)", "(8-6-1)", "(8-7-0)", "(8-7-1)",
"(8-8-0)", "(8-9-0)", "(9-0-0)", "(9-1-0)", "(9-2-0)", "(9-3-0)",
"(9-4-0)", "(9-5-0)", "(9-5-1)", "(9-6-0)", "(9-6-1)", "(9-7-0)",
"(9-8-0)", "(9-9-0)"), class = "factor"), HomeFinal = c(14L,
14L, 45L, 35L, 10L, 7L), AwayFinal = c(26L, 24L, 0L, 31L,
6L, 16L), HomeLast = c(4, 4, 5, 5, 0, 7), AwayLast = c(6,
4, 0, 1, 6, 6), Winner = c("Away", "Away", "Home", "Home",
"Home", "Away")), .Names = c("X1", "HomeTeam", "AwayTeam",
"Date", "Season", "HomeRecord", "AwayRecord", "HomeFinal", "AwayFinal",
"HomeLast", "AwayLast", "Winner"), row.names = c(NA, 6L), class = "data.frame")
Then you would do.
ggfluctuation(table(gamesWide$HomeLast, gamesWide$AwayLast), type="colour") + labs(x="Away", y="Home") + opts(title="Distribution of Last Digit of Score")
To get
Of course, that image was generated using the full dataset. This should be further extensible to data that isn't so symmetric and rectangular.

Related

Limit the number of joined rows in Kusto / KQL / ADX

Given the following Kusto query, is it possible to limit the result set so only the two cities with highest population per country are retrieved?
My real scenario is for sure a lot more complex but I've spent several hours now to figure out how I could do this. I tried with the top-nested operator but this operator actually changes the column layout by aggregating on a single column and not just reduces the amount of fetched rows by grouping criteria.
let population=datatable (name: string, population: int64) [
"New York", 4478934739,
"Washington DC", 412165236,
"Miami", 124437843,
"Berlin", 222347384,
"Munich", 6783434,
"Hamburg", 6000033
];
let country=datatable (name: string, country: string) [
"New York", "US",
"Washington DC", "US",
"Miami", "US",
"Berlin", "DE",
"Munich", "DE",
"Hamburg", "DE"
];
population
| join kind=inner country on name
Would this work?
Note that the partition operator is currently limited to 64 values (this is a temporary limitation)
let Populations=datatable (name: string, population: int64) [
"New York", 4478934739,
"Washington DC", 412165236,
"Miami", 124437843,
"Berlin", 222347384,
"Munich", 6783434,
"Hamburg", 6000033
];
let Countries=datatable (name: string, country: string) [
"New York", "US",
"Washington DC", "US",
"Miami", "US",
"Berlin", "DE",
"Munich", "DE",
"Hamburg", "DE"
];
Countries
| partition by country(
lookup Populations on name
| top 2 by population
)
If you can't use partition due to the number of partitions limitation here is an alternative:
let Populations=datatable (name: string, population: int64) [
"New York", 4478934739,
"Washington DC", 412165236,
"Miami", 124437843,
"Berlin", 222347384,
"Munich", 6783434,
"Hamburg", 6000033
];
let Countries=datatable (name: string, country: string) [
"New York", "US",
"Washington DC", "US",
"Miami", "US",
"Berlin", "DE",
"Munich", "DE",
"Hamburg", "DE"
];
Countries
| lookup Populations on name
| order by country, population desc
| extend rn = row_number(0, country != prev(country))
| where rn <=1
| project country, name, population

Filtering Data Frame by Multiple Criteria Based on Separate Index Vector

I have a data frame with multiple column fields of data one of which is an index variable. It's essentially multiple time-series of transactions related to a particular unique identifier (the separate index vector). I'd like to filter this data frame provided the following criteria are met:
Time elapsed (in days) between the first entry for the corresponding index (top being most recent date) being less than 90 days and the transaction type being either a P-Purchase if the first entry is an S-Sale or vice versa (if Sale and then Purchase).
I'm not sure if I should use an If else statement or dplyr's case.when method so I'm struggling with how to solve this.
Here is a sample of the scripts of my work (which is generating an error) using either if else or dplyr:
With for loop and dplyr
for (i in length(ciknumbers2)) {
data10 <- if(InsiderList3$`Insider CIK` == ciknumbers2[i])
filter(head(InsiderList3$`Transaction Date`,1)-InsiderList3$`Transaction Date`< 90 &
head(InsiderList3$`Transaction Type`,1) != InsiderList3$`Transaction Type`)
}
- Just dplyr
filt_data <- InsiderList3 %>%
filter(
if (`Insider CIK` == ciknumbers2) {
head(InsiderList3$`Transaction Date`,1)-InsiderList3$`Transaction Date`<90 & head(InsiderList3$`Transaction Type`,1) != InsiderList3$`Transaction Type`
} else {
}
) %>%
mutate(totalrows = nrow(.)) %>%
summarize()
InsiderList3 %>%
filter(case_when(`Insider CIK` == ciknumbers2,
head(InsiderList3$`Transaction Date`,1)-InsiderList3$`Transaction Date`< 90,
head(InsiderList3$`Transaction Type`,1) != InsiderList3$`Transaction Type`
)) %>%
tail(1)
The index list is:
"0001337645" "0001749420" "0001658704" "0001642765" "000852412" "0001499263" "0001769077" "0001239635" "0001790576" "0001198046"
And the data frame looks like this:
Help constructing a solution under either method would be much appreciated.
Update with Sample dput
dput(InsiderList5)
structure(list(`Insider CIK` = c("0001337645", "0001337645",
"0001337645", "0001337645", "0001337645", "0001337645", "0001337645",
"0001337645", "0001337645"), `Insider Full Name and CIK` = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Bachmann Lisa M (0001337645)",
"Robins Ronald A Jr (0001499263)", "Strub Robert (0001769077)",
"Mueller Andrej (0001790576)", "Schlonsky Michael Allen (0001557088)",
"SRS Investment Management, LLC (0001503174)", "JAMES MICHAEL C (0001365831)",
"THORN BRUCE K (0001215178)", "Ramsden Jonathan E (0001451778)",
"PATEL GOYAL KRUTI (0001770886)", "Asmar Christian (0001787532)",
"Bergman Artur (0001769490)", "Castillo Kirsten (0001808806)",
"Andres Juan (0001760670)", "Jolson Joseph A (0001398113)", "Wilson Jan C. (0001807821)",
"Beeler Brian K (0001641580)", "Magnuson Lee A (0001726150)",
"Fernandes Prabhavathi (0001540883)", "Osbourn William F Jr (0001421529)",
"Morno-Wade Suzan (0001761233)", "Mancini Joseph H. (0001573159)",
"Pasquale Maria E (0001736295)", "STERN ADAM K (0001403497)",
"Heiss Xavier (0001803559)", "Feldman Michael David (0001693649)",
"Dellovo Victor (0001484174)", "Dhanak Dashyant (0001760682)",
"Robinson Douglas (0001383267)", "Barry Corie S (0001676999)",
"KLAERNER GERRIT (0001743681)", "GAY JOHN M. (0001765684)", "Tiscornia Anthony D (0001707972)",
"Nefkens Michael G (0001566846)", "Shulkin David (0001808395)",
"STAFFORD JOHN S III (0001218981)", "Nelson Connia M (0001767555)",
"Boyd Steven (0001706140)", "ARMISTICE CAPITAL, LLC (0001601086)",
"LAROCHE RICHARD F JR (0001012859)", "Holloman James Phillip (0001424987)"
), class = "factor"), `Acquistion or Disposition` = structure(c(1L,
1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L), .Label = c("A", "D", "-"), class = "factor"),
`Transaction Date` = structure(c(18358, 18353, 18353, 18333,
18332, 18332, 18330, 18290, 18155), class = "Date"), `Deemed Execution Date` = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Â", "2020-01-02"
), class = "factor"), Issuer = structure(c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 1L), .Label = c("BIG LOTS INC", "GMS Inc.",
"ABERCROMBIE & FITCH CO /DE/", "GREEN DOT CORP", "AVIS BUDGET GROUP, INC.",
"GUIDED THERAPEUTICS INC", "Terra Tech Corp.", "MENS WEARHOUSE INC",
"PETSMART INC", "TAILORED BRANDS INC", "ETSY INC", "AVID TECHNOLOGY, INC.",
"Fastly, Inc.", "Ocugen, Inc.", "Avantor, Inc.", "Evelo Biosciences, Inc.",
"Moderna, Inc.", "Harvest Capital Credit Corp", "JMP GROUP LLC",
"Spartan Energy Acquisition Corp.", "Horizon Pharma plc",
"Horizon Therapeutics Public Ltd Co", "RAVEN INDUSTRIES INC",
"Cempra Holdings, LLC", "CEMPRA, INC.", "TIME WARNER CABLE INC.",
"XEROX CORP", "Xerox Holdings Corp", "INCYTE CORP", "DarioHealth Corp.",
"INVIVO THERAPEUTICS HOLDINGS CORP.", "LabStyle Innovations Corp.",
"Matinas BioPharma Holdings, Inc.", "Modigene Inc.", "ORGANOVO HOLDINGS, INC.",
"PROLOR Biotech, Inc.", "CSP INC /MA/", "VERINT SYSTEMS INC",
"BEST BUY CO INC", "DOMINOS PIZZA INC", "Tricida, Inc.",
"Novan, Inc.", "Coupa Software Inc", "DXC Technology Co",
"Hewlett Packard Enterprise Co", "RESIDEO TECHNOLOGIES, INC.",
"ORASURE TECHNOLOGIES INC", "AWARE INC /MA/", "Xencor Inc",
"Hudson Global, Inc.", "Cerecor Inc.", "AMAG PHARMACEUTICALS, INC.",
"AYTU BIOSCIENCE, INC", "EYEGATE PHARMACEUTICALS INC", "INNOVUS PHARMACEUTICALS, INC.",
"ReShape Lifesciences Inc.", "CROSS BORDER RESOURCES, INC.",
"NATIONAL HEALTHCARE CORP", "CINTAS CORP", "ROCKWELL AUTOMATION INC",
"ROCKWELL AUTOMATION, INC"), class = "factor"), Form = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("4", "3", "5",
"4/A", "3/A"), class = "factor"), `Transaction Type` = structure(c(1L,
1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L), .Label = c("A-Award", "F-InKind",
"M-Exempt", "S-Sale", "-", "G-Gift", "W-Will", "J-Other",
"P-Purchase", "X-InTheMoney", "D-Return", "C-Conversion",
"I-Discretionary"), class = "factor"), `Direct or Indirect Ownership` = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("--D", "-ED",
"--I", "1-I", "---", "-EI"), class = "factor"), `Number of Securities Transacted` = structure(c(44L,
49L, 34L, 54L, 16L, 30L, 17L, 53L, 51L), .Label = c("10133.0000",
"10500.0000", "11165.0000", "11651.0000", "12559.0000", "12962.0000",
"14200.0000", "15452.0000", "16563.0000", "17386.0000", "17500.0000",
"17574.0000", "18349.0000", "18835.0000", "18917.0000", "19453.0000",
"1976.0000", "1988.0000", "20396.0000", "20614.0000", "2156.0000",
"2169.0000", "2231.0000", "22343.0000", "24000.0000", "24150.0000",
"24750.0000", "2750.0000", "2751.0000", "2912.0000", "29733.0000",
"2986.0000", "30643.0000", "3166.0000", "3228.0000", "3326.0000",
"3502.0000", "3610.0000", "36650.0000", "37831.0000", "4136.0000",
"4499.0000", "4536.0000", "46634.0000", "50000.0000", "5800.0000",
"6048.0000", "6479.0000", "6980.0000", "7517.0000", "8579.0000",
"9241.0000", "941.0000", "9488.0000", "9538.0000", "", "1000.0000",
"10000.0000", "1009.0000", "11714.0000", "1200.0000", "1398.0000",
"1405.0000", "1483.0000", "17482.0000", "194.0000", "198.0000",
"2000.0000", "2213.0000", "250.0000", "2885.0000", "2957.0000",
"29636.0000", "3662.0000", "395.0000", "400.0000", "4400.0000",
"4709.0000", "5000.0000", "5200.0000", "5474.0000", "566.0000",
"600.0000", "6000.0000", "603.0000", "650.0000", "6801.0000",
"6841.0000", "7000.0000", "784.0000", "9503.0000", "9740.0000",
"1457.0000", "20000.0000", "33208.0000", "40000.0000", "4603.0000",
"526.0000", "6348.0000", "8037.0000", "16753.0000", "26672.0000",
"100.0000", "1048.0000", "1073.0000", "1080.0000", "10962.0000",
"11349.0000", "1212.0000", "1249.0000", "12698.0000", "1314.0000",
"1354.0000", "1358.0000", "14900.0000", "15000.0000", "15765.0000",
"1607.0000", "1668.0000", "17103.0000", "1748.0000", "20003.0000",
"2067.0000", "2092.0000", "2158.0000", "2398.0000", "2496.0000",
"2602.0000", "3002.0000", "30442.0000", "3057.0000", "3183.0000",
"3750.0000", "4102.0000", "4200.0000", "4500.0000", "4556.0000",
"4785.0000", "4972.0000", "5238.0000", "6161.0000", "6600.0000",
"675.0000", "679.0000", "686.0000", "8201.0000", "8461.0000",
"9518.0000", "1.0000", "1070300.0000", "1122700.0000", "1376795.0000",
"1588800.0000", "1621445.0000", "213321.0000", "288439.0000",
"3500.0000", "3500000.0000", "400000.0000", "4189300.0000",
"500000.0000", "1100.0000", "11500.0000", "120000.0000",
"1200000.0000", "12500.0000", "125000.0000", "134.0000",
"1500.0000", "150000.0000", "1500000.0000", "151162.0000",
"160000.0000", "185724.0000", "190000.0000", "1935.0000",
"200000.0000", "2000000.0000", "215196.0000", "240.0000",
"244133.0000", "2500.0000", "250000.0000", "27320.0000",
"2900.0000", "3100.0000", "3300.0000", "35000.0000", "3900.0000",
"4000.0000", "450000.0000", "4600.0000", "490196.0000", "500.0000",
"51282.0000", "5300.0000", "5400.0000", "54429.0000", "54430.0000",
"5500.0000", "58168.0000", "600000.0000", "6100.0000", "64599.0000",
"6500.0000", "67680.0000", "7100.0000", "7560.0000", "7565.0000",
"76865.0000", "800000.0000", "900000.0000", "91465.0000",
"9700.0000", "10010.0000", "105135.0000", "1069.0000", "115911.0000",
"12.0000", "12213.0000", "1452.0000", "16812.0000", "17895.0000",
"1860.0000", "19050.0000", "1961.0000", "19653.0000", "21046.0000",
"2138.0000", "21600.0000", "21815.0000", "219.0000", "23235.0000",
"2351.0000", "2721.0000", "2777.0000", "2981.0000", "31026.0000",
"35211.0000", "3565.0000", "40585.0000", "4509.0000", "48351.0000",
"4938.0000", "49926.0000", "5352.0000", "5644.0000", "6354.0000",
"651.0000", "6732.0000", "68634.0000", "6874.0000", "709.0000",
"7236.0000", "724.0000", "73.0000", "78170.0000", "7844.0000",
"811.0000", "8500.0000", "8535.0000", "9505.0000", "9509.0000",
"9826.0000", "9827.0000", "1278.0000", "140000.0000", "1475.0000",
"1481.0000", "1838.0000", "1871.0000", "2002.0000", "20439.0000",
"2143.0000", "23202.0000", "2611.0000", "2775.0000", "3028.0000",
"3031.0000", "3032.0000", "320.0000", "3356.0000", "36000.0000",
"3656.0000", "3705.0000", "3748.0000", "40438.0000", "41491.0000",
"4250.0000", "5375.0000", "5681.0000", "5750.0000", "60000.0000",
"639.0000", "67500.0000", "67568.0000", "70000.0000", "7500.0000",
"8707.0000", "8709.0000", "92807.0000", "10417.0000", "1061.0000",
"1088.0000", "1198.0000", "1199.0000", "123.0000", "1433.0000",
"1590.0000", "1604.0000", "1641.0000", "1930.0000", "1931.0000",
"2022.0000", "2023.0000", "2055.0000", "2311.0000", "276.0000",
"2901.0000", "32412.0000", "3398.0000", "3399.0000", "3400.0000",
"3408.0000", "346.0000", "357.0000", "3653.0000", "4159.0000",
"4160.0000", "459.0000", "525.0000", "530.0000", "55316.0000",
"593.0000", "669.0000", "720.0000", "721.0000", "774.0000",
"999.0000", "106447.0000", "11874.0000", "13759.0000", "150.0000",
"153060.0000", "15888.0000", "1652.0000", "180.0000", "19477.0000",
"2216.0000", "231.0000", "28859.0000", "32309.0000", "35449.0000",
"6790.0000", "8960.0000", "9045.0000", "10524.0000", "109027.0000",
"11250.0000", "11900.0000", "11976.0000", "14500.0000", "17023.0000",
"1828.0000", "200.0000", "22500.0000", "25741.0000", "2626.0000",
"26759.0000", "3350.0000", "3550.0000", "36527.0000", "38473.0000",
"450.0000", "4874.0000", "50403.0000", "7246.0000", "7400.0000",
"7754.0000", "9422.0000", "54000.0000", "10006.0000", "104.0000",
"11319.0000", "13697.0000", "15281.0000", "161120.0000",
"16317.0000", "2046.0000", "2188.0000", "254396.0000", "28924.0000",
"300.0000", "3001.0000", "31380.0000", "3272.0000", "3632.0000",
"5581.0000", "7094.0000", "730.0000", "8006.0000", "8187.0000",
"1001.0000", "10611.0000", "10744.0000", "12695.0000", "1300.0000",
"13462.0000", "1400.0000", "1435.0000", "14964.0000", "16575.0000",
"1700.0000", "1713.0000", "1799.0000", "1901.0000", "1948.0000",
"2134.0000", "2294.0000", "2400.0000", "25359.0000", "3000.0000",
"312.0000", "3150.0000", "31513.0000", "34100.0000", "3510.0000",
"3600.0000", "3699.0000", "3710.0000", "3810.0000", "4038.0000",
"40969.0000", "4100.0000", "4477.0000", "4523.0000", "4524.0000",
"4597.0000", "4799.0000", "5129.0000", "5150.0000", "5324.0000",
"5379.0000", "5600.0000", "58355.0000", "5900.0000", "6078.0000",
"6388.0000", "6808.0000", "6966.0000", "700.0000", "703.0000",
"7272.0000", "7300.0000", "7506.0000", "7920.0000", "794.0000",
"8300.0000", "8900.0000", "9118.0000", "37500.0000", "10232.0000",
"11200.0000", "1204.0000", "12260.0000", "153000.0000", "17259.0000",
"17260.0000", "17931.0000", "1825.0000", "19014.0000", "20465.0000",
"21690.0000", "2173.0000", "2370.0000", "2784.0000", "29027.0000",
"29520.0000", "2955.0000", "297.0000", "3200.0000", "36782.0000",
"3700.0000", "3741.0000", "3871.0000", "4119.0000", "4209.0000",
"4534.0000", "48000.0000", "4903.0000", "4969.0000", "51779.0000",
"558000.0000", "6545.0000", "6716.0000", "7962.0000", "8000.0000",
"8240.0000", "8425.0000", "8775.0000", "8888.0000", "9500.0000",
"9810.0000", "1025.0000", "1140.0000", "2030.0000", "2281.0000",
"100000.0000", "12105.0000", "13123.0000", "133499.0000",
"15266.0000", "183570.0000", "9000.0000", "10089.0000", "10781.0000",
"11192.0000", "1162.0000", "11757.0000", "11800.0000", "1363.0000",
"15031.0000", "1576.0000", "163281.0000", "19579.0000", "1960.0000",
"20104.0000", "2024.0000", "2157.0000", "2330.0000", "2371.0000",
"2372.0000", "24299.0000", "25550.0000", "2607.0000", "2608.0000",
"2798.0000", "3474.0000", "3898.0000", "40359.0000", "40820.0000",
"4312.0000", "4469.0000", "4676.0000", "5026.0000", "61744.0000",
"6316.0000", "6391.0000", "785.0000", "8394.0000", "879.0000",
"887.0000", "1409.0000", "14350.0000", "17514.0000", "199.0000",
"2430.0000", "2529.0000", "2964.0000", "3587.0000", "583.0000",
"7153.0000", "7443.0000", "7657.0000", "8640.0000", "1005.0000",
"109.0000", "1122.0000", "11860.0000", "12349.0000", "1282.0000",
"131.0000", "13521.0000", "1366.0000", "13978.0000", "1430.0000",
"14312.0000", "1620.0000", "16359.0000", "1699.0522", "1817.0000",
"1954.0000", "2018.0000", "2102.0000", "21780.0000", "2182.0000",
"2374.0000", "24065.0000", "247.0000", "2470.0000", "2627.8701",
"2810.0000", "30297.0000", "315.0000", "3307.0000", "3462.0000",
"3578.0000", "358.0000", "3830.0000", "3916.0000", "4021.0000",
"4089.0000", "4391.0000", "4412.0000", "470.0000", "4742.0000",
"4776.0000", "4860.0000", "5045.0000", "5110.0000", "5465.0000",
"607.0000", "6418.0000", "659.0000", "672.0000", "740.0000",
"7417.0000", "750.0000", "7574.0000", "7818.0000", "7881.0000",
"8072.0000", "8731.0000", "9047.0000", "1223.0000", "127.0000",
"128.0000", "16428.0000", "1842.0000", "22772.0000", "41193.0000",
"517.0000", "5389.0000", "5928.0000", "6761.0000", "6881.0000",
"76.0000", "77.0000", "10400.0000", "107416.0000", "128299.0000",
"17000.0000", "275000.0000", "278407.0000", "31500.0000",
"416667.0000", "479271.0000", "493.0000", "577901.0000",
"627901.0000", "750000.0000", "80000.0000", "85.0000", "952676.0000",
"3727.0000", "554.0000", "12623.0000", "13135.0000", "14579.0000",
"16901.0000", "2049.0000", "21755.0000", "22338.0000", "2246.0000",
"2373.0000", "24538.0000", "28389.0000", "3265.0000", "3729.0000",
"39462.0000", "3954.0000", "50494.0000", "5584.0000", "5585.0000",
"6134.0000", "6718.0000", "68604.0000", "6991.0000", "7145.0000",
"758.0000", "8197.0000", "9089.0000", "9865.0000", "110.0000",
"1120.0000", "1165.0000", "12000.0000", "1586.0000", "2142.0000",
"2927.0000", "30000.0000", "952.0000", "3844.0000", "48403.0000",
"533.0000", "6878.0000", "101.0000", "10599.0000", "11000.0000",
"1125.0000", "1186.0000", "122.0000", "12200.0000", "12320.0000",
"1344.0000", "1373.0000", "1380.0000", "1432.0000", "14726.0000",
"1524.0000", "1529.0000", "15500.0000", "1555.0000", "1568.0000",
"15762.0000", "1780.0000", "1800.0000", "1841.0000", "1875.0000",
"1979.0000", "2084.0000", "2152.0000", "233.0000", "2409.0000",
"2443.0000", "2635.0000", "2835.0000", "2899.0000", "3070.0000",
"3080.0000", "3096.0000", "3136.0000", "3146.0000", "3296.0000",
"3405.0000", "3419.0000", "3751.0000", "3757.0000", "3849.0000",
"4106.0000", "4107.0000", "4292.0000", "4321.0000", "4645.0000",
"4901.0000", "4931.0000", "503.0000", "5060.0000", "5316.0000",
"5317.0000", "5412.0000", "5497.0000", "5557.0000", "5838.0000",
"5862.0000", "6083.0000", "6168.0000", "6323.0000", "802.0000",
"8487.0000", "9635.0000", "13378.0000", "14906.0000", "1537.0000",
"1643.0000", "21393.0000", "2191.0000", "23384.0000", "23670.0000",
"2451.0000", "25578.0000", "28974.0000", "2982.0000", "3123.0000",
"3134.0000", "31343.0000", "3246.0000", "334.0000", "3426.0000",
"3513.0000", "360.0000", "399.0000", "41123.0000", "4357.0000",
"436.0000", "4404.0000", "443.0000", "523.0000", "545.0000",
"5511.0000", "62829.0000", "6309.0000", "6783.0000", "7170.0000",
"787.0000", "793.0000", "87503.0000", "103.0000", "10854.0000",
"110000.0000", "11108.0000", "1273.0000", "1419.0000", "1503.0000",
"15790.0000", "1615.0000", "164.0000", "18440.0000", "1911.0000",
"1932.0000", "1942.0000", "2369.0000", "2497.0000", "26400.0000",
"2919.0000", "292000.0000", "2995.0000", "2996.0000", "31422.0000",
"3371.0000", "3503.0000", "3504.0000", "3643.0000", "3892.0000",
"3948.0000", "3987.0000", "4146.0000", "496.0000", "5203.0000",
"58.0000", "6685.0000", "68.0000", "727.0000", "9220.0000",
"34000.0000", "1325.0000", "140.0000", "2040.0000", "260.0000",
"261.0000", "268.0000", "285.0000", "306.0000", "407.0000",
"408.0000", "425.0000", "434.0000", "442.0000", "4759.0000",
"501.0000", "51.0000", "519.0000", "52.0000", "54.0000",
"55.0000", "763.0000", "764.0000", "828.0000", "95.0000",
"96.0000", "988.0000", "1011.0000", "102915.0000", "103367.0000",
"10460.0000", "10603.0000", "108193.0000", "1172.0000", "1175.0000",
"11823.0000", "14072.0000", "141881.0000", "143120.0000",
"14724.0000", "147607.0000", "154400.0000", "15745.0000",
"15831.0000", "16526.0000", "18100.0000", "19589.0000", "20700.0000",
"2100.0000", "211406.0000", "215868.0000", "24100.0000",
"25413.0000", "257965.0000", "34.0000", "35340.0000", "36700.0000",
"37845.0000", "39448.0000", "398.0000", "40205.0000", "4025.0000",
"449.0000", "4653.0000", "467.0000", "487.0000", "494.0000",
"4964.0000", "512076.0000", "58767.0000", "636.0000", "681.0000",
"691.0000", "70680.0000", "725.0000", "7843.0000", "834.0000",
"844.0000", "86.0000", "874.0000", "976.0000", "9927.0000",
"8965.0000", "10025.0000", "10150.0000", "103454.0000", "11516.0000",
"120324.0000", "12641.0000", "13000.0000", "13700.0000",
"14000.0000", "14611.0000", "19750.0000", "2225.0000", "24351.0000",
"24653.0000", "25.0000", "25000.0000", "252225.0000", "26888.0000",
"286671.0000", "29500.0000", "30007.0000", "305.0000", "30775.0000",
"30992.0000", "324.0000", "36500.0000", "36965.0000", "37951.0000",
"38099.0000", "38300.0000", "43500.0000", "43720.0000", "44771.0000",
"48312.0000", "5004.0000", "50293.0000", "5217240.0000",
"5308.0000", "5349.0000", "53767.0000", "58000.0000", "58862.0000",
"59863.0000", "6047.0000", "60876.0000", "6215.0000", "63061.0000",
"73423.0000", "75347.0000", "79035.0000", "91677.0000", "92779.0000",
"1041.0000", "1211.0000", "41401.0000", "4282.0000", "772.0000",
"0.0000", "108920.0000", "1374.0000", "1487639.0000", "1567.0000",
"1918587.0000", "1951219.0000", "19569979.0000", "200637.0000",
"2456822.0000", "26611.0000", "300000.0000", "335279.0000",
"3455129.0000", "3907165.0000", "4280.0000", "431489.0000",
"4403409.0000", "49303.0000", "5000000.0000", "76190.0000",
"8433.0000", "85191.0000", "9626.0000", "981282.0000", "1038.0000",
"12131.0000", "133334.0000", "1669.0000", "1921.0000", "1980.0000",
"2331.0000", "2600.0000", "2630.0000", "2959.0000", "378232.0000",
"40235.0000", "4138.0000", "4407.0000", "4473.0000", "4959.0000",
"49921.0000", "51688.0000", "5302.0000", "5726.0000", "7105.0000",
"8635.0000", "96534.0000", "1096.0000", "12386.0000", "12614.0000",
"13726.0000", "14640.0000", "14951.0000", "151.0000", "155.0000",
"159.0000", "162.0000", "166.0000", "181.0000", "182.0000",
"184.0000", "186.0000", "188.0000", "18902.0000", "19376.0000",
"205.0000", "207.0000", "20706.0000", "209.0000", "211.0000",
"213.0000", "220.0000", "235.0000", "2604.0000", "28186.0000",
"286.0000", "314.0000", "31436.0000", "34976.0000", "350.0000",
"3730.0000", "3731.0000", "3745.0000", "385.0000", "4019.0000",
"42282.0000", "432.0000", "448.0000", "5091.0000", "5373.0000",
"5650.0000", "581.0000", "6115.0000", "658.0000", "6981.0000",
"7462.0000", "7523.0000", "7761.0000", "8106.0000", "829.0000",
"84.0000", "875.0000", "8964.0000", "898.0000", "935.0000",
"9762.0000"), class = "factor")), row.names = c(NA, 9L), class = "data.frame")
There's probably a nicer solution, but I would do it as follows
library(data.table)
library(lubridate)
DT <- as.data.table(InsiderList3)
setnames(DT, gsub(" ", ".", colnames(DT)))
setkey(DT, Insider.CIK, Transaction.Date)
# Mark the 90 day period
DT[, minDate := min(Transaction.Date), by=Insider.CIK]
DT[Transaction.Date - days(90) <= MinDate, DateMatch := 1]
# Mark all firms whose first transactions are S or P
DT[Transaction.Date==min(Transaction.Date) & Transaction.Type %in% c("S-Sale", "P-Purchase"), FirstTransaction := Transaction.Type, by=Insider.CIK]
DT[, FirstTransaction := unique(FirstTransaction), by=Insider.CIK]
# Then you can filter based on the two criteria
DT.SP <- DT[DateMatch==1 & FirstTransaction=="S-Sale" & Transaction.Type=="P-Purchase"]
DT.PS <- DT[DateMatch==1 & FirstTransaction=="P-Sale" & Transaction.Type=="S-Purchase"]
This is a quick and dirty filtering... I'm sure there's a way to do this in fewer lines of code, but this should work. Pretty or not, the solution should be pretty fast since it uses data.table.

R Text Mining - the most frequent word in string across entire data frame

I am struggling to grasp text mining and determine word frequencies. I am just starting to have an understanding of R and its packages and I just find out about tm (after reading a while I have a feeling that this might solve my problem).
My question is: how can I determine the two most frequently used in a string across the entire column?
I have the following example:
structure(list(Location = c("Chicago", "Chicago", "Chicago",
"LA", "LA", "LA", "LA", "LA", "LA", "Texas", "Texas", "Texas",
"Texas", "Texas"), Code = c(4450L, 4450L, 4450L, 4450L, 4450L,
4450L, 4450L, 4450L, 4450L, 4410L, 4410L, 4410L, 4410L, 4410L
), Description = c("LABOR - CROSSOVER BOARD BRACKET", "LABOR - CROWN DOOR GASKET",
"LABOR - CROWN DOOR GASKET - APPLY 4' NEW GASKET AND RE-APPLY",
"LABOR - CUSHIONING DEVICE - END OF CAR CUSTOMER SUPPLIED MATERIAL",
"LABOR - DOOR EDGE", "LABOR - DOOR GASKET, CROWN CORNER", "LABOR - DOOR LOCK POCKET STG",
"LABOR - DOOR LOCK RECEPTICALS STG", "LABOR - DOOR LOCK STG",
"BOLT, HT, UNDER 5/8\"\" DIA & 6\"\" - SIDE POST", "BOLT, HT, UNDER 5/8\"\" DIA & 6\"\" - TRAINLINE TROLLEY",
"BOLT,HT,5/8 IN.DIA.OR LESS UNDER 6\"\" LONG - BRAKE STEP", "BOLT,HT,5/8 IN.DIA.OR LESS UNDER 6\"\" LONG - CROSSOVER BOARD",
"BOLT,HT,5/8 IN.DIA.OR LESS UNDER 6\"\" LONG - CROSSOVER BOARD BRACKET"
), `Desired Description Based on frequency` = c("Labor - CROWN DOOR GASKET",
"Labor - CROWN DOOR GASKET", "Labor - CROWN DOOR GASKET", "Labor - DOOR LOCK",
"Labor - DOOR LOCK", "Labor - DOOR LOCK", "Labor - DOOR LOCK",
"Labor - DOOR LOCK", "Labor - DOOR LOCK", "Bolt - HT", "Bolt - HT",
"Bolt - HT", "Bolt - HT", "Bolt - HT")), .Names = c("Location",
"Code", "Description", "Desired Description Based on frequency"
), row.names = c(NA, -14L), class = "data.frame")
In the end I wish I could add this column:
Desired Description Based on frequency
Labor - CROWN DOOR GASKET
Labor - CROWN DOOR GASKET
Labor - CROWN DOOR GASKET
Labor - DOOR LOCK
Labor - DOOR LOCK
Labor - DOOR LOCK
Labor - DOOR LOCK
Labor - DOOR LOCK
Labor - DOOR LOCK
Bolt - HT
Bolt - HT
Bolt - HT
Bolt - HT
Bolt - HT
Basically I want to evaluate all the 4450 or 4410s and see out of all the description in the table, which the most common and add that as a column. Another condition would be based on the location. Can someone please help me with a simple example?
Thank you so much
I don't think there's a one-size-fits-it-all-solution to your problem. (Beginning with the fact that there's no exact rule on which or how many words to take for the description.) However, here are two quick&dirty approaches, which might be helpful as a starting point:
library(tm)
txts <- gsub("[^A-Z]", " ", df$Description)
groups <- paste(df$Location, df$Code)
# 1
opts <- list(tolower=F, removePunctuation=TRUE, wordLengths=c(2, Inf))
lst <- split(txts, groups)
res <- sapply(lst, function(x) {
freq <- termFreq(paste(x, collapse=" "), opts)/length(x)
paste(names(freq[rank(-freq, ties.method = "first")<=3]), collapse = " - ")
})
rep(res, lengths(lst))
# 2
lst <- lapply(strsplit(txts, "\\s+"), function(x) x[1:min(c(3,length(x)))] )
lst <- split(lst, groups)
n <- lengths(lst)
lst <- mapply("/", lapply(lst, function(x) sort(table(unlist(x)), decreasing = T)), n)
rep(sapply(lst, function(x) paste(names(x)[x>=.5], collapse=" - ")), n)

Change range of "fill" values in choropleth map R

Any help would be greatly appreciated!!
I'm trying to create a choropleth map in R that shows the counties of texas, color-coded by their population ranges.
My problem is that the range of populations is too large. The highest population is over 4 million, but most of the counties have a population under 50,000. The criteria for the fill is: (0-1mil), (1-2mil), (2-3mil), (3-4mil), (4-5mil) but almost all fall under 0-1mil.
How can I change the legend to account for different ranges of numbers? For example, maybe:
(0-1,000), (1,000-10,000), (10,000-100,000), (100,000-1mil), (1mil-5mil)
Here's the code I wrote to plot the data:
txplot <- ggplot(txczpop, aes(fill=pop2014)) + geom_map(txmap)
tm_shape(txmap) +
tm_fill("pop2014", title="TX County Population", palette = "PRGn") +
tm_borders(alpha=.5) +
tm_style_beaver()
Here's the result:
[![enter image description here][1]][1]
I'm using a census county shapefile and population also retrieved from a census file.
Here's the output of my population data:
txczpop <- structure(list(county_fips = c(48001L, 48003L, 48005L, 48007L,
48009L, 48011L, 48013L, 48015L, 48017L, 48019L, 48021L, 48023L,
48025L, 48027L, 48029L, 48031L, 48033L, 48035L, 48037L, 48039L,
48041L, 48043L, 48045L, 48047L, 48049L, 48051L, 48053L, 48055L,
48057L, 48059L, 48061L, 48063L, 48065L, 48067L, 48069L, 48071L,
48073L, 48075L, 48077L, 48079L, 48081L, 48083L, 48085L, 48087L,
48089L, 48091L, 48093L, 48095L, 48097L, 48099L, 48101L, 48103L,
48105L, 48107L, 48109L, 48111L, 48113L, 48115L, 48117L, 48119L,
48121L, 48123L, 48125L, 48127L, 48129L, 48131L, 48133L, 48135L,
48137L, 48141L, 48139L, 48143L, 48145L, 48147L, 48149L, 48151L,
48153L, 48155L, 48157L, 48159L, 48161L, 48163L, 48165L, 48167L,
48169L, 48171L, 48173L, 48175L, 48177L, 48179L, 48181L, 48183L,
48185L, 48187L, 48189L, 48191L, 48193L, 48195L, 48197L, 48199L,
48201L, 48203L, 48205L, 48207L, 48209L, 48211L, 48213L, 48215L,
48217L, 48219L, 48221L, 48223L, 48225L, 48227L, 48229L, 48231L,
48233L, 48235L, 48237L, 48239L, 48241L, 48243L, 48245L, 48247L,
48249L, 48251L, 48253L, 48255L, 48257L, 48259L, 48261L, 48263L,
48265L, 48267L, 48269L, 48271L, 48273L, 48275L, 48283L, 48277L,
48279L, 48281L, 48285L, 48287L, 48289L, 48291L, 48293L, 48295L,
48297L, 48299L, 48301L, 48303L, 48305L, 48313L, 48315L, 48317L,
48319L, 48321L, 48323L, 48307L, 48309L, 48311L, 48325L, 48327L,
48329L, 48331L, 48333L, 48335L, 48337L, 48339L, 48341L, 48343L,
48345L, 48347L, 48349L, 48351L, 48353L, 48355L, 48357L, 48359L,
48361L, 48363L, 48365L, 48367L, 48369L, 48371L, 48373L, 48375L,
48377L, 48379L, 48381L, 48383L, 48385L, 48387L, 48389L, 48391L,
48393L, 48395L, 48397L, 48399L, 48401L, 48403L, 48405L, 48407L,
48409L, 48411L, 48413L, 48415L, 48417L, 48419L, 48421L, 48423L,
48425L, 48427L, 48429L, 48431L, 48433L, 48435L, 48437L, 48439L,
48441L, 48443L, 48445L, 48447L, 48449L, 48451L, 48453L, 48455L,
48457L, 48459L, 48461L, 48463L, 48465L, 48467L, 48469L, 48471L,
48473L, 48475L, 48477L, 48479L, 48481L, 48483L, 48485L, 48487L,
48489L, 48491L, 48493L, 48495L, 48497L, 48499L, 48501L, 48503L,
48505L, 48507L), county_name = c("Anderson", "Andrews", "Angelina",
"Aransas", "Archer", "Armstrong", "Atascosa", "Austin", "Bailey",
"Bandera", "Bastrop", "Baylor", "Bee", "Bell", "Bexar", "Blanco",
"Borden", "Bosque", "Bowie", "Brazoria", "Brazos", "Brewster",
"Briscoe", "Brooks", "Brown", "Burleson", "Burnet", "Caldwell",
"Calhoun", "Callahan", "Cameron", "Camp", "Carson", "Cass", "Castro",
"Chambers", "Cherokee", "Childress", "Clay", "Cochran", "Coke",
"Coleman", "Collin", "Collingsworth", "Colorado", "Comal", "Comanche",
"Concho", "Cooke", "Coryell", "Cottle", "Crane", "Crockett",
"Crosby", "Culberson", "Dallam", "Dallas", "Dawson", "Deaf Smith",
"Delta", "Denton", "DeWitt", "Dickens", "Dimmit", "Donley", "Duval",
"Eastland", "Ector", "Edwards", "El Paso", "Ellis", "Erath",
"Falls", "Fannin", "Fayette", "Fisher", "Floyd", "Foard", "Fort Bend",
"Franklin", "Freestone", "Frio", "Gaines", "Galveston", "Garza",
"Gillespie", "Glasscock", "Goliad", "Gonzales", "Gray", "Grayson",
"Gregg", "Grimes", "Guadalupe", "Hale", "Hall", "Hamilton", "Hansford",
"Hardeman", "Hardin", "Harris", "Harrison", "Hartley", "Haskell",
"Hays", "Hemphill", "Henderson", "Hidalgo", "Hill", "Hockley",
"Hood", "Hopkins", "Houston", "Howard", "Hudspeth", "Hunt", "Hutchinson",
"Irion", "Jack", "Jackson", "Jasper", "Jeff Davis", "Jefferson",
"Jim Hogg", "Jim Wells", "Johnson", "Jones", "Karnes", "Kaufman",
"Kendall", "Kenedy", "Kent", "Kerr", "Kimble", "King", "Kinney",
"Kleberg", "Knox", "La Salle", "Lamar", "Lamb", "Lampasas", "Lavaca",
"Lee", "Leon", "Liberty", "Limestone", "Lipscomb", "Live Oak",
"Llano", "Loving", "Lubbock", "Lynn", "Madison", "Marion", "Martin",
"Mason", "Matagorda", "Maverick", "McCulloch", "McLennan", "McMullen",
"Medina", "Menard", "Midland", "Milam", "Mills", "Mitchell",
"Montague", "Montgomery", "Moore", "Morris", "Motley", "Nacogdoches",
"Navarro", "Newton", "Nolan", "Nueces", "Ochiltree", "Oldham",
"Orange", "Palo Pinto", "Panola", "Parker", "Parmer", "Pecos",
"Polk", "Potter", "Presidio", "Rains", "Randall", "Reagan", "Real",
"Red River", "Reeves", "Refugio", "Roberts", "Robertson", "Rockwall",
"Runnels", "Rusk", "Sabine", "San Augustine", "San Jacinto",
"San Patricio", "San Saba", "Schleicher", "Scurry", "Shackelford",
"Shelby", "Sherman", "Smith", "Somervell", "Starr", "Stephens",
"Sterling", "Stonewall", "Sutton", "Swisher", "Tarrant", "Taylor",
"Terrell", "Terry", "Throckmorton", "Titus", "Tom Green", "Travis",
"Trinity", "Tyler", "Upshur", "Upton", "Uvalde", "Val Verde",
"Van Zandt", "Victoria", "Walker", "Waller", "Ward", "Washington",
"Webb", "Wharton", "Wheeler", "Wichita", "Wilbarger", "Willacy",
"Williamson", "Wilson", "Winkler", "Wise", "Wood", "Yoakum",
"Young", "Zapata", "Zavala"), pop2014 = c(57627L, 17477L, 87750L,
24972L, 8811L, 1955L, 47774L, 29114L, 6910L, 20892L, 78069L,
3592L, 32863L, 329140L, 1855866L, 10812L, 652L, 17780L, 93275L,
338124L, 209152L, 9173L, 1536L, 7194L, 37653L, 17253L, 44943L,
39810L, 21797L, 13513L, 420392L, 12621L, 6013L, 30261L, 7781L,
38145L, 50902L, 7089L, 10370L, 2935L, 3254L, 8430L, 885241L,
3017L, 20719L, 123694L, 13550L, 4050L, 38761L, 75562L, 1415L,
4950L, 3812L, 5899L, 2266L, 7135L, 2518638L, 13372L, 19195L,
5238L, 753363L, 20684L, 2218L, 11089L, 3543L, 11533L, 18176L,
153904L, 1879L, 833487L, 159317L, 40147L, 16989L, 33752L, 24833L,
3831L, 5949L, 1275L, 685345L, 10600L, 19762L, 18531L, 19425L,
314198L, 6435L, 25520L, 1291L, 7549L, 20462L, 23044L, 123534L,
123204L, 27172L, 147250L, 34720L, 3147L, 8199L, 5509L, 3928L,
55621L, 4441370L, 67336L, 6089L, 5769L, 185025L, 4180L, 79290L,
831073L, 34848L, 23577L, 53921L, 35921L, 22741L, 36651L, 3211L,
88493L, 21773L, 1574L, 8855L, 14739L, 35552L, 2204L, 252235L,
5255L, 41353L, 157456L, 19936L, 14906L, 111236L, 38880L, 400L,
785L, 50562L, 4438L, 262L, 3526L, 32190L, 3858L, 7474L, 49523L,
13574L, 20156L, 19721L, 16742L, 16861L, 78117L, 23524L, 3553L,
12091L, 19510L, 86L, 293974L, 5771L, 13861L, 10149L, 5460L, 4071L,
36519L, 57023L, 8199L, 243441L, 805L, 47894L, 2147L, 155830L,
24256L, 4870L, 9076L, 19416L, 518947L, 22148L, 12743L, 1153L,
65301L, 48195L, 14138L, 15093L, 356221L, 10758L, 2070L, 83433L,
28096L, 23769L, 123164L, 9908L, 15893L, 46079L, 121627L, 6976L,
11032L, 128220L, 3755L, 3371L, 12446L, 14349L, 7302L, 928L, 16500L,
87809L, 10416L, 53923L, 10350L, 8610L, 27099L, 66915L, 5622L,
3162L, 17328L, 3343L, 25515L, 3084L, 218842L, 8694L, 62955L,
9405L, 1339L, 1403L, 3972L, 7581L, 1945360L, 135143L, 927L, 12739L,
1608L, 32506L, 116608L, 1151145L, 14224L, 21418L, 40354L, 3454L,
27117L, 48974L, 52910L, 91081L, 69789L, 46820L, 11625L, 34438L,
266673L, 41168L, 5714L, 132355L, 12973L, 21903L, 489250L, 46402L,
7821L, 61638L, 42852L, 8286L, 18350L, 14319L, 12267L)), .Names = c("county_fips",
"county_name", "pop2014"), row.names = c(5100L, 5101L, 5103L,
5106L, 5107L, 5109L, 5112L, 5114L, 5116L, 5118L, 5120L, 5121L,
5124L, 5126L, 5128L, 5129L, 5131L, 5133L, 5136L, 5137L, 5140L,
5141L, 5143L, 5146L, 5147L, 5150L, 5152L, 5153L, 5156L, 5158L,
5159L, 5161L, 5163L, 5166L, 5168L, 5170L, 5171L, 5174L, 5176L,
5178L, 5179L, 5182L, 5183L, 5185L, 5188L, 5190L, 5192L, 5194L,
5195L, 5198L, 5200L, 5201L, 5203L, 5205L, 5208L, 5209L, 5212L,
5214L, 5215L, 5218L, 5219L, 5221L, 5224L, 5226L, 5228L, 5230L,
5232L, 5233L, 5235L, 5239L, 5237L, 5242L, 5244L, 5245L, 5248L,
5249L, 5251L, 5254L, 5256L, 5257L, 5260L, 5261L, 5264L, 5265L,
5268L, 5270L, 5272L, 5274L, 5276L, 5278L, 5280L, 5281L, 5284L,
5286L, 5288L, 5290L, 5292L, 5293L, 5296L, 5298L, 5300L, 5301L,
5303L, 5306L, 5308L, 5309L, 5312L, 5314L, 5316L, 5317L, 5319L,
5321L, 5323L, 5326L, 5327L, 5330L, 5332L, 5334L, 5335L, 5337L,
5339L, 5341L, 5343L, 5346L, 5348L, 5349L, 5352L, 5354L, 5356L,
5357L, 5360L, 5362L, 5364L, 5365L, 5368L, 5369L, 5372L, 5374L,
5382L, 5376L, 5378L, 5379L, 5383L, 5385L, 5388L, 5390L, 5392L,
5394L, 5396L, 5398L, 5400L, 5401L, 5404L, 5412L, 5413L, 5416L,
5418L, 5419L, 5421L, 5406L, 5407L, 5409L, 5423L, 5425L, 5427L,
5429L, 5432L, 5434L, 5435L, 5438L, 5440L, 5442L, 5443L, 5446L,
5448L, 5449L, 5451L, 5453L, 5456L, 5457L, 5460L, 5461L, 5464L,
5465L, 5468L, 5470L, 5472L, 5474L, 5476L, 5477L, 5480L, 5482L,
5484L, 5486L, 5488L, 5489L, 5491L, 5494L, 5496L, 5498L, 5499L,
5501L, 5504L, 5505L, 5508L, 5510L, 5511L, 5514L, 5516L, 5518L,
5520L, 5522L, 5524L, 5526L, 5527L, 5530L, 5531L, 5533L, 5536L,
5537L, 5540L, 5542L, 5544L, 5546L, 5547L, 5550L, 5552L, 5554L,
5555L, 5558L, 5559L, 5562L, 5563L, 5566L, 5568L, 5569L, 5571L,
5574L, 5575L, 5578L, 5579L, 5582L, 5584L, 5585L, 5587L, 5590L,
5592L, 5594L, 5595L, 5598L, 5600L, 5602L, 5604L, 5606L), class = "data.frame")
I just created a new column in the population dataframe that summarizes the population based on the ranges that I want to use, and then use that as the criteria for the fill:
txczpop$poprange[txczpop$pop2014 >= 0 & txczpop < 1000] <- "0-1,000"
txczpop$poprange[txczpop$pop2014 >= 1000 & txczpop < 10000] <- "1-10,000"
txczpop$poprange[txczpop$pop2014 >= 10000 & txczpop$pop2014 < 100000] <- "10,000-100,000"
txczpop$poprange[txczpop$pop2014 >= 100000 & txczpop$pop2014 < 1000000] <- "100,000 - 1,000,000"
txczpop$poprange[txczpop$pop2014 >= 1000000 & txczpop$pop2014 <= 5000000] <- "1,000,000 - 5,000,000"

Fitting tau(time constant) over a specific period using ggplot2

I apologize in advance for the specificity of this question, I'm hoping my neuroscience jargon doesn't confuse things.
I have current traces from single cell recordings and I need to fit a tau from the peak to 4 seconds after the peak out on each trace. In reality, this is just an exponential decay.
So what I'm looking for is a ggplot overlay of the fit (so really a fit line over the period requested.) something along the lines of stat_smooth()
My data looks like follow:
dput(stackover_data)
structure(list(Time = c(0.09990001, 0.19990001, 0.29990001, 0.39990001,
0.49990001, 0.59990001, 0.69990001, 0.79990001, 0.89990001, 0.99990001,
1.09990001, 1.19990001, 1.29990001, 1.39990001, 1.49990001, 1.59990001,
1.69990001, 1.79990001, 1.89990001, 1.99990001, 2.09990001, 2.19990001,
2.29990001, 2.39990001, 2.49990001, 2.59990001, 2.69990001, 2.79990001,
2.89990001, 2.99990001, 3.09990001, 3.19990001, 3.29990001, 3.39990001,
3.49990001, 3.59990001, 3.69990001, 3.79990001, 3.89990001, 3.99990001,
4.09990001, 4.19990001, 4.29990001, 4.39990001, 4.49990001, 4.59990001,
4.69990001, 4.79990001, 4.89990001, 4.99990001, 5.09990001, 5.19990001,
5.29990001, 5.39990001, 5.49990001, 5.59990001, 5.69990001, 5.79990001,
5.89990001, 5.99990001, 6.09990001, 6.19990001, 6.29990001, 6.39990001,
6.49990001, 6.59990001, 6.69990001, 6.79990001, 6.89990001, 6.99990001,
7.09990001, 7.19990001, 7.29990001, 7.39990001, 7.49990001, 7.59990001,
7.69990001, 7.79990001, 7.89990001, 7.99990001, 8.09990001, 8.19990001,
8.29990001, 8.39990001, 8.49990001, 8.59990001, 8.69990001, 8.79990001,
8.89990001, 8.99990001, 9.09990001, 9.19990001, 9.29990001, 9.39990001,
9.49990001, 9.59990001, 9.69990001, 9.79990001, 9.89990001, 9.99990001,
10.09990001, 10.19990001, 10.29990001, 10.39990001, 10.49990001,
10.59990001, 10.69990001, 10.79990001, 10.89990001, 10.99990001,
11.09990001, 11.19990001, 11.29990001, 11.39990001, 11.49990001,
11.59990001, 11.69990001, 11.79990001, 11.89990001, 11.99990001,
12.09990001, 12.19990001, 12.29990001, 12.39990001, 12.49990001,
12.59990001, 12.69990001, 12.79990001, 12.89990001, 12.99990001,
13.09990001, 13.19990001, 13.29990001, 13.39990001, 13.49990001,
13.59990001, 13.69990001, 13.79990001, 13.89990001, 13.99990001,
14.09990001, 14.19990001, 14.29990001, 14.39990001, 14.49990001,
14.59990001, 14.69990001, 14.79990001, 14.89990001, 14.99990001,
15.09990001, 15.19990001, 15.29990001, 15.39990001, 15.49990001,
15.59990001, 15.69990001, 15.79990001, 15.89990001, 15.99990001,
16.09990001, 16.19990001, 16.29990001, 16.39990001, 16.49990001,
16.59990001, 16.69990001, 16.79990001, 16.89990001, 16.99990001,
17.09990001, 17.19990001, 17.29990001, 17.39990001, 17.49990001,
17.59990001, 17.69990001, 17.79990001, 17.89990001, 17.99990001,
18.09990001, 18.19990001, 18.29990001, 18.39990001, 18.49990001,
18.59990001, 18.69990001, 18.79990001, 18.89990001, 18.99990001,
19.09990001, 19.19990001, 19.29990001, 19.39990001, 19.49990001,
19.59990001, 19.69990001, 19.79990001, 19.89990001, 19.99990001
), `Trace 1` = c(-3.08656892325052, 9.36821982641837, 8.13806079083122,
10.7039590839898, 7.25670468903547, 4.31122291688919, 1.77905971163193,
-6.27606834721828, -8.65955381985049, -10.1445673910916, -10.6649772153892,
-6.52301948183154, -11.9757817854835, -0.976254254762154, 0.237467076202677,
-11.8114896779541, -11.0022757370468, -16.1845427042923, 2.70565927469852,
-18.9048281652216, -22.153682283437, -4363.32044948884, -3470.59111611883,
-2877.51064886248, -2253.51505229908, -1876.62974792002, -1541.14546478629,
-1288.26617158403, -1232.52313999354, -1042.98549163259, -938.795146277054,
-810.913567086442, -736.390770574588, -682.044521632168, -638.575324886466,
-596.587948389699, -629.282103146111, -569.200610245336, -587.387817942122,
-521.939307772762, -533.776693538631, -514.782451411908, -445.949395199026,
-451.026618716539, -461.669600651513, -442.372477296489, -426.132547857502,
-452.471101919398, -449.377497412324, -436.817659066873, -487.464805660851,
-442.915455035179, -458.666741489705, -454.990437793055, -455.173154690614,
-426.885702219019, -456.985443835707, -408.869318611773, -442.418742303429,
-407.42593099033, -414.538253423006, -316.036755461507, -248.395118743017,
-198.708015370115, -183.88480278352, -160.041754268138, -139.020785864805,
-123.701641615743, -137.253323547789, -124.240619047461, -121.512859107349,
-113.719386521321, -102.98429740535, -118.721098087137, -109.789471870234,
-122.145399997255, -109.542467905009, -96.6725326170008, -108.16233877188,
-94.1092968366083, -88.0987495250118, -89.2245425850472, -96.3495439993499,
-110.340434956898, -98.8777265479938, -88.0674013452629, -102.755317774957,
-91.0752842494157, -107.959830375198, -78.8424385901398, -93.7743443479161,
-98.3591889757604, -72.3214090579355, -85.6296201608712, -112.596656084088,
-115.518068650615, -110.973655632476, -78.8021307215932, -85.6443070182152,
-93.1744356389988, -100.483987323044, -87.6672104421484, -83.6481077757535,
-82.1465876740715, -86.7838666454595, -75.9066755520263, -86.6416980998645,
-88.9405806921788, -74.0592581080291, -86.3433338300531, -93.5114839187431,
-91.1875849041866, -82.7018083540351, -102.859075734953, -82.1494206590809,
-79.2197323780198, -78.1558787387238, -86.2649418863144, -93.5271994290692,
-84.6678528566242, -71.4828270654073, -69.5618263581887, -67.2920558863641,
-58.1490330439793, -59.8163238740351, -73.6957797622946, -61.7673947702343,
-63.6492255747164, -64.043638367468, -73.8301991524909, -69.3055992018769,
-65.9342860783478, -74.9891521715357, -74.5779808619617, -69.9029875902787,
-69.746935396023, -71.35455530782, -73.5279471991205, -77.8000626250279,
-74.6065254864801, -64.0834786591292, -45.1346212136811, -43.1615385011179,
-42.554841323715, -50.276566542849, -56.1940469314277, -49.6368473019083,
-53.9842269565738, -54.1480156577708, -48.2160751112714, -43.2902743874793,
-46.4358385791469, -58.4459814967147, -49.0443619038303, -56.8007684056415,
-56.3345802020277, -40.1704617527471, -38.2624410086947, -39.9339279963857,
-27.7637250414188, -40.8686520798649, -30.9246406597275, -32.70021580322,
-28.7536071219405, -32.4403271637661, -30.3389870650563, -23.2866770834185,
-40.349370835336, -39.7478049410975, -38.4648592612063, -37.4191914226093,
-52.1259539643142, -38.0993644763616, -37.5493511061199, -25.0794144286873,
-32.6165749053184, -17.2616237291667, -32.1515419380766, -27.0814392288745,
-26.2440551871993, -17.5882979851251, -19.852012057918, -22.1408312947152,
-21.3395092026716, -28.0789344732281, -19.6443281019371, -26.6585778398899,
-24.2994817465722, -27.847437388783, -22.0565059455712, -24.8747905836351,
-30.7544250721296, -29.9924061105416, -22.5728855329732, -24.9767037068316,
-18.8208568928653, -10.7306087216159, -16.4281210876173, -19.3057174287183,
-15.9745523586581), `Trace 2` = c(5.94927992143286, 1.42121402161905,
6.78788136514507, 3.33970424403748, -3.73956433922802, -7.3097330836793,
-9.18242380095097, 3.29952017048882, 5.17208246028, 1.53238537592179,
6.90832098860733, 3.16748380079213, 5.49988319742749, 3.86758484926656,
11.981378748128, -0.208159377585758, -7.2781503569274, 1.71389537416221,
-4.77614396689646, -0.561115871778583, -14.5385068323377, -4944.63006397872,
-3891.23141079918, -3065.66300899921, -2527.61853791531, -2060.05223709386,
-1763.27814995793, -1451.77884853301, -1368.42409410389, -1115.3385743493,
-990.942590410893, -852.951182088994, -920.516421149141, -860.486324532349,
-751.257935697359, -768.757880984424, -713.935725464811, -715.011716140783,
-616.355633698422, -672.133827760044, -629.418857335266, -607.342562427532,
-599.657392949772, -553.265193759536, -572.878306401819, -583.153736972572,
-589.661926862833, -527.552306430313, -553.672277977987, -539.906254466276,
-525.491319968336, -564.876755897633, -505.363603776286, -575.649358123809,
-497.897048596953, -544.360501913781, -483.501835830955, -533.085516954168,
-478.007776808595, -453.333986756674, -447.125338584774, -380.268661132557,
-253.127091546528, -227.877427383931, -191.488369613846, -166.799480062655,
-171.446527550207, -156.775073739292, -148.804014388301, -151.79670303645,
-152.599645619669, -113.40778617347, -133.161498039771, -129.929086626311,
-156.827022418459, -146.618934465797, -94.20997521855, -124.514491421933,
-89.8538131075507, -91.6500841191965, -113.887119052802, -109.971138752404,
-122.532101557384, -107.714865018275, -120.258046104427, -133.296582343699,
-111.726093380577, -121.533575165211, -93.2177144160483, -97.3820468959891,
-111.294540122284, -100.913019601926, -90.8456358331201, -94.143801546019,
-89.9179990101121, -79.3197988311624, -87.0020882554621, -103.315348613102,
-92.6091563297179, -87.3900461813473, -106.61173718685, -117.18534688083,
-106.417237368758, -114.977458547258, -101.759575332149, -93.2156781534656,
-79.4452885332048, -83.8035016923155, -85.7770898942133, -80.151862850598,
-72.4787832080729, -75.4887322569144, -70.8074563945233, -76.9714115362765,
-74.2305756099668, -72.7994824773122, -86.4890002605112, -68.5241155035978,
-76.723559709657, -60.5721619300473, -64.185161607482, -81.0777473166996,
-59.7023719535658, -48.0615168793405, -63.7668096308392, -69.0412888697281,
-70.9894443873153, -48.8366672284658, -43.1003899300401, -50.5591327356918,
-65.184353265464, -52.2850955061746, -66.0074402595226, -69.3081002474696,
-69.3876079446205, -53.0919105504865, -69.3289906081927, -63.5198462609014,
-70.9316181916525, -68.753726760587, -57.1360889764902, -45.7297495037924,
-45.0225501894778, -45.7487568762369, -46.486850874694, -46.7437348684622,
-41.1396209831975, -38.5056250833188, -49.8076229939374, -46.9868446480308,
-38.4639795698766, -40.8336417843587, -49.9810721301961, -58.7820616810459,
-51.4348042554539, -56.9132093579117, -53.527642322397, -49.7620292214747,
-54.6821412367706, -66.7962460110546, -52.0791798888695, -43.3022792658504,
-47.5584813904168, -36.3352478868053, -39.8699383685185, -43.9973042247199,
-49.3191665577514, -42.0818922168797, -37.7538461067954, -29.1591142899914,
-31.6815992654018, -35.3664789321372, -29.6435427630652, -45.5528322348158,
-37.0568327299924, -29.6739001502498, -37.0952936191428, -44.1786405275305,
-50.929131558954, -31.7842698176079, -31.4300485665082, -33.960404707688,
-34.0566040705914, -41.2299273666286, -30.1094672132779, -28.6802701495622,
-37.1973277887207, -31.7114680610898, -32.5450840790921, -28.5823680785893,
-27.5268500574853, -15.2389866754792, -19.2079199191329, -28.9156878395334,
-16.5863741697772, -31.6638747515138, -34.679018047114, -32.6358481677933,
-26.3463300811612, -18.542719903123), `Trace 3` = c(4.74984904312473,
-6.83237755613888, -4.15774846489683, 0.0116753456567835, 4.3974208810568,
8.56175743226069, -0.271281722577697, -10.7982854290066, -18.31704442037,
-15.7577103721081, -20.5775487342614, -17.7307614062074, -14.2844531791322,
-5.43267691733291, 2.52755064598145, -0.79977817636393, -8.07074631773528,
-14.6631228094247, -9.70657043283029, -11.4521183354252, -10.8401698276907,
-5495.45466682125, -4324.53856178012, -3525.03574657791, -2774.37269062517,
-2175.8494069729, -1790.54797875842, -1574.71991002551, -1342.90235937345,
-1212.59715782746, -1042.56659527622, -1006.18885759924, -962.27473179716,
-870.459562514915, -847.89784200018, -770.205392585522, -709.279500982242,
-740.484104629636, -739.054108223511, -645.039948927494, -712.347389757369,
-609.734365947307, -613.120930696929, -630.805982834916, -581.828460576409,
-608.843958294059, -602.432567890874, -547.023947351376, -571.475614300591,
-561.707380041648, -535.094375334464, -578.783728464712, -535.290908514374,
-489.922390143035, -522.824368605682, -520.322259788742, -556.271028784585,
-536.474251127913, -546.811709881227, -541.304029428261, -563.702125503968,
-460.326259578172, -348.718859066443, -261.740363110507, -244.084411205067,
-182.533320181224, -205.356578064485, -157.759947570376, -162.214202634405,
-180.011654146528, -176.904445787135, -167.116794266109, -163.416234638164,
-155.821745831568, -134.760994564946, -131.702791057298, -138.598664148971,
-139.818370538306, -134.700400696581, -137.275258643317, -119.921721377143,
-118.187525264885, -118.490007264421, -130.391338033262, -138.05114734155,
-127.505366899025, -132.636233878671, -123.490111075107, -113.42942794286,
-122.40925776411, -114.48254277334, -122.78054774327, -90.9528981496313,
-96.9508488566491, -107.842237007564, -91.0521871208113, -91.0347011279681,
-85.7054147126896, -89.7363815316723, -77.6588521980595, -76.6733051688234,
-85.821987540449, -90.3216600489126, -66.1137192454051, -77.7157754909198,
-89.8637060331546, -80.9149315468257, -71.7434481475478, -97.6600776026528,
-103.405900069071, -83.7502802699966, -77.4600968679799, -77.8705250264031,
-78.9012879762878, -87.4698127883497, -92.9587198547098, -96.1222986385502,
-91.1990314226718, -83.8744116889207, -61.7720026226848, -83.6616594930646,
-91.3899042204887, -74.2552104995002, -94.3117384183545, -81.130091800586,
-87.6638514042855, -79.061763018934, -78.7472359255489, -72.9642062301352,
-83.5759889935287, -74.066505526926, -62.6191749912397, -75.7150349466975,
-71.8704211546101, -68.4798935028027, -77.0470926055507, -76.6024078920054,
-66.5760393943856, -77.8848130194484, -77.5151449198185, -65.5482413040489,
-47.6087884294611, -35.6865291924552, -47.9648700291328, -62.8176093048552,
-48.969044800301, -46.853228499419, -52.562083189922, -58.7677963803543,
-63.6615259259388, -51.7351340481719, -54.5967510836755, -54.1294816023731,
-47.7621427567758, -40.9244974392914, -50.5286026229961, -45.9961132210891,
-43.143208905187, -43.8395623054062, -49.3326618452772, -48.308625153823,
-44.9833219970479, -32.4206723427767, -45.6628898472981, -41.4748918817457,
-43.6438737242104, -42.9844450147366, -30.3729810016378, -44.9519080631137,
-45.8241111308495, -40.4766896430523, -40.3007872752484, -46.4770136239361,
-49.3759491415156, -46.0844075639024, -34.3090862431636, -26.6047381016158,
-28.9119815377167, -33.1619464253006, -37.3813739690468, -46.5001186141503,
-54.5235621985407, -44.5233400090119, -55.2272273265567, -48.088333647706,
-55.3522589332341, -52.8146474401922, -60.1877313269088, -48.2512741329643,
-34.8540879147507, -55.2019332852645, -50.8488894007021, -49.9600753927381,
-48.5654335180739, -47.8918651630979, -45.1405419376454, -40.8504490169926,
-38.5815843253789, -45.001677748311, -43.0547862406721)), .Names = c("Time",
"Trace 1", "Trace 2", "Trace 3"), row.names = c(1000L, 2000L,
3000L, 4000L, 5000L, 6000L, 7000L, 8000L, 9000L, 10000L, 11000L,
12000L, 13000L, 14000L, 15000L, 16000L, 17000L, 18000L, 19000L,
20000L, 21000L, 22000L, 23000L, 24000L, 25000L, 26000L, 27000L,
28000L, 29000L, 30000L, 31000L, 32000L, 33000L, 34000L, 35000L,
36000L, 37000L, 38000L, 39000L, 40000L, 41000L, 42000L, 43000L,
44000L, 45000L, 46000L, 47000L, 48000L, 49000L, 50000L, 51000L,
52000L, 53000L, 54000L, 55000L, 56000L, 57000L, 58000L, 59000L,
60000L, 61000L, 62000L, 63000L, 64000L, 65000L, 66000L, 67000L,
68000L, 69000L, 70000L, 71000L, 72000L, 73000L, 74000L, 75000L,
76000L, 77000L, 78000L, 79000L, 80000L, 81000L, 82000L, 83000L,
84000L, 85000L, 86000L, 87000L, 88000L, 89000L, 90000L, 91000L,
92000L, 93000L, 94000L, 95000L, 96000L, 97000L, 98000L, 99000L,
100000L, 101000L, 102000L, 103000L, 104000L, 105000L, 106000L,
107000L, 108000L, 109000L, 110000L, 111000L, 112000L, 113000L,
114000L, 115000L, 116000L, 117000L, 118000L, 119000L, 120000L,
121000L, 122000L, 123000L, 124000L, 125000L, 126000L, 127000L,
128000L, 129000L, 130000L, 131000L, 132000L, 133000L, 134000L,
135000L, 136000L, 137000L, 138000L, 139000L, 140000L, 141000L,
142000L, 143000L, 144000L, 145000L, 146000L, 147000L, 148000L,
149000L, 150000L, 151000L, 152000L, 153000L, 154000L, 155000L,
156000L, 157000L, 158000L, 159000L, 160000L, 161000L, 162000L,
163000L, 164000L, 165000L, 166000L, 167000L, 168000L, 169000L,
170000L, 171000L, 172000L, 173000L, 174000L, 175000L, 176000L,
177000L, 178000L, 179000L, 180000L, 181000L, 182000L, 183000L,
184000L, 185000L, 186000L, 187000L, 188000L, 189000L, 190000L,
191000L, 192000L, 193000L, 194000L, 195000L, 196000L, 197000L,
198000L, 199000L, 200000L), class = "data.frame")
If you
plot(stackover_data$Time,stackover_data$'Trace 1',type='l'
you can see from time = 2 is where the peak is and I'm looking to fit it from 2 seconds to 6 seconds in ggplot. ( yes I know I used base graphics for the example, its just easier to plot one graph in that vs ggplot2.
Thanks all!
library(ggplot)
# rename the variables, so there's no space in them (is easier in aes())
names(neurons)[2:4] <- paste0('Trace', 1:3)
Now we make a plot, where we use a selection of the data based on the lowest point in the trace:
ggplot(neurons, aes(x = Time, y = Trace1)) +
geom_line() +
geom_smooth(data = subset(neurons, Time >= Time[which.min(Trace1)] &
Time < Time[which.min(Trace1)] + 4)) +
theme_classic() +
xlim(0, 10)
Results in:
Note that the ggplot equivalent of your simple base plot is this:
qplot(neurons$Time, neurons$Trace1, geom = 'line')
Basically, just as easy ;)

Resources