Editing Google motion chart - r

I am using Google Motion Chart with Shiny R and I am not able to edit some of the basic things. I am new to Shiny R and already started loving it. Here is my code:
ui.R
library(shiny)
shinyUI((
mainPanel(
h4("Interactive Analytics"),
htmlOutput("view_gviz")
)
))
shiny.R
library(shiny)
library(googleVis)
sample_data <- structure(list(YEAR = c(2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L), STORM = structure(c(3L, 9L, 16L, 25L, 36L, 40L, 46L, 58L, 64L, 70L, 75L, 86L, 91L, 97L, 100L, 8L, 10L, 22L, 29L, 32L, 39L, 52L, 53L, 67L, 72L, 80L, 84L, 5L, 13L, 20L, 24L, 35L, 38L, 51L, 56L, 66L, 73L, 77L, 81L, 92L, 95L, 99L, 106L), .Label = c("alberto", "alex", "allison", "alpha", "ana", "andrea", "arlene", "arthur", "barry", "bertha", "beryl", "beta", "bill", "bonnie", "bret", "chantal", "charley", "chris", "cindy", "claudette", "colin", "cristobal", "danielle", "danny", "dean", "debby", "delta", "dennis", "dolly", "don", "earl", "edouard", "emily", "epsilon", "erika", "erin", "ernesto", "fabian", "fay", "felix", "fiona", "florence", "frances", "franklin", "fred", "gabrielle", "gamma", "gaston", "gert", "gordon", "grace", "gustav", "hanna", "harvey", "helene", "henri", "hermine", "humberto", "ida", "igor", "ike", "ingrid", "irene", "iris", "isaac", "isabel", "isidore", "ivan", "jeanne", "jerry", "jose", "josephine", "juan", "julia", "karen", "karl", "kate", "katia", "katrina", "kyle", "larry", "laura", "lee", "lili", "lisa", "lorenzo", "marco", "maria", "matthew", "melissa", "michelle", "mindy", "nana", "nate", "nicholas", "nicole", "noel", "not named", "odette", "olga", "omar", "ophelia", "otto", "paloma", "paula", "peter", "philippe", "richard", "rina", "rita", "sean", "shary", "stan", "subtrop", "tammy", "tomas", "unnamed", "vince", "wilma", "zeta"), class = "factor"), MAX_PRESSURE = c(1012L, 1018L, 1012L, 1013L, 1015L, 1009L, 1010L, 1012L, 1011L, 1008L, 1009L, 1010L, 1006L, 1000L, 1006L, 1009L, 1013L, 1008L, 1009L, 1013L, 1009L, 1009L, 1014L, 1009L, 1016L, 1012L, 1009L, 1008L, 1009L, 1016L, 1024L, 1011L, 1009L, 1013L, 1013L, 1009L, 1009L, 1008L, 1009L, 1008L, 1014L, 1006L, 1009L), AVERAGE_PRESSURE = c(1006.05, 1006.37, 1006.78, 1005.37, 991, 991.32, 992.04, 993, 991.09, 1006.79, 992.15, 1008.5, 979.27, 991.25, 992.82, 1000.29, 1010.85, 1002.3, 1002.73, 1007.25, 1005.4, 983.81, 1003.86, 984.83, 1010.44, 1001.45, 992.67, 1001.64, 1006.14, 1002.25, 1014.29, 1001.7, 965.33, 1009.5, 1005.42, 955.5, 986.57, 984.46, 1002.58, 1006.5, 1004.62, 1000.4, 1002.13), MIN_PRESSURE = c(1000L, 990L, 997L, 994L, 968L, 962L, 975L, 970L, 948L, 1004L, 982L, 1007L, 934L, 986L, 973L, 992L, 1008L, 999L, 997L, 1002L, 998L, 960L, 1001L, 934L, 1004L, 980L, 940L, 994L, 997L, 982L, 1000L, 988L, 939L, 1007L, 997L, 915L, 969L, 952L, 993L, 1002L, 990L, 993L, 990L), MAX_WIND_SPEED = c(60L, 70L, 70L, 70L, 120L, 115L, 80L, 105L, 145L, 50L, 80L, 40L, 140L, 75L, 90L, 60L, 40L, 50L, 60L, 65L, 60L, 100L, 60L, 125L, 60L, 85L, 145L, 60L, 60L, 85L, 75L, 75L, 145L, 40L, 60L, 165L, 105L, 125L, 65L, 45L, 70L, 65L, 70L), AVERAGE_WIND_SPEED = c(30.72, 36.45, 47.65, 44.48, 64.67, 62.6, 58.53, 69.23, 73.8, 42, 62.1, 36.87, 77.72, 62.5, 55.63, 47, 26, 43.07, 44.61, 37.25, 25.21, 56.48, 42, 61.03, 40.55, 48.46, 61.05, 43.12, 35.25, 54.88, 39.65, 52, 100.47, 30.41, 35.47, 115.43, 74.04, 73.1, 39.63, 37.85, 41.12, 50.9, 45), MIN_WIND_SPEED = c(15L, 10L, 30L, 30L, 15L, 30L, 30L, 30L, 30L, 30L, 35L, 35L, 35L, 50L, 30L, 35L, 15L, 35L, 25L, 25L, 0L, 25L, 25L, 25L, 35L, 30L, 30L, 0L, 25L, 30L, 25L, 30L, 30L, 15L, 30L, 30L, 30L, 35L, 15L, 30L, 25L, 35L, 35L), MAX_STORM_MOVEMENT_SPEED = c(31L, 13L, 37L, 37L, 36L, 23L, 37L, 32L, 21L, 23L, 48L, 32L, 35L, 23L, 20L, 39L, 11L, 23L, 23L, 12L, 17L, 46L, 14L, 57L, 36L, 24L, 28L, 34L, 24L, 31L, 21L, 26L, 48L, 25L, 18L, 47L, 44L, 56L, 10L, 14L, 23L, 36L, 24L), AVERAGE_STORM_MOVEMENT_SPEED = c(7.85, 6.86, 19.12, 17.82, 13.93, 11.22, 14.8, 13.16, 18.14, 18.88, 14, 13.59, 8.87, 11.18, 8.43, 21.89, 5.36, 5.58, 12.83, 4.89, 4.54, 18.61, 7.85, 11.94, 16.37, 6.97, 12.33, 14.64, 13.05, 14.59, 10.13, 18.55, 17.07, 11.81, 6.7, 12.19, 12.19, 17.61, 3.75, 9.38, 9.24, 17.14, 13.14), MIN_STORM_MOVEMENT_SPEED = c(1L, 2L, 3L, 0L, 3L, 1L, 2L, 8L, 13L, 10L, 5L, 4L, 1L, 1L, 1L, 5L, 2L, 3L, 0L, 0L, 2L, 8L, 3L, 2L, 5L, 1L, 3L, 4L, 3L, 2L, 3L, 13L, 6L, 6L, 2L, 5L, 2L, 5L, 0L, 5L, 1L, 4L, 3L), STORM_LENGTH = c(14L, 6L, 8L, 7L, 16L, 12L, 10L, 6L, 5L, 2L, 4L, 4L, 8L, 2L, 11L, 5L, 5L, 3L, 6L, 5L, 6L, 7L, 3L, 13L, 2L, 22L, 13L, 9L, 5L, 10L, 11L, 3L, 13L, 3L, 5L, 14L, 5L, 15L, 10L, 4L, 19L, 5L, 4L)), .Names = c("YEAR", "STORM", "MAX_PRESSURE", "AVERAGE_PRESSURE", "MIN_PRESSURE", "MAX_WIND_SPEED", "AVERAGE_WIND_SPEED", "MIN_WIND_SPEED", "MAX_STORM_MOVEMENT_SPEED", "AVERAGE_STORM_MOVEMENT_SPEED", "MIN_STORM_MOVEMENT_SPEED", "STORM_LENGTH"), row.names = c(NA, 43L), class = "data.frame")
shinyServer(function(input, output) {
output$view_gviz <- renderGvis({
chart <- gvisMotionChart(sample_data,
idvar="STORM",
timevar="YEAR",
xvar="AVERAGE_WIND_SPEED",
yvar="AVERAGE_PRESSURE",
colorvar="MAX_STORM_MOVEMENT_SPEED",
sizevar="STORM_LENGTH",
options=list(width=1080, height=500, showChartButtons = FALSE)
)
})
})
Now, here are the list of things that I am not able to do:
I do want to put/edit the tooltips. I have seen one example in Gapminder site. I want to add tooltip over indicators on X-axis, Y-axis, colors and size.
Remove the play button along with playback speed button. It does not sound good idea, but I do want to drag through the slider and no animation over time. It would make sense with full data.
Zoom-in doesn't work with mouse click. We must press enter with keyboard to zoom. Do we have any workaround to fix this?

Unfortunately, I don't think item 1 and 2 are supported by the Google API. Regarding 3, I think this depends on the Flash player you use. Chrome's build in Flash player has caused me issues in the past, while Adobe's Flash player seems to allow the zooming function.
The Google Chart API Forum might be the best place to ask such questions, as they are not directly related to R or shiny.

Related

R datatable rotate header not aligning

I am trying to rotate the header for a datatable. I was able to rotate the header but the rotated header is not aligning with the columns. This is what I have now:
I referenced this for my code : header direction in shiny data table
Here is my code:
datatable(df, rownames = F,class = c("compact"),options = list(paging = F, autoWidth = T,searching= FALSE,
scrollX=T,
initComplete = JS("function(settings, json) {$(this.api().table().header()).css({'font-size' : '12px'});}"),
headerCallback = JS(
"function(thead, data, start, end, display){
var $ths = $(thead).find('th');
$ths.css({'vertical-align': 'bottom', 'white-space': 'nowrap'});
var betterCells = [];
$ths.each(function(){
var cell = $(this);
var newDiv = $('<div>', {height: 'auto', width: cell.height()});
var newInnerDiv = $('<div>', {text: cell.text()});
newDiv.css({margin: 'auto'});
newInnerDiv.css({
transform: 'rotate(220deg)',
'writing-mode': 'tb-rl',
'white-space': 'nowrap'
});
newDiv.append(newInnerDiv);
betterCells.push(newDiv);
});
$ths.each(function(i){
$(this).html(betterCells[i]);
});
}"
))) %>%
formatStyle(columns = c(1:19), `font-size` = '12px')
DATA:
structure(list(diag_category_name = structure(1:18, .Label = c("AIDS",
"Any malignancy", "Cerebrovascular disease", "Chronic pulmonary disease",
"Congestive heart failure", "Dementia", "Diabetes (mild to moderate)",
"Diabetes with chronic complications", "Hemoplegia or paralegia",
"Metastatic solid tumor", "Mild liver disease", "Moderate to severe liver disease",
"Myocardial infarction", "No Charlson Comorbidity", "Peptic ulcer disease",
"Peripheral vascular disease", "Renal disease", "Rheumatologic disease"
), class = "factor"), AIDS = c(20L, 6L, 3L, 5L, 1L, NA, NA, NA,
1L, 14L, 2L, NA, 1L, NA, NA, 3L, 5L, NA), `Any malignancy` = c(6L,
1051L, 108L, 209L, 106L, 34L, 130L, 158L, 14L, NA, 92L, 10L,
86L, NA, 37L, 190L, 161L, 32L), `Cerebrovascular disease` = c(3L,
108L, 421L, 122L, 105L, 44L, 47L, 94L, 35L, 303L, 35L, 7L, 67L,
NA, 23L, 165L, 111L, 15L), `Chronic pulmonary disease` = c(5L,
209L, 122L, 726L, 146L, 34L, 80L, 152L, 23L, 505L, 84L, 15L,
90L, NA, 43L, 234L, 173L, 31L), `Congestive heart failure` = c(1L,
106L, 105L, 146L, 381L, 27L, 37L, 124L, 15L, 269L, 43L, 3L, 121L,
NA, 18L, 166L, 160L, 16L), Dementia = c(NA, 34L, 44L, 34L, 27L,
109L, 11L, 32L, 5L, 74L, 5L, 1L, 26L, NA, 3L, 44L, 44L, 3L),
`Diabetes (mild to moderate)` = c(NA, 130L, 47L, 80L, 37L,
11L, 475L, NA, 8L, 335L, 58L, 9L, 36L, NA, 17L, 79L, 45L,
12L), `Diabetes with chronic complications` = c(NA, 158L,
94L, 152L, 124L, 32L, NA, 573L, 18L, 400L, 75L, 9L, 83L,
NA, 27L, 173L, 229L, 16L), `Hemoplegia or paralegia` = c(1L,
14L, 35L, 23L, 15L, 5L, 8L, 18L, 63L, 45L, 4L, NA, 13L, NA,
4L, 21L, 14L, 3L), `Metastatic solid tumor` = c(14L, NA,
303L, 505L, 269L, 74L, 335L, 400L, 45L, 2778L, 264L, 27L,
186L, NA, 79L, 492L, 455L, 68L), `Mild liver disease` = c(2L,
92L, 35L, 84L, 43L, 5L, 58L, 75L, 4L, 264L, 362L, NA, 29L,
NA, 22L, 73L, 70L, 8L), `Moderate to severe liver disease` = c(NA,
10L, 7L, 15L, 3L, 1L, 9L, 9L, NA, 27L, NA, 38L, 3L, NA, 6L,
11L, 10L, 1L), `Myocardial infarction` = c(1L, 86L, 67L,
90L, 121L, 26L, 36L, 83L, 13L, 186L, 29L, 3L, 277L, NA, 20L,
99L, 86L, 11L), `No Charlson Comorbidity` = c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 49L, NA, NA, NA,
NA), `Peptic ulcer disease` = c(NA, 37L, 23L, 43L, 18L, 3L,
17L, 27L, 4L, 79L, 22L, 6L, 20L, NA, 118L, 33L, 31L, 4L),
`Peripheral vascular disease` = c(3L, 190L, 165L, 234L, 166L,
44L, 79L, 173L, 21L, 492L, 73L, 11L, 99L, NA, 33L, 693L,
197L, 22L), `Renal disease` = c(5L, 161L, 111L, 173L, 160L,
44L, 45L, 229L, 14L, 455L, 70L, 10L, 86L, NA, 31L, 197L,
633L, 24L), `Rheumatologic disease` = c(NA, 32L, 15L, 31L,
16L, 3L, 12L, 16L, 3L, 68L, 8L, 1L, 11L, NA, 4L, 22L, 24L,
106L)), row.names = c(NA, -18L), class = c("data.table",
"data.frame"), .internal.selfref = <pointer: 0x5638d466ed90>, sorted = "diag_category_name")
I have a fix. However, you may need to change some things depending on how you use this. For example, when you add x-axis scrolling overflow is hidden. When the table headings are tilted, there will be an overflow. That equates to some of the header labels getting hidden.
datatable(df1, rownames = F, class = c("compact"),
options = list(
paging = F, autoWidth = F,
searching= FALSE,
scrollX = F,
initComplete = JS("function(settings, json) {
$(this.api().table().header()).css({
'font-size' : '12px'});}"),
headerCallback = JS("function(thead, data, start, end, display){
var $ths = $(thead).find('th');
$ths.css({'vertical-align': 'top', 'padding': '4px 0px',
'transform': 'rotate(180deg)', 'border': 'none'});
var betterCells = [];
$ths.each(function(){
var cell = $(this);
var newDiv = $('<div>', {width: '13px', float: 'left'});
var newInnerDiv = $('<div>', {text: cell.text()});
newDiv.css({margin: 'auto'});
newInnerDiv.css({
'writing-mode': 'vertical-rl',
'white-space': 'nowrap',
'text-align': 'left',
'transform-origin': 'top left',
'transform': 'rotate(45deg)',
'overflow': 'visible'
});
newDiv.append(newInnerDiv);
betterCells.push(newDiv);
});
$ths.each(function(i){
$(this).html(betterCells[i]);
});}"))) %>%
formatStyle(columns = c(1:19), `font-size` = '12px')
There's a lot going on here, and most of it was your code. However, if you have any questions about what's happening here, just let me know.

How to get the n most common items in each group

I'm trying to make a plot, where for each chapter in a book it shows the most common words for that chapter. The problem I'm having is that I'm using the top_n function with a value of 10, but I'm not getting exactly 10 in each facet. Also I would like to know what is the difference here between using count and add_count. Here is the plot:
And the code:
library(tidytext)
library(tidyverse)
notw_processed %>%
filter(chapter < 13) %>%
count(chapter, word) %>%
group_by(chapter) %>%
top_n(10, n) %>%
ungroup() %>%
mutate(word = as_factor(word)) %>%
mutate(word = reorder_within(word, n, chapter)) %>%
ggplot(aes(x = word, y = n)) + geom_col() + coord_flip() +
facet_wrap(~chapter, scale = "free_y") + scale_x_reordered()
And a sample from the data:
dput(notw_processed[sample(1:50000, size = 200, replace = FALSE),])
structure(list(linenumber = c(1884L, 3131L, 41L, 2756L, 1011L,
538L, 3312L, 1856L, 2764L, 2691L, 3702L, 505L, 2090L, 2796L,
1811L, 270L, 228L, 3088L, 3262L, 778L, 1446L, 1696L, 1839L, 1413L,
3961L, 1375L, 306L, 895L, 1647L, 2037L, 822L, 2412L, 3266L, 1287L,
3919L, 3900L, 141L, 1628L, 1459L, 465L, 3309L, 193L, 60L, 4040L,
3276L, 3522L, 682L, 1338L, 394L, 2023L, 2929L, 3239L, 808L, 160L,
206L, 2173L, 3818L, 203L, 383L, 1443L, 1693L, 645L, 1535L, 1974L,
1557L, 3931L, 1877L, 1683L, 1154L, 1601L, 3548L, 1959L, 1625L,
777L, 704L, 3054L, 2152L, 3624L, 2968L, 2035L, 1621L, 2275L,
3625L, 805L, 2731L, 1334L, 2460L, 2294L, 684L, 896L, 371L, 1837L,
2009L, 903L, 1020L, 3300L, 1504L, 1495L, 611L, 2208L, 2277L,
2025L, 1991L, 584L, 1590L, 1468L, 610L, 2683L, 1697L, 156L, 2640L,
3507L, 1975L, 163L, 2807L, 2285L, 1687L, 219L, 4069L, 3983L,
1365L, 176L, 653L, 2226L, 4020L, 3841L, 1915L, 1455L, 486L, 3881L,
2596L, 2252L, 1248L, 3879L, 364L, 2176L, 2304L, 2900L, 75L, 2488L,
1852L, 3504L, 1547L, 2713L, 1574L, 3275L, 3061L, 3368L, 3628L,
3883L, 1701L, 3637L, 3781L, 3042L, 836L, 354L, 2934L, 1781L,
1964L, 113L, 1707L, 2609L, 2066L, 1882L, 3841L, 2362L, 3894L,
466L, 2296L, 1230L, 2250L, 1816L, 3947L, 1668L, 139L, 1872L,
3296L, 2878L, 206L, 2336L, 3852L, 730L, 3956L, 2311L, 373L, 17L,
83L, 626L, 936L, 2165L, 2686L, 4030L, 1582L, 1120L, 1761L, 1002L,
40L, 734L, 3733L, 3933L), chapter = c(23L, 41L, 1L, 37L, 12L,
6L, 43L, 23L, 37L, 37L, 49L, 6L, 27L, 38L, 23L, 3L, 2L, 40L,
43L, 9L, 17L, 22L, 23L, 16L, 52L, 16L, 3L, 11L, 21L, 26L, 10L,
33L, 43L, 15L, 52L, 52L, 1L, 20L, 18L, 5L, 43L, 2L, 1L, 53L,
43L, 46L, 8L, 16L, 4L, 26L, 39L, 43L, 9L, 1L, 2L, 29L, 50L, 2L,
4L, 17L, 22L, 8L, 20L, 26L, 20L, 52L, 23L, 22L, 14L, 20L, 46L,
26L, 20L, 9L, 8L, 40L, 28L, 46L, 40L, 26L, 20L, 31L, 46L, 9L,
37L, 16L, 35L, 31L, 8L, 11L, 3L, 23L, 26L, 11L, 12L, 43L, 19L,
19L, 7L, 30L, 31L, 26L, 26L, 7L, 20L, 18L, 7L, 37L, 22L, 1L,
36L, 45L, 26L, 1L, 38L, 31L, 22L, 2L, 53L, 52L, 16L, 1L, 8L,
31L, 53L, 51L, 24L, 18L, 6L, 52L, 36L, 31L, 14L, 52L, 3L, 29L,
32L, 39L, 1L, 35L, 23L, 45L, 20L, 37L, 20L, 43L, 40L, 43L, 46L,
52L, 22L, 46L, 50L, 40L, 10L, 3L, 39L, 23L, 26L, 1L, 22L, 36L,
26L, 23L, 51L, 32L, 52L, 5L, 31L, 14L, 31L, 23L, 52L, 21L, 1L,
23L, 43L, 38L, 2L, 32L, 51L, 8L, 52L, 32L, 3L, 1L, 1L, 7L, 12L,
29L, 37L, 53L, 20L, 13L, 22L, 12L, 1L, 8L, 50L, 52L), word = c("choose",
"remember", "demon", "manet", "question", "remembering", "finally",
"times", "marks", "false", "approach", "plum", "unable", "head",
"treated", "kote", "chronicler", "method", "locate", "thousand",
"blinding", "hat", "world", "cinder’s", "rallying", "crack",
"building", "expecting", "wrong", "sow", "god", "husband", "fela",
"counter", "wil", "lump", "stew", "ate", "deep", "forehead",
"untarnished", "horse", "west", "series", "archives", "thumb",
"folk", "slight", "don’t", "leaden", "candle’s", "books", "powerful",
"banished", "dried", "spoken", "you’re", "shape", "limping",
"earlier", "customers", "eager", "wagon", "looked", "strangely",
"yesterday", "finally", "frightening", "indignantly", "bit",
"front", "pints", "squash", "taborlin", "trouble", "whipped",
"skarpi", "command", "smile", "considered", "lay", "purse", "eyes",
"symptoms", "tin", "troupers", "luggage", "penny", "bright",
"bricks", "nodded", "mother", "dead", "imply", "should’ve", "front",
"broke", "play", "story", "pulled", "found", "lay", "skarpi",
"knowing", "smelled", "knots", "chronicler", "worth", "shouted",
"stew", "pennies", "university", "pennies", "fine", "boy", "smells",
"sound", "chronicler", "crescent", "stay", "proper", "soldiers",
"tables", "shirt", "hoping", "riot", "boy", "time", "scribe",
"prove", "sync", "haven’t", "talking", "tired", "smith’s", "half",
"half", "plainly", "it’s", "called", "knees", "beck", "wouldn’t",
"tray", "worth", "physically", "moment", "simmon", "simply",
"meat", "forward", "impressive", "scarred", "ayes", "don’t",
"street", "friends", "tanee", "friends", "eyes", "looked", "namer",
"story", "eyes", "mains", "expressions", "shop", "listening",
"lucky", "words", "half", "wicked", "candle", "fever", "fidget",
"shook", "mind", "law", "incredibly", "favor", "grate", "read",
"fierce", "urchin", "they’re", "broke", "chair", "call", "transferred",
"remembered", "tarbean’s", "heard", "hot", "chronicler", "size",
"silly", "wary", "mended", "thin", "dal")), row.names = c(NA,
-200L), class = c("tbl_df", "tbl", "data.frame"))
As pointed out by #StupidWolf, in case of a tie then top_n returns all the ties, so it doesn't have to return exactly 10 cases.
notw_processed %>%
filter(chapter < 13) %>%
count(chapter, word) %>%
group_by(chapter) %>%
top_n(10, n) %>%
slice(1:10) %>%
ungroup() %>%
mutate(word = as_factor(word)) %>%
mutate(word = reorder_within(word, n, chapter)) %>%
ggplot(aes(x = word, y = n)) + geom_col() + coord_flip() +
facet_wrap(~chapter, scale = "free") + scale_x_reordered()
By grouping by chapter and slicing after the top_n call, I can ensure it will be exactly 10 values per facet.

Replacing missing values with group_by median or mean values [duplicate]

This question already has answers here:
replace NA with groups mean in a non specified number of columns [duplicate]
(2 answers)
Closed 3 years ago.
I have been trying various time series imputation models to impute the missing time series using the imputeTS but the test results are quite poor. Since it looks like seasonal time series data I thought about simply imputing the missing values with the median or average over a number of months.
The data looks like:
> head(data, 10)
v1 v2 v3 v4
1 1908 1 118 10
2 1908 2 138 33
3 1908 3 128 17
4 1908 4 NA 60
5 1908 5 NA 114
6 1908 6 72 124
7 1908 7 NA 44
8 1908 8 70 134
9 1908 9 58 121
10 1908 10 42 117
I calculated the median values:
data %>%
as_tibble() %>%
group_by(v2) %>%
summarise(imp_v3 = median(v3, na.rm = TRUE),
imp_v4 = median(v4, na.rm = TRUE))
Which gives:
# A tibble: 12 x 3
v2 imp_v3 imp_v4
<int> <dbl> <dbl>
1 1 126 23
2 2 132 27.5
3 3 138 33
4 4 22.5 76
5 5 42.5 102.
6 6 72 127
7 7 82 50
8 8 80 51
9 9 57 121
10 10 24 98
11 11 145 71
12 12 130. 31
All I want to do now is impute the missing values for each v1 and v2 with the corresponding medians.
That is all missing values in v3 which has a 1 in v2 will be replaced with 126. All missing values in v4 which has a 5 in column v2 would be replaced with 102..
The numbers in v1 are years and the numbers in v2 are months.
Data:
data <- structure(list(v1 = c(1908L, 1908L, 1908L, 1908L, 1908L, 1908L,
1908L, 1908L, 1908L, 1908L, 1908L, 1908L, 1909L, 1909L, 1909L,
1909L, 1909L, 1909L, 1909L, 1909L, 1909L, 1909L, 1909L, 1909L,
1910L, 1910L, 1910L, 1910L, 1910L, 1910L, 1910L, 1910L, 1910L,
1910L, 1910L, 1910L, 1911L, 1911L, 1911L, 1911L, 1911L, 1911L,
1911L, 1911L, 1911L, 1911L, 1911L, 1911L, 1912L, 1912L, 1912L,
1912L, 1912L, 1912L, 1912L, 1912L, 1912L, 1912L, 1912L, 1912L,
1913L, 1913L, 1913L, 1913L, 1913L, 1913L, 1913L, 1913L, 1913L,
1913L, 1913L, 1913L, 1914L, 1914L, 1914L, 1914L, 1914L, 1914L,
1914L, 1914L, 1914L, 1914L, 1914L, 1914L, 1915L, 1915L, 1915L,
1915L, 1915L, 1915L, 1915L, 1915L, 1915L, 1915L, 1915L, 1915L,
1916L, 1916L, 1916L, 1916L, 1916L, 1916L, 1916L, 1916L, 1916L,
1916L, 1916L, 1916L, 1917L, 1917L, 1917L, 1917L, 1917L, 1917L,
1917L, 1917L, 1917L, 1917L, 1917L, 1917L, 1918L, 1918L, 1918L,
1918L, 1918L, 1918L, 1918L, 1918L, 1918L, 1918L, 1918L, 1918L,
1919L, 1919L, 1919L, 1919L, 1919L, 1919L, 1919L, 1919L, 1919L,
1919L, 1919L, 1919L, 1920L, 1920L, 1920L, 1920L, 1920L, 1920L,
1920L, 1920L, 1920L, 1920L, 1920L, 1920L, 1921L, 1921L, 1921L,
1921L, 1921L, 1921L, 1921L, 1921L, 1921L, 1921L, 1921L, 1921L,
1922L, 1922L, 1922L, 1922L, 1922L, 1922L, 1922L, 1922L, 1922L,
1922L, 1922L, 1922L, 1923L, 1923L, 1923L, 1923L, 1923L, 1923L,
1923L, 1923L, 1923L, 1923L, 1923L, 1923L, 1924L, 1924L, 1924L,
1924L, 1924L, 1924L, 1924L, 1924L, 1924L, 1924L, 1924L, 1924L,
1925L, 1925L, 1925L, 1925L, 1925L, 1925L, 1925L, 1925L, 1925L,
1925L, 1925L, 1925L, 1926L, 1926L, 1926L, 1926L, 1926L, 1926L,
1926L, 1926L, 1926L, 1926L, 1926L, 1926L, 1927L, 1927L, 1927L,
1927L, 1927L, 1927L, 1927L, 1927L, 1927L, 1927L, 1927L, 1927L,
1928L, 1928L, 1928L, 1928L, 1928L, 1928L, 1928L, 1928L, 1928L,
1928L, 1928L, 1928L, 1929L, 1929L, 1929L, 1930L, 1930L, 1930L,
1930L, 1930L, 1930L, 1930L, 1930L, 1930L, 1930L, 1930L, 1930L,
1931L, 1931L, 1931L, 1931L, 1931L, 1931L, 1931L, 1931L, 1931L,
1931L, 1931L, 1931L, 1932L, 1932L, 1932L, 1932L, 1932L, 1932L,
1932L, 1932L, 1932L, 1932L, 1932L, 1932L, 1933L, 1933L, 1933L,
1933L, 1933L, 1933L, 1933L, 1933L, 1933L, 1933L, 1933L, 1933L,
1934L, 1934L, 1934L, 1934L, 1934L, 1934L, 1934L, 1934L, 1934L,
1934L, 1934L, 1934L, 1935L, 1935L, 1935L, 1935L, 1935L, 1935L,
1935L, 1935L, 1935L, 1935L, 1935L, 1935L, 1936L, 1936L, 1936L,
1936L, 1936L, 1936L, 1936L, 1936L, 1936L, 1936L, 1936L, 1936L,
1937L, 1937L, 1937L, 1937L, 1937L, 1937L, 1937L, 1937L, 1937L,
1937L, 1937L, 1937L, 1938L, 1938L, 1938L, 1938L, 1938L, 1938L,
1938L, 1938L, 1938L, 1938L, 1938L, 1938L, 1939L, 1939L, 1939L,
1939L, 1939L, 1939L, 1939L, 1939L, 1939L, 1939L, 1939L, 1939L,
1940L, 1940L, 1940L, 1940L, 1940L, 1940L, 1940L, 1940L, 1940L,
1940L, 1940L, 1940L, 1941L, 1941L, 1941L, 1941L, 1941L, 1941L,
1941L, 1941L, 1941L, 1941L, 1941L, 1941L, 1942L, 1942L, 1942L,
1942L, 1942L, 1942L, 1942L, 1942L, 1942L, 1942L, 1942L, 1942L,
1943L, 1943L, 1943L, 1943L, 1943L, 1943L, 1943L, 1943L, 1943L,
1943L, 1943L, 1943L, 1944L, 1944L, 1944L, 1944L, 1944L, 1944L,
1944L, 1944L, 1944L, 1944L, 1944L, 1944L, 1945L, 1945L, 1945L,
1945L, 1945L, 1945L, 1945L, 1945L), v2 = c(1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 10L, 11L, 12L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L,
7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L), v3 = c(118L, 138L, 128L, NA, NA, 72L,
NA, 70L, 58L, 42L, 159L, 125L, 118L, 122L, 123L, 22L, 43L, 45L,
68L, 82L, 41L, 29L, 140L, 120L, 119L, NA, 156L, 161L, 39L, 72L,
65L, 75L, 45L, 26L, 122L, 145L, 125L, 135L, 130L, 3L, 58L, 73L,
103L, 101L, 68L, 16L, 144L, 147L, NA, 132L, NA, 24L, NA, NA,
77L, 46L, 34L, 18L, 148L, 154L, 121L, NA, 148L, 9L, 43L, 69L,
70L, 79L, 64L, 27L, 4L, 134L, 126L, 158L, NA, 37L, 34L, 78L,
86L, 91L, 65L, NA, 156L, 130L, 121L, 129L, 144L, 12L, 36L, 80L,
NA, 77L, 63L, NA, 125L, NA, 160L, 121L, 112L, 16L, 44L, 40L,
NA, 96L, NA, NA, 153L, 112L, 105L, 107L, 122L, 148L, 58L, NA,
92L, 76L, 68L, 5L, 159L, 113L, 127L, 145L, 147L, NA, 64L, 61L,
NA, NA, NA, 20L, 144L, 153L, NA, 106L, 121L, 4L, 68L, 72L, 67L,
83L, NA, 14L, 117L, 137L, 139L, 148L, 4L, 160L, 44L, 70L, 58L,
59L, 54L, 27L, 152L, 128L, 154L, 130L, 162L, 17L, 47L, 80L, 104L,
NA, 72L, 57L, 127L, 155L, 116L, 127L, 129L, 151L, 59L, 65L, NA,
56L, 44L, NA, 153L, 140L, 147L, 128L, 155L, 161L, 17L, NA, 95L,
70L, 47L, 18L, 122L, NA, 126L, 119L, 132L, 160L, 35L, 62L, 75L,
62L, NA, 24L, 154L, 155L, 138L, 134L, 138L, 6L, 42L, 83L, 98L,
82L, 39L, 30L, 125L, 114L, 133L, 147L, 150L, 23L, NA, 67L, 97L,
87L, 66L, 8L, 142L, 130L, 133L, 132L, 158L, 5L, 36L, NA, 81L,
74L, 45L, 31L, 145L, NA, NA, 142L, 143L, 14L, 36L, 49L, 82L,
80L, 54L, 24L, 161L, 124L, 21L, NA, 137L, 141L, 109L, NA, 162L,
34L, 81L, 70L, 80L, 52L, 26L, 150L, 126L, 122L, 119L, 130L, NA,
41L, 62L, 75L, NA, 39L, 22L, 158L, 138L, 153L, 125L, 141L, 158L,
28L, 73L, 84L, 90L, 54L, 16L, 147L, 138L, 112L, NA, 4L, 23L,
51L, 89L, 100L, 99L, 83L, 23L, 141L, 111L, 138L, 136L, 138L,
10L, 48L, 77L, 104L, 80L, 79L, 26L, NA, 157L, 131L, 140L, 154L,
10L, 33L, 81L, 99L, NA, 60L, 20L, 152L, 113L, 118L, 110L, 153L,
NA, 41L, 76L, 78L, 88L, 66L, 25L, 144L, 142L, 135L, 134L, 118L,
16L, 50L, 66L, 90L, NA, 59L, 28L, 142L, 113L, 143L, 133L, 25L,
15L, 38L, 69L, 74L, 87L, 61L, 24L, 11L, 127L, 121L, 141L, 144L,
NA, 45L, 76L, 78L, 87L, 62L, 12L, 3L, 119L, 1L, NA, 154L, 7L,
64L, 102L, 78L, 79L, 53L, 19L, 151L, 126L, 2L, 115L, NA, 146L,
23L, 85L, 103L, 74L, 71L, NA, 151L, 140L, 94L, 93L, 136L, 22L,
40L, 79L, 82L, 85L, 56L, 28L, 141L, 152L, NA, 150L, 162L, 35L,
56L, 72L, 92L, 75L, 51L, 31L, 149L, 126L, 147L, NA, 146L, 26L,
40L, 55L, 82L, 88L, 47L, 13L, 149L, 131L, 108L, 159L, 17L, 32L,
49L, 72L, 95L, 83L), v4 = c(10L, 33L, 17L, 60L, 114L, 124L, 44L,
134L, 121L, 117L, 73L, NA, 15L, 3L, 3L, 72L, 86L, 112L, 42L,
41L, 118L, 106L, 31L, 18L, 5L, 29L, NA, 63L, NA, 133L, 135L,
52L, 122L, 110L, 4L, 75L, NA, 27L, 33L, 78L, 116L, 127L, 51L,
NA, 123L, 94L, 71L, 72L, 15L, NA, 74L, 73L, 103L, 136L, 51L,
129L, 114L, 81L, NA, 63L, 25L, 26L, 60L, 77L, 105L, 132L, 38L,
NA, 37L, NA, 89L, 62L, 21L, 74L, 65L, 82L, 96L, 128L, 49L, 45L,
121L, 109L, NA, 31L, 27L, 22L, 29L, 74L, 87L, 119L, 37L, 36L,
126L, 89L, 6L, 26L, 82L, 19L, NA, 70L, 100L, 114L, 45L, 54L,
126L, 98L, 80L, 6L, 5L, 12L, NA, 22L, 103L, 131L, 44L, 53L, 37L,
75L, NA, 1L, 16L, 68L, 59L, 68L, 110L, NA, 39L, 48L, 118L, 98L,
31L, 76L, 15L, 10L, NA, NA, 110L, 131L, NA, 39L, 120L, 70L, 16L,
59L, NA, NA, 70L, 79L, 107L, 127L, 38L, 134L, 122L, 105L, 83L,
31L, 77L, 33L, NA, NA, 96L, 123L, 53L, 49L, 130L, 125L, 23L,
72L, NA, 19L, 27L, 21L, NA, 123L, 133L, 136L, 121L, 87L, 72L,
72L, 69L, 33L, 67L, 71L, 85L, 120L, NA, 41L, NA, 102L, 19L, 19L,
25L, NA, 9L, NA, 107L, 131L, 43L, 34L, 36L, 104L, 83L, 86L, 61L,
28L, 32L, 73L, 109L, 129L, 54L, 51L, 112L, 101L, 2L, 8L, 29L,
74L, 70L, 87L, 84L, 120L, NA, 44L, NA, 80L, 61L, 30L, 33L, 20L,
74L, 77L, 93L, 111L, 50L, 51L, 129L, 102L, 67L, 5L, 23L, 62L,
62L, 72L, 94L, 115L, 47L, 44L, 122L, 98L, 86L, 1L, 90L, 64L,
63L, 27L, 5L, 24L, 81L, 95L, 133L, NA, NA, 136L, 107L, NA, 29L,
14L, 24L, 7L, 77L, 105L, 135L, 52L, 136L, 120L, 84L, 88L, 63L,
70L, 18L, 26L, 63L, NA, 125L, 56L, 56L, 124L, 85L, NA, 75L, 3L,
24L, 60L, 86L, 114L, 136L, 56L, 57L, NA, NA, 71L, 1L, 29L, 29L,
25L, 78L, 102L, 132L, 58L, 41L, 34L, 111L, 76L, 90L, 33L, 66L,
72L, 78L, 86L, 37L, 52L, 48L, 129L, 98L, NA, NA, 19L, 9L, 71L,
60L, 101L, 133L, 50L, NA, 46L, 96L, NA, 59L, 31L, 30L, 6L, 88L,
113L, NA, 56L, 51L, 126L, 107L, 61L, 17L, NA, 62L, 91L, 74L,
100L, 35L, 43L, 52L, 134L, 108L, 97L, 26L, 23L, 63L, 66L, 80L,
NA, 125L, 49L, 56L, 40L, 86L, 88L, 28L, 13L, 11L, 33L, 75L, 109L,
40L, 43L, 39L, 119L, 99L, 80L, 29L, 12L, NA, 23L, 65L, NA, 130L,
57L, NA, 46L, 109L, 69L, 66L, 10L, NA, 20L, 78L, 97L, 126L, 43L,
58L, 120L, 107L, 66L, 76L, 23L, 69L, 64L, 98L, 96L, 135L, 41L,
47L, 127L, 97L, 74L, 26L, 65L, 20L, 27L, 92L, 98L, 124L, 55L,
53L, 121L, 96L, NA, 27L, NA, 73L, NA, 85L, 106L, 135L, 54L, NA
)), class = "data.frame", row.names = c(NA, -443L))
We can use replace to replace the NA elements with the median of the column
library(dplyr)
data %>%
group_by(v2) %>%
mutate_at(vars(v3, v4), list(~ replace(., is.na(.), median(., na.rm = TRUE))))
# A tibble: 443 x 4
# Groups: v2 [12]
# v1 v2 v3 v4
# <int> <int> <dbl> <dbl>
# 1 1908 1 118 10
# 2 1908 2 138 33
# 3 1908 3 128 17
# 4 1908 4 22.5 60
# 5 1908 5 42.5 114
# 6 1908 6 72 124
# 7 1908 7 82 44
# 8 1908 8 70 134
# 9 1908 9 58 121
#10 1908 10 42 117
# … with 433 more rows
Or another option is na.aggregate from zoo
library(zoo)
data %>%
group_by(v2) %>%
mutate_at(vars(v3, v4), na.aggregate)
By default, the FUN is mean. If we want median specify the FUN
data %>%
group_by(v2) %>%
mutate_at(vars(v3, v4), na.aggregate, FUN = median)

transition_reveal does not work with both geom_point and geom_smooth

I can make through transition_reveal appearing geom_points. Looks nice, but I also wanna add exponential regression and visualise both simultaneously.
Transition_reveal makes render neither with both smooth and points, nor with just smooth. Its possible only for geom_points. Whats wrong?
Here is a code and error. Should be exponential visualisation of decomposition process of tree wood, where t - age of sampling, Pt - its density.
gif <- ggplot(data, aes(t,Pt)) +
geom_point(shape=21, aes(fill=group),col="black",size=3) +
stat_smooth(method="nls", data=decay,
method.args = list(formula = y~a*exp(-b*x),
start = list(a=570,b=2*log(2)/570)),
se=F,size=0.5,
col="black",linetype="dotted") +
transition_reveal(Pt,t)
Error in `[.data.frame`(df, , c("alpha", "colour", "size", "linetype")) : undefined columns are chosen
Data:
structure(list(Pt = c(450L, 430L, 321L, 250L, 560L, 550L, 221L,
221L, 117L, 400L, 420L, 460L, 490L, 430L, 480L, 290L, 330L, 190L,
321L, 540L, 520L, 400L, 560L, 550L, 561L, 540L, 333L, 567L, 580L,
228L, 290L, 190L, 340L, 440L, 311L, 287L, 510L, 500L, 470L, 431L,
417L, 420L, 300L, 400L, 390L, 380L, 350L, 375L, 435L, 317L, 220L,
500L, 490L, 270L, 267L, 298L, 207L, 180L, 205L, 287L, 275L, 520L,
500L, 404L, 392L, 220L, 291L, 300L, 325L, 283L, 310L, 219L, 430L,
290L, 350L, 344L, 523L, 500L, 344L, 570L, 560L, 520L, 87L, 550L,
534L, 400L, 390L, 380L, 350L, 375L, 440L, 303L, 210L, 490L),
P0 = c(570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L,
570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L,
570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L,
570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L,
570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L,
570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L,
570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L,
570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L,
570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L, 570L,
570L, 570L, 570L, 570L, 570L), t = c(12L, 13L, 27L, 29L,
4L, 3L, 42L, 47L, 49L, 8L, 8L, 8L, 7L, 7L, 7L, 12L, 12L,
27L, 27L, 3L, 3L, 11L, 2L, 2L, 2L, 2L, 29L, 1L, 1L, 36L,
35L, 32L, 12L, 13L, 27L, 29L, 4L, 3L, 12L, 11L, 19L, 8L,
18L, 8L, 7L, 7L, 7L, 22L, 13L, 27L, 28L, 3L, 3L, 9L, 38L,
39L, 39L, 41L, 43L, 27L, 29L, 4L, 3L, 12L, 11L, 19L, 8L,
8L, 8L, 7L, 7L, 35L, 7L, 35L, 12L, 27L, 3L, 3L, 11L, 2L,
2L, 2L, 42L, 1L, 3L, 9L, 6L, 7L, 8L, 24L, 13L, 25L, 29L,
5L), k = structure(c(18L, 25L, 23L, 45L, 3L, 7L, 29L, 21L,
51L, 65L, 58L, 40L, 24L, 60L, 33L, 73L, 67L, 62L, 23L, 13L,
49L, 50L, 6L, 12L, 5L, 41L, 14L, 4L, 1L, 38L, 17L, 54L, 63L,
19L, 28L, 31L, 43L, 64L, 8L, 36L, 9L, 58L, 56L, 65L, 72L,
74L, 77L, 16L, 22L, 26L, 52L, 64L, 71L, 80L, 20L, 10L, 39L,
44L, 32L, 37L, 34L, 30L, 64L, 47L, 53L, 70L, 81L, 79L, 78L,
83L, 82L, 42L, 60L, 17L, 61L, 15L, 46L, 64L, 69L, 2L, 6L,
68L, 66L, 57L, 27L, 59L, 76L, 74L, 75L, 11L, 19L, 35L, 55L,
48L), .Label = c("-0,017391743", "0", "0,004424894", "0,005277057",
"0,007957728", "0,008849789", "0,011906028", "0,016075306",
"0,016450007", "0,016629304", "0,017446264", "0,017859041",
"0,018022407", "0,018534271", "0,018703508", "0,019032288",
"0,019307298", "0,019699065", "0,019912433", "0,019957571",
"0,020159014", "0,020791564", "0,02126649", "0,021604424",
"0,021680858", "0,021730911", "0,021746841", "0,022438646",
"0,022558897", "0,022951887", "0,023660488", "0,023782009",
"0,024550037", "0,025133285", "0,025276142", "0,025411661",
"0,025413116", "0,02545252", "0,025972245", "0,026801234",
"0,027033611", "0,027330418", "0,027806409", "0,028114134",
"0,028419843", "0,028684966", "0,028685124", "0,030246194",
"0,030602516", "0,032197438", "0,03231556", "0,034000315",
"0,034034047", "0,034331634", "0,034432029", "0,035658549",
"0,035718083", "0,038172706", "0,039352424", "0,04026445",
"0,040641934", "0,040689344", "0,043057562", "0,043676087",
"0,044271477", "0,044755434", "0,045545309", "0,045903775",
"0,045908609", "0,050105727", "0,050410323", "0,054212803",
"0,056312953", "0,057923587", "0,060962901", "0,06324827",
"0,069671887", "0,070226397", "0,080231736", "0,083023822",
"0,084039137", "0,087009152", "0,100027066"), class = "factor"),
year = c(2006L, 2005L, 1991L, 1989L, 2014L, 2015L, 1976L,
1971L, 1969L, 2010L, 2010L, 2010L, 2011L, 2011L, 2011L, 2006L,
2006L, 1991L, 1991L, 2015L, 2015L, 2007L, 2016L, 2016L, 2016L,
2016L, 1989L, 2017L, 2017L, 1982L, 1983L, 1986L, 2006L, 2005L,
1991L, 1989L, 2014L, 2015L, 2006L, 2007L, 1999L, 2010L, 2000L,
2010L, 2011L, 2011L, 2011L, 1996L, 2005L, 1991L, 1990L, 2015L,
2015L, 2009L, 1980L, 1979L, 1979L, 1977L, 1975L, 1991L, 1989L,
2014L, 2015L, 2006L, 2007L, 1999L, 2010L, 2010L, 2010L, 2011L,
2011L, 1983L, 2011L, 1983L, 2006L, 1991L, 2015L, 2015L, 2007L,
2016L, 2016L, 2016L, 1976L, 2017L, 2015L, 2009L, 2012L, 2011L,
2010L, 1994L, 2005L, 1993L, 1989L, 2013L)), .Names = c("Pt",
"P0", "t", "k", "year"), class = "data.frame", row.names = c(NA,
-94L))
You can try to calculate the regression before the plotting. I have no idea about nls. Thus I used the loess function. The challenge is the combination of independent points and line.
# the static plot
library(tidyverse)
library(gganimate)
d %>%
mutate(gr=factor(case_when(Pt<200 ~ 1,
Pt<300 ~ 2,
Pt<400 ~ 3,
Pt<500 ~ 4,
TRUE ~ 5))) %>%
ggplot(aes(t,Pt)) +
geom_point(aes(fill=gr),
shape=21,col="black",
size=3, show.legend = F) +
geom_smooth(method = "loess",se = FALSE,
col="black",linetype="dotted")
Then you can do
d %>%
mutate(gr=factor(case_when(Pt<200 ~ 1,
Pt<300 ~ 2,
Pt<400 ~ 3,
Pt<500 ~ 4,
TRUE ~ 5))) %>%
arrange(t) %>%
rowid_to_column() %>% # arrange and add rowid for the transition_reveal
mutate(predict=predict(loess(Pt ~ t, data=.), .)) %>% # calculate the predicted values
mutate(predict_lag=lag(predict,default = max(predict)), # add the next values for segments
t_lag=lag(t,default = min(t))) %>%
ggplot(aes(t,Pt)) +
geom_point(aes(fill=gr),
shape=21,col="black",
size=3, show.legend = F) +
geom_segment(aes(x=t, y=predict, xend=t_lag, yend=predict_lag),
col="black",linetype="dotted") +
transition_reveal(rowid, rowid)
Thanks a lot for code above, exponential nls can be built just through
mutate(predict=predict(nls(Pt ~ a*exp(-b*t),start = list(a=570,b=2*log(2)/570), data=.), .)) %>%
instead of mutate(predict=predict(loess(Pt ~ t, data=.), .)) %>%

Calculating column value using a glm

I have a dataset with about 20,000 lines of data. The data are grouped by year and area. I'm trying to calculate the length at which 50% of shrimp are female. When I run the following code, It just returns the value for the entire dataset, not unique values for each year/area pair.
The dataset is named shrimp.
library(MASS)
library(plyr)
Data
shrimp <- structure(list(cruise = c(1972L, 1972L, 1972L, 1972L, 1972L,
1972L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L,
2003L, 2003L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L, 1985L,
1985L, 1985L, 1985L, 1985L, 1985L), areaname = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Balboa",
"Chiniak", "Pavlof"), class = "factor"), size = c(9, 9.5, 10,
10.5, 11, 11.5, 15.5, 16, 16.5, 17, 17.5, 18, 18.5, 19, 19.5,
20, 13.5, 14, 14.5, 15, 15.5, 16, 16.5, 17, 17.5, 18, 18.5, 19
), male = c(49L, 61L, 92L, 57L, 46L, 9L, 151L, 64L, 30L, 0L,
1L, 0L, 0L, 0L, 0L, 0L, 638L, 625L, 666L, 447L, 393L, 214L, 119L,
33L, 12L, 13L, 7L, 6L), female = c(0L, 0L, 0L, 0L, 0L, 0L, 3L,
15L, 35L, 78L, 122L, 105L, 76L, 28L, 13L, 36L, 0L, 5L, 8L, 13L,
38L, 60L, 54L, 38L, 28L, 93L, 131L, 195L), total = c(49L, 61L,
92L, 57L, 46L, 9L, 154L, 79L, 65L, 78L, 123L, 105L, 76L, 28L,
13L, 36L, 638L, 630L, 674L, 460L, 431L, 274L, 173L, 71L, 40L,
106L, 138L, 201L)), .Names = c("cruise", "areaname", "size",
"male", "female", "total"), row.names = c(NA, 28L), class = "data.frame")
Model
cdata <- ddply(shrimp, c("cruise", "areaname"), summarise,
female = dose.p(glm(cbind(female, males) ~ size,
family=binomial(logit), data=shrimp), cf=1:2, p =.5))
There's nothing to test this against (yet) but perhaps:
library(MASS)
library(plyr)
cdata <- ddply(shrimp, c("cruise", "areaname"), summarise,
female = dose.p( glm(cbind(female, male) ~ size,
family=binomial("logit") ), cf=1:2, p = 0.5)
)
## cruise areaname female
## 1 1972 Balboa 67.15131
## 2 1985 Pavlof 16.88196
## 3 2003 Chiniak 16.37031

Resources