我正在尝试使用 gganimate 创建动画图。
当我将以下因素 dat$period 传递给 transition_states 时,
我得到 3 个静态图像。我更愿意让这些点在州与州之间“移动”。
这是我的代码:
plot <-
ggplot(data = dat, aes(x = age, y = value, color = period)) +
geom_point(size = 3, aes(group = period)) +
facet_wrap(~group)+
transition_states(states=period, transition_length = 2, state_length = 1) +
ease_aes('linear')+
enter_fade()+
exit_fade()
plot
Run Code Online (Sandbox Code Playgroud)
这是我的数据:
record period value age group
1 1 start 45 24 a
2 2 start 6 22 c
3 3 start 23 32 b
4 4 start 67 11 a
5 1 middle …Run Code Online (Sandbox Code Playgroud) 这是我希望我的数据框的外观:
record color size height weight
1 blue large heavy
1 red
2 green small tall thin
Run Code Online (Sandbox Code Playgroud)
但是,数据 (df) 显示如下:
record vars
1 color = "blue", size = "large"
2 color = "green", size = "small"
2 height = "tall", weight = "thin"
1 color = "red", weight = "heavy"
Run Code Online (Sandbox Code Playgroud)
df 的代码
structure(list(record = c(1L, 2L, 2L, 1L), vars = structure(c(1L,
2L, 4L,
3L), .Label = c("color = \"blue\", size = \"large\"",
"color = \"green\", size = \"small\"", "color = …Run Code Online (Sandbox Code Playgroud) 我正在使用case_when()fromdplyr创建以下列,result.
z <- tibble(a = c(40, 30, NA),
b = c(NA, 20, 10))
z %>%
mutate(result = case_when(
!is.na(a) ~ a,
is.na(a) & !is.na(b) ~ b
)
)
Run Code Online (Sandbox Code Playgroud)
上面给出了以下内容:
a b result
<dbl> <dbl> <dbl>
1 40 NA 40
2 30 20 30
3 NA 10 10
Run Code Online (Sandbox Code Playgroud)
但是,我想同时创建另一列 ,它显示从中提取result_logic值的位置(a 或 b)。result输出看起来像这样。
a b result result_logic
<dbl> <dbl> <dbl> <chr>
1 40 NA 40 a
2 30 20 30 a
3 …Run Code Online (Sandbox Code Playgroud) 我正在尝试对文本数据库执行以下搜索。
这是示例数据库,df
df <- data.frame(
id = c(1, 2, 3, 4, 5, 6),
name = c("john doe", "carol jones", "jimmy smith",
"jenny ruiz", "joey jones", "tim brown"),
place = c("reno nevada", "poland maine", "warsaw poland",
"trenton new jersey", "brooklyn new york", "atlanta georgia")
)
Run Code Online (Sandbox Code Playgroud)
我有一个字符串向量,其中包含我要查找的术语。
new_search <- c("poland", "jones")
Run Code Online (Sandbox Code Playgroud)
我将向量传递给 str_detect 以在 df 的任何列中查找 new_search 中的任何字符串,然后返回匹配的行...
df %>%
filter_all(any_vars(str_detect(., paste(new_search, collapse = "|"))))
Run Code Online (Sandbox Code Playgroud)
问题...如何将 str_detect 的结果提取到新列中?
对于返回的每一行......我想生成一个成功匹配的术语列表并将它们放入列表或字符向量(matched_terms)......像这样......
id name place matched_terms
1 2 carol jones poland maine c("jones", "poland")
2 …Run Code Online (Sandbox Code Playgroud) 我想使用特定列中的值作为 gt 表的标题。
标题列中的所有行都具有相同的字符值。
如何引用 tab_header 中的列
我想这样做是因为我正在从一个大型数据框中制作许多表,每个表都有不同的标题
这是示例数据
d <- data.frame(
organism=c("Grasshopper", "Bumblebee", "MycoTB"),
protein=c("Jumpylegs", "venom-1", "rpoB"),
accession=c("Gr1,Gr2,Gr3", "Bbv4,Bbv5,Bbv6", "Mtb1,Mtb2,Mtb3,Mtb4,Mtb5"),
title = c("mytitle", "mytitle", "mytitle")
)
Run Code Online (Sandbox Code Playgroud)
这是我的代码
d %>%
gt() %>%
tab_header(
title = # here I want to get the text from the title column, somthing like .$title[1]
)
Run Code Online (Sandbox Code Playgroud)