Skip to content

Instantly share code, notes, and snippets.

@therohk
Last active March 26, 2020 11:57
Show Gist options
  • Save therohk/cceef36656cfdfa81b0773e52aa30007 to your computer and use it in GitHub Desktop.
Save therohk/cceef36656cfdfa81b0773e52aa30007 to your computer and use it in GitHub Desktop.
plot_slice_freq = function(dfin, start, stend, include=c(), stem_flag=TRUE, floor_unit="halfyear", plot_name="_") {
# ---- publish-date-ungram-hertz
ungram_hz_inc <- dfin %>%
subset(select=c(ungram,publish_date)) %>%
mutate(Date = as.Date(substr(publish_date,1,8), format="%Y%m%d")) %>%
filter(Date>=start & Date<=stend)
if(stem_flag) {
ungram_hz_inc$ungram <- wordStem(ungram_hz_inc$ungram, language="english")
include <- wordStem(include, language="english")
}
ungram_hz_inc <- ungram_hz_inc %>%
filter(ungram %in% include) %>%
mutate(time_floor = floor_date(Date, unit=floor_unit)) %>%
group_by(time_floor, ungram) %>%
summarise(count=n())
ungram_hz_inc <- ungram_hz_inc %>%
ungroup() %>%
complete(nesting(ungram), time_floor)
ungram_hz_inc[is.na(ungram_hz_inc$count), "count"]<-0
ungram_hz_plot <- data.frame(ungram_hz_inc)
ungram_hz_plot$unifctr <- factor(ungram_hz_plot$ungram, levels=include)
plot_title <- paste(plot_name, "ungram hertz", start, "unto", stend, "slby", floor_unit,"ly")
plot_ly(ungram_hz_plot, x = ~time_floor, y = ~log2(count), split = ~unifctr, type = 'scatter', mode = 'lines') %>%
layout(title = plot_title, xaxis = list(rangeslider = list(type = "date")), yaxis = list(title = "freq-log2"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment