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.
Related
I have a data.frame fish.test0 for which I want to grep specific variables (in varlist) matching the group column to create a sub-data.frame that will undergo a statistical test. The results of the test is saved in tests.res.t. I want to loop the varlist so that I get one results for each input in varlist
Script:
varlist <- c("Abiotrophia","Alphatorquevirus")
for (i in varlist) {
fish.test <- fish.test0[grep("i",fish.test0$group),]
column <- c("ACDC")
tests <- list()
dat_test <- sapply( column, function(colx)
lapply( unique(fish.test$Merge), function(x)
fisher.test( data.frame(
a=c(( fish.test[ which(fish.test$Merge %in% x)[2],"Present"] -
fish.test[ which(fish.test$Merge %in% x)[2], colx] ),fish.test[ which(fish.test$Merge %in% x)[2], colx]
),
b=c(( fish.test[ which(fish.test$Merge %in% x)[1],"NotPresent"] -
fish.test[ which(fish.test$Merge %in% x)[1], colx] ), fish.test[ which(fish.test$Merge %in% x)[1], colx]))) #,alternative = "greater"
) )
rownames(dat_test) <- unique(fish.test$Merge )
colnames(dat_test) <- column
tests.res <- sapply(dat_test[1:dim(dat_test)[1],1], function(x) {
c(x$estimate[1],
x$estimate[2],
ci.lower = x$conf.int[1],
ci.upper = x$conf.int[2],
p.value = x$p.value)
})
tests.res.t <- as.data.frame(t(tests.res))
}
test-data:
fish.test0 <- structure(list(Present = c(4L, 4L, 9L, 9L, 57L, 57L, 146L, 146L,
91L, 91L, 26L, 26L, 6L, 6L, 12L, 12L, 33L, 33L, 10L, 10L, 66L,
66L, 4L, 4L, 4L, 4L, 9L, 9L, 18L, 18L, 19L, 19L, 51L, 51L, 50L,
50L, 12L, 12L, 7L, 7L, 14L, 14L, 27L, 27L, 9L, 9L, 5L, 5L, 6L,
6L, 22L, 22L, 3L, 3L, 14L, 14L, 4L, 4L, 15L, 15L, 6L, 6L, 8L,
8L, 4L, 4L), NotPresent = c(11L, 11L, 44L, 44L, 126L, 126L, 532L,
532L, 382L, 382L, 97L, 97L, 14L, 14L, 43L, 43L, 85L, 85L, 41L,
41L, 336L, 336L, 19L, 19L, 27L, 27L, 67L, 67L, 108L, 108L, 81L,
81L, 240L, 240L, 258L, 258L, 47L, 47L, 31L, 31L, 82L, 82L, 110L,
110L, 63L, 63L, 178L, 178L, 672L, 672L, 451L, 451L, 120L, 120L,
104L, 104L, 47L, 47L, 387L, 387L, 94L, 94L, 300L, 300L, 133L,
133L), group = c("G__Abiotrophia_NotPresent_Anus", "G__Abiotrophia_Present_Anus",
"G__Abiotrophia_NotPresent_Bile duct", "G__Abiotrophia_Present_Bile duct",
"G__Abiotrophia_NotPresent_Bone/Soft tissue", "G__Abiotrophia_Present_Bone/Soft tissue",
"G__Abiotrophia_NotPresent_Breast", "G__Abiotrophia_Present_Breast",
"G__Abiotrophia_NotPresent_Colorectum", "G__Abiotrophia_Present_Colorectum",
"G__Abiotrophia_NotPresent_Esophagus", "G__Abiotrophia_Present_Esophagus",
"G__Abiotrophia_NotPresent_Gallbladder", "G__Abiotrophia_Present_Gallbladder",
"G__Abiotrophia_NotPresent_Head and neck", "G__Abiotrophia_Present_Head and neck",
"G__Abiotrophia_NotPresent_Kidney", "G__Abiotrophia_Present_Kidney",
"G__Abiotrophia_NotPresent_Liver", "G__Abiotrophia_Present_Liver",
"G__Abiotrophia_NotPresent_Lung", "G__Abiotrophia_Present_Lung",
"G__Abiotrophia_NotPresent_Lymphoid tissue", "G__Abiotrophia_Present_Lymphoid tissue",
"G__Abiotrophia_NotPresent_Mesothelium", "G__Abiotrophia_Present_Mesothelium",
"G__Abiotrophia_NotPresent_Nervous system", "G__Abiotrophia_Present_Nervous system",
"G__Abiotrophia_NotPresent_Ovary", "G__Abiotrophia_Present_Ovary",
"G__Abiotrophia_NotPresent_Pancreas", "G__Abiotrophia_Present_Pancreas",
"G__Abiotrophia_NotPresent_Prostate", "G__Abiotrophia_Present_Prostate",
"G__Abiotrophia_NotPresent_Skin", "G__Abiotrophia_Present_Skin",
"G__Abiotrophia_NotPresent_Small intestine", "G__Abiotrophia_Present_Small intestine",
"G__Abiotrophia_NotPresent_Stomach", "G__Abiotrophia_Present_Stomach",
"G__Abiotrophia_NotPresent_Unknown", "G__Abiotrophia_Present_Unknown",
"G__Abiotrophia_NotPresent_Urothelial tract", "G__Abiotrophia_Present_Urothelial tract",
"G__Abiotrophia_NotPresent_Uterus", "G__Abiotrophia_Present_Uterus",
"G__Alphatorquevirus_NotPresent_Bone/Soft tissue", "G__Alphatorquevirus_Present_Bone/Soft tissue",
"G__Alphatorquevirus_NotPresent_Breast", "G__Alphatorquevirus_Present_Breast",
"G__Alphatorquevirus_NotPresent_Colorectum", "G__Alphatorquevirus_Present_Colorectum",
"G__Alphatorquevirus_NotPresent_Esophagus", "G__Alphatorquevirus_Present_Esophagus",
"G__Alphatorquevirus_NotPresent_Kidney", "G__Alphatorquevirus_Present_Kidney",
"G__Alphatorquevirus_NotPresent_Liver", "G__Alphatorquevirus_Present_Liver",
"G__Alphatorquevirus_NotPresent_Lung", "G__Alphatorquevirus_Present_Lung",
"G__Alphatorquevirus_NotPresent_Pancreas", "G__Alphatorquevirus_Present_Pancreas",
"G__Alphatorquevirus_NotPresent_Skin", "G__Alphatorquevirus_Present_Skin",
"G__Alphatorquevirus_NotPresent_Urothelial tract", "G__Alphatorquevirus_Present_Urothelial tract"
), ABCD = c(3L, 2L, 17L, 6L, 34L, 18L, 240L, 53L, 321L, 73L,
87L, 25L, 6L, 3L, 20L, 8L, 15L, 7L, 19L, 4L, 265L, 42L, 6L, 1L,
4L, 2L, 22L, 4L, 70L, 13L, 54L, 12L, 116L, 33L, 58L, 11L, 6L,
2L, 26L, 6L, 42L, 8L, 74L, 18L, 19L, 3L, 52L, 0L, 288L, 5L, 377L,
17L, 110L, 2L, 19L, 3L, 21L, 2L, 298L, 9L, 60L, 6L, 68L, 1L,
89L, 3L), Total = c(15L, 15L, 53L, 53L, 183L, 183L, 678L, 678L,
473L, 473L, 123L, 123L, 20L, 20L, 55L, 55L, 118L, 118L, 51L,
51L, 402L, 402L, 23L, 23L, 31L, 31L, 76L, 76L, 126L, 126L, 100L,
100L, 291L, 291L, 308L, 308L, 59L, 59L, 38L, 38L, 96L, 96L, 137L,
137L, 72L, 72L, 183L, 183L, 678L, 678L, 473L, 473L, 123L, 123L,
118L, 118L, 51L, 51L, 402L, 402L, 100L, 100L, 308L, 308L, 137L,
137L), Merge = c("Abiotrophia_Anus", "Abiotrophia_Anus", "Abiotrophia_Bile duct",
"Abiotrophia_Bile duct", "Abiotrophia_Bone/Soft tissue", "Abiotrophia_Bone/Soft tissue",
"Abiotrophia_Breast", "Abiotrophia_Breast", "Abiotrophia_Colorectum",
"Abiotrophia_Colorectum", "Abiotrophia_Esophagus", "Abiotrophia_Esophagus",
"Abiotrophia_Gallbladder", "Abiotrophia_Gallbladder", "Abiotrophia_Head and neck",
"Abiotrophia_Head and neck", "Abiotrophia_Kidney", "Abiotrophia_Kidney",
"Abiotrophia_Liver", "Abiotrophia_Liver", "Abiotrophia_Lung",
"Abiotrophia_Lung", "Abiotrophia_Lymphoid tissue", "Abiotrophia_Lymphoid tissue",
"Abiotrophia_Mesothelium", "Abiotrophia_Mesothelium", "Abiotrophia_Nervous system",
"Abiotrophia_Nervous system", "Abiotrophia_Ovary", "Abiotrophia_Ovary",
"Abiotrophia_Pancreas", "Abiotrophia_Pancreas", "Abiotrophia_Prostate",
"Abiotrophia_Prostate", "Abiotrophia_Skin", "Abiotrophia_Skin",
"Abiotrophia_Small intestine", "Abiotrophia_Small intestine",
"Abiotrophia_Stomach", "Abiotrophia_Stomach", "Abiotrophia_Unknown",
"Abiotrophia_Unknown", "Abiotrophia_Urothelial tract", "Abiotrophia_Urothelial tract",
"Abiotrophia_Uterus", "Abiotrophia_Uterus", "Alphatorquevirus_Bone/Soft tissue",
"Alphatorquevirus_Bone/Soft tissue", "Alphatorquevirus_Breast",
"Alphatorquevirus_Breast", "Alphatorquevirus_Colorectum", "Alphatorquevirus_Colorectum",
"Alphatorquevirus_Esophagus", "Alphatorquevirus_Esophagus", "Alphatorquevirus_Kidney",
"Alphatorquevirus_Kidney", "Alphatorquevirus_Liver", "Alphatorquevirus_Liver",
"Alphatorquevirus_Lung", "Alphatorquevirus_Lung", "Alphatorquevirus_Pancreas",
"Alphatorquevirus_Pancreas", "Alphatorquevirus_Skin", "Alphatorquevirus_Skin",
"Alphatorquevirus_Urothelial tract", "Alphatorquevirus_Urothelial tract"
)), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 10L, 9L, 12L,
11L, 13L, 14L, 16L, 15L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L,
25L, 26L, 28L, 27L, 29L, 30L, 31L, 32L, 34L, 33L, 35L, 36L, 38L,
37L, 40L, 39L, 42L, 43L, 45L, 44L, 47L, 46L, 1011L, 1012L, 1014L,
1013L, 1015L, 1016L, 1017L, 1018L, 1019L, 1020L, 1022L, 1021L,
1023L, 1024L, 1026L, 1025L, 1027L, 1028L, 1029L, 1030L), class = "data.frame")
This is probably not an answer but it should help to improve you code. If I'm terribly wrong, I'll remove my answer right away. I have loeft out the test business which I don't understand, but your problem seems to be extraction.
The first thing is that you need to remove the quotation marks in your grep command, try:
varlist <- c("Abiotrophia","Alphatorquevirus")
for( i in varlist )
{
# extract rows which contain the variable
fish.test <- fish.test0[ grep( i, fish.test0$group ), ]
print( head( fish.test ) )
}
From what I understand, you need to define column and tests outside your loop. Does that give you more of what you want:
varlist <- c("Abiotrophia","Alphatorquevirus")
column <- "ACDC"
tests <- list()
for( i in 1 : length( varlist ) ) # index can be used later to fill the list
{
# extract rows which contain the variable
fish.test <- fish.test0[ grep( varlist[ i ], fish.test0$group ), ]
# add a column with your name of choice
fish.test <- cbind( fish.test, c( 1: length( fish.test$group ) ) )
colnames( fish.test )[ length( fish.test ) ] <- column
# write each result into your defined list
tests[[ i ]] <- fish.test
}
This question already has answers here:
Create binary column (0/1) based on condition in another column
(2 answers)
Closed 3 years ago.
I am trying to create a new column in R (yes/no indicator) where if the data in X3 is >= 50 it would = 1(yes) or if <= 49 it would = 0(no).
I have tried various combinations of ifelse statements, I just cannot get it work. I need this step in order to construct my confidence interval.
dput (crime)
structure(list(Y = c(478L, 494L, 643L, 341L, 773L, 603L, 484L,
546L, 424L, 548L, 506L, 819L, 541L, 491L, 514L, 371L, 457L, 437L,
570L, 432L, 619L, 357L, 623L, 547L, 792L, 799L, 439L, 867L, 912L,
462L, 859L, 805L, 652L, 776L, 919L, 732L, 657L, 1419L, 989L,
821L, 1740L, 815L, 760L, 936L, 863L, 783L, 715L, 1504L, 1324L,
940L), X1 = c(184L, 213L, 347L, 565L, 327L, 260L, 325L, 102L,
38L, 226L, 137L, 369L, 109L, 809L, 29L, 245L, 118L, 148L, 387L,
98L, 608L, 218L, 254L, 697L, 827L, 693L, 448L, 942L, 1017L, 216L,
673L, 989L, 630L, 404L, 692L, 1517L, 879L, 631L, 1375L, 1139L,
3545L, 706L, 451L, 433L, 601L, 1024L, 457L, 1441L, 1022L, 1244L
), X2 = c(40L, 32L, 57L, 31L, 67L, 25L, 34L, 33L, 36L, 31L, 35L,
30L, 44L, 32L, 30L, 16L, 29L, 36L, 30L, 23L, 33L, 35L, 38L, 44L,
28L, 35L, 31L, 39L, 27L, 36L, 38L, 46L, 29L, 32L, 39L, 44L, 33L,
43L, 22L, 30L, 86L, 30L, 32L, 43L, 20L, 55L, 44L, 37L, 82L, 66L
), X3 = c(74L, 72L, 70L, 71L, 72L, 68L, 68L, 62L, 69L, 66L, 60L,
81L, 66L, 67L, 65L, 64L, 64L, 62L, 59L, 56L, 46L, 54L, 54L, 45L,
57L, 57L, 61L, 52L, 44L, 43L, 48L, 57L, 47L, 50L, 48L, 49L, 72L,
59L, 49L, 54L, 62L, 47L, 45L, 48L, 69L, 42L, 49L, 57L, 72L, 67L
), X4 = c(11L, 11L, 18L, 11L, 9L, 8L, 12L, 13L, 7L, 9L, 13L,
4L, 9L, 11L, 12L, 10L, 12L, 7L, 15L, 15L, 22L, 14L, 20L, 26L,
12L, 9L, 19L, 17L, 21L, 18L, 19L, 14L, 19L, 19L, 16L, 13L, 13L,
14L, 9L, 13L, 22L, 17L, 34L, 26L, 23L, 23L, 18L, 15L, 22L, 26L
), X5 = c(31L, 43L, 16L, 25L, 29L, 32L, 24L, 28L, 25L, 58L, 21L,
77L, 37L, 37L, 35L, 42L, 21L, 81L, 31L, 50L, 24L, 27L, 22L, 18L,
23L, 60L, 14L, 31L, 24L, 23L, 22L, 25L, 25L, 21L, 32L, 31L, 13L,
21L, 46L, 27L, 18L, 39L, 15L, 23L, 7L, 23L, 30L, 35L, 15L, 18L
), X6 = c(20L, 18L, 16L, 19L, 24L, 15L, 14L, 11L, 12L, 15L, 9L,
36L, 12L, 16L, 11L, 14L, 10L, 27L, 16L, 15L, 8L, 13L, 11L, 8L,
11L, 18L, 12L, 10L, 9L, 8L, 10L, 12L, 9L, 9L, 11L, 14L, 22L,
13L, 13L, 12L, 15L, 11L, 10L, 12L, 12L, 11L, 12L, 13L, 16L, 16L
), X7 = structure(list(Y = c(478L, 494L, 643L, 341L, 773L, 603L,
484L, 546L, 424L, 548L, 506L, 819L, 541L, 491L, 514L, 371L, 457L,
437L, 570L, 432L, 619L, 357L, 623L, 547L, 792L, 799L, 439L, 867L,
912L, 462L, 859L, 805L, 652L, 776L, 919L, 732L, 657L, 1419L,
989L, 821L, 1740L, 815L, 760L, 936L, 863L, 783L, 715L, 1504L,
1324L, 940L), X1 = c(184L, 213L, 347L, 565L, 327L, 260L, 325L,
102L, 38L, 226L, 137L, 369L, 109L, 809L, 29L, 245L, 118L, 148L,
387L, 98L, 608L, 218L, 254L, 697L, 827L, 693L, 448L, 942L, 1017L,
216L, 673L, 989L, 630L, 404L, 692L, 1517L, 879L, 631L, 1375L,
1139L, 3545L, 706L, 451L, 433L, 601L, 1024L, 457L, 1441L, 1022L,
1244L), X2 = c(40L, 32L, 57L, 31L, 67L, 25L, 34L, 33L, 36L, 31L,
35L, 30L, 44L, 32L, 30L, 16L, 29L, 36L, 30L, 23L, 33L, 35L, 38L,
44L, 28L, 35L, 31L, 39L, 27L, 36L, 38L, 46L, 29L, 32L, 39L, 44L,
33L, 43L, 22L, 30L, 86L, 30L, 32L, 43L, 20L, 55L, 44L, 37L, 82L,
66L), X3 = c(74L, 72L, 70L, 71L, 72L, 68L, 68L, 62L, 69L, 66L,
60L, 81L, 66L, 67L, 65L, 64L, 64L, 62L, 59L, 56L, 46L, 54L, 54L,
45L, 57L, 57L, 61L, 52L, 44L, 43L, 48L, 57L, 47L, 50L, 48L, 49L,
72L, 59L, 49L, 54L, 62L, 47L, 45L, 48L, 69L, 42L, 49L, 57L, 72L,
67L), X4 = c(11L, 11L, 18L, 11L, 9L, 8L, 12L, 13L, 7L, 9L, 13L,
4L, 9L, 11L, 12L, 10L, 12L, 7L, 15L, 15L, 22L, 14L, 20L, 26L,
12L, 9L, 19L, 17L, 21L, 18L, 19L, 14L, 19L, 19L, 16L, 13L, 13L,
14L, 9L, 13L, 22L, 17L, 34L, 26L, 23L, 23L, 18L, 15L, 22L, 26L
), X5 = c(31L, 43L, 16L, 25L, 29L, 32L, 24L, 28L, 25L, 58L, 21L,
77L, 37L, 37L, 35L, 42L, 21L, 81L, 31L, 50L, 24L, 27L, 22L, 18L,
23L, 60L, 14L, 31L, 24L, 23L, 22L, 25L, 25L, 21L, 32L, 31L, 13L,
21L, 46L, 27L, 18L, 39L, 15L, 23L, 7L, 23L, 30L, 35L, 15L, 18L
), X6 = c(20L, 18L, 16L, 19L, 24L, 15L, 14L, 11L, 12L, 15L, 9L,
36L, 12L, 16L, 11L, 14L, 10L, 27L, 16L, 15L, 8L, 13L, 11L, 8L,
11L, 18L, 12L, 10L, 9L, 8L, 10L, 12L, 9L, 9L, 11L, 14L, 22L,
13L, 13L, 12L, 15L, 11L, 10L, 12L, 12L, 11L, 12L, 13L, 16L, 16L
), X7 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0,
1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0)), row.names = c(NA, -50L), .Names = c("Y",
"X1", "X2", "X3", "X4", "X5", "X6", "X7"), class = "data.frame")), .Names = c("Y",
"X1", "X2", "X3", "X4", "X5", "X6", "X7"), row.names = c(NA,
-50L), class = "data.frame")
The dput didn't work. But I'd managed to dump the data from column Y and X3 in a dataset (called data) and use dplyr::mutate to do the transformation with a straightforward ifelse condition.
library(dplyr)
data <- data %>% mutate(X3_cat = ifelse(X3 >= 50, 1, 0))
This question already has answers here:
How to put labels over geom_bar in R with ggplot2
(4 answers)
Closed 5 years ago.
Having a dataset like this:
df <- structure(list(word = structure(c(1L, 12L, 23L, 34L, 43L, 44L,
45L, 46L, 47L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 13L,
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 24L, 25L, 26L, 27L,
28L, 29L, 30L, 31L, 32L, 33L, 35L, 36L, 37L, 38L, 39L, 40L, 41L,
42L), .Label = c("word1", "word10", "word11", "word12", "word13",
"word14", "word15", "word16", "word17", "word18", "word19", "word2",
"word20", "word21", "word22", "word23", "word24", "word25", "word26",
"word27", "word28", "word29", "word3", "word30", "word31", "word32",
"word33", "word34", "word35", "word36", "word37", "word38", "word39",
"word4", "word40", "word41", "word42", "word43", "word44", "word45",
"word46", "word47", "word5", "word6", "word7", "word8", "word9"
), class = "factor"), frq = c(1975L, 1665L, 1655L, 1469L, 1464L,
1451L, 1353L, 1309L, 1590L, 1545L, 1557L, 1556L, 1130L, 1153L,
1151L, 1150L, 1144L, 1141L, 1115L, 194L, 195L, 135L, 135L, 130L,
163L, 167L, 164L, 159L, 153L, 145L, 143L, 133L, 133L, 153L, 153L,
150L, 119L, 115L, 115L, 115L, 114L, 113L, 113L, 113L, 115L, 102L,
101L)), .Names = c("word", "frq"), class = "data.frame", row.names = c(NA,
-47L))
With this command lines I produce a bar plot graph
dat2 = transform(df,word = reorder(word,frq))
df2 <- head(dat2, 10)
p = ggplot(df2, aes(x = word, y = frq)) + geom_bar(stat = "identity", fill = "yellow")
p2 <- p +coord_flip()
How is it possible to have the number of frq in the end of every bar?
I would use annotate..
p2 + annotate(geom = "text",x = df2$word, y= df2$frq, label = df2$frq)
I have the following data frame containing water temperature measurements for multiple depths and dates:
wtemp <- structure(list(Date = structure(c(12604, 12604, 12604, 12604,
12604, 12604, 12604, 12604, 12604, 12604, 12604, 12604, 12604,
12604, 12604, 12680, 12680, 12680, 12680, 12680, 12680, 12680,
12680, 12680, 12680, 12680, 12680, 12680, 12680, 12680, 12714,
12714, 12714, 12714, 12714, 12714, 12714, 12714, 12714, 12714,
12714, 12714, 12714, 12714, 12714, 12751, 12751, 12751, 12751,
12751, 12751, 12751, 12751, 12751, 12751, 12751, 12751, 12751,
12751, 12751, 12770, 12770, 12770, 12770, 12770, 12770, 12770,
12770, 12770, 12770, 12770, 12770, 12770, 12770, 12770, 12806,
12806, 12806, 12806, 12806, 12806, 12806, 12806, 12806, 12806,
12806, 12806, 12806, 12806, 12806, 12848, 12848, 12848, 12848,
12848, 12848, 12848, 12848, 12848, 12848, 12848, 12848, 12848,
12848, 12848, 12885, 12885, 12885, 12885, 12885, 12885, 12885,
12885, 12885, 12885, 12885, 12885, 12885, 12885, 12885, 12918,
12918, 12918, 12918, 12918, 12918, 12918, 12918, 12918, 12918,
12918, 12918, 12918, 12918, 12918, 12987, 12987, 12987, 12987,
12987, 12987, 12987, 12987, 12987, 12987, 12987, 12987, 12987,
12987, 12987, 13015, 13015, 13015, 13015, 13015, 13015, 13015,
13015, 13015, 13015, 13015, 13015, 13015, 13015, 13015, 13051,
13051, 13051, 13051, 13051, 13051, 13051, 13051, 13051, 13051,
13051, 13051, 13051, 13051, 13051, 13103, 13103, 13103, 13103,
13103, 13103, 13103, 13103, 13103, 13103, 13103, 13103, 13103,
13103, 13103), class = "Date"), Depth = structure(c(1L, 7L, 15L,
16L, 17L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 1L, 7L,
15L, 16L, 17L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 1L,
7L, 15L, 16L, 17L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L,
1L, 7L, 15L, 16L, 17L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L,
12L, 1L, 7L, 15L, 16L, 17L, 2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L,
11L, 12L, 1L, 7L, 15L, 16L, 17L, 2L, 3L, 4L, 5L, 6L, 8L, 9L,
10L, 11L, 12L, 1L, 7L, 15L, 16L, 17L, 2L, 3L, 4L, 5L, 6L, 8L,
9L, 10L, 11L, 12L, 1L, 7L, 15L, 16L, 17L, 2L, 3L, 4L, 5L, 6L,
8L, 9L, 10L, 11L, 12L, 1L, 7L, 15L, 16L, 17L, 2L, 3L, 4L, 5L,
6L, 8L, 9L, 10L, 11L, 12L, 1L, 7L, 15L, 16L, 17L, 2L, 3L, 4L,
5L, 6L, 8L, 9L, 10L, 11L, 12L, 1L, 7L, 15L, 16L, 17L, 2L, 3L,
4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 1L, 7L, 15L, 16L, 17L, 2L,
3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L, 1L, 7L, 15L, 16L, 17L,
2L, 3L, 4L, 5L, 6L, 8L, 9L, 10L, 11L, 12L), .Label = c("0", "10",
"12", "14", "16", "18", "2", "20", "22", "24", "26", "28", "30",
"32", "4", "6", "8", "AR"), class = "factor"), T_water = c(33L,
33L, 32L, 32L, 31L, 31L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L,
29L, 32L, 32L, 32L, 32L, 31L, 31L, 30L, 30L, 30L, 30L, 30L, 30L,
30L, 30L, 30L, 33L, 33L, 32L, 32L, 31L, 31L, 30L, 30L, 30L, 30L,
30L, 30L, 30L, 30L, 30L, 32L, 32L, 32L, 31L, 31L, 31L, 31L, 31L,
31L, 31L, 31L, 31L, 31L, 30L, 30L, 31L, 31L, 31L, 31L, 31L, 31L,
31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 32L, 30L, 30L, 30L, 30L,
30L, 30L, 30L, 30L, 30L, 31L, 31L, 31L, 31L, 31L, 31L, 30L, 30L,
30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 29L, 29L, 28L, 28L, 28L,
30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L,
30L, 30L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L,
29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L,
29L, 29L, 28L, 28L, 28L, 28L, 31L, 31L, 31L, 31L, 30L, 30L, 30L,
30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 32L, 32L, 32L, 32L, 31L,
31L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 33L, 33L, 32L,
32L, 31L, 31L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L)), .Names = c("Date",
"Depth", "T_water"), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 17L, 18L, 19L, 20L, 21L,
22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 33L, 34L, 35L,
36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 49L,
50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L,
63L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L,
77L, 78L, 79L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L,
91L, 92L, 93L, 94L, 95L, 97L, 98L, 99L, 100L, 101L, 102L, 103L,
104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 113L, 114L, 115L,
116L, 117L, 118L, 119L, 120L, 121L, 122L, 123L, 124L, 125L, 126L,
127L, 131L, 132L, 133L, 134L, 135L, 136L, 137L, 138L, 139L, 140L,
141L, 142L, 143L, 144L, 145L, 149L, 150L, 151L, 152L, 153L, 154L,
155L, 156L, 157L, 158L, 159L, 160L, 161L, 162L, 163L, 167L, 168L,
169L, 170L, 171L, 172L, 173L, 174L, 175L, 176L, 177L, 178L, 179L,
180L, 181L, 185L, 186L, 187L, 188L, 189L, 190L, 191L, 192L, 193L,
194L, 195L, 196L, 197L, 198L, 199L, 203L, 204L, 205L, 206L, 207L,
208L, 209L, 210L, 211L, 212L, 213L, 214L, 215L, 216L, 217L), class = "data.frame")
On the x-axis I need to show the dates, on the y-axis the temperature, and the multiple lines correspond to the depths. Specifically, I am trying to reproduce this figure:
I am trying to use this command:
library(lattice)
xyplot(T_water ~ Date, data = wtemp, type = "l", auto.key=TRUE)
which works only partially. I need to plot the other lines too. It doesn't need to be using lattice, other libraries (such as ggplot2) can be used too.
How to achieve that?
Thanks
Based on the comments, I came out with the following answer:
xyplot(T_water ~ Date, group = Depth, data = wtemp, t='l')
However, the plot had too many lines, and I wanted to keep only depths 0,12 and 24. This is how I've done it:
# Select depths
wtemp <- wtemp[wtemp$Depth %in% c("0","12","24"),]
# Drop old depths
wtemp$Depth <- levels(droplevels(wtemp$Depth))
# Plot new data
xyplot(T_water ~ Date, group = Depth, data = wtemp, t='b',auto.key =list(points = FALSE, columns=3, lines = TRUE))
Thanks to those who helped.
Here is my proposal. I am using the normal plot function and this should work:
# First get a subset of your dataframe of the rows where depth=0
sub <- wtemp[wtemp$Depth==0,]
# Define line colours
colours <- rainbow(14)
# And draw the respective plot
plot(sub$Date,sub$T_water,type="l",col=colours[1], xaxt='n')
# Draw nicer ticks
minDate <- as.Date("2004-07-05")
maxDate <- as.Date("2005-11-16")
axis.Date(1, at=seq(minDate,maxDate ,"month"), format="%m/%d/%Y")
# Loop through all other subsets and create a line which gets added to the plot
for(i in 2:28){
sub <- wtemp[wtemp$Depth==i,]
lines(sub$Date,sub$T_water,col=colours[i/2+1])
}
# Add a legend: http://www.r-bloggers.com/adding-a-legend-to-a-plot/
legend(as.Date("2005-05-05"),33, # legend position
seq(0,28,2), # what to display: values 0 to 28 with an interval of 2
lty=1, # gives the legend appropriate symbols (lines)
lwd=2.5, # line width
col=colours) # gives the legend lines the correct colour as defined above
Update: If you only want to display selected values:
# Define values to display
valueDsp <- c(0,12,24)
# First get a subset of your dataframe of the rows where depth=0
sub <- wtemp[wtemp$Depth==valueDsp[1],]
# Define line colours
colours <- rainbow(length(valueDsp))
# And draw the respective plot
plot(sub$Date,sub$T_water,type="l",col=colours[1], xaxt='n')
# Draw nicer ticks
minDate <- as.Date("2004-07-05")
maxDate <- as.Date("2005-11-16")
axis.Date(1, at=seq(minDate,maxDate ,"month"), format="%m/%d/%Y")
# Loop through all other subsets and create a line which gets added to the plot
for(i in 2:length(valueDsp)){
sub <- wtemp[wtemp$Depth==valueDsp[i],]
lines(sub$Date,sub$T_water,col=colours[i])
}
# Add a legend: http://www.r-bloggers.com/adding-a-legend-to-a-plot/
legend(as.Date("2005-05-05"),33, # legend position
valueDsp, # what to display: values 0 to 28 with an interval of 2
lty=1, # gives the legend appropriate symbols (lines)
lwd=2.5, # line width
col=colours) # gives the legend lines the correct colour as defined above
You can easily adjust the script to change colours, displayed values, legend and so on.
Ps. Are you aware that following months are missing in the dataset:
2004-08
2005-02
2005-06
2005-10
Kind Regards
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.