- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试复制以下代码( thomasp85/gganimate )。我有两个问题:
geom_point
的标签(变量:partidos); geom_point
的标签(变量:partidos)显示几秒钟。
dput(polls_)
structure(list(week = structure(c(1551571200, 1551571200, 1551571200,
1551571200, 1551571200, 1550966400, 1550966400, 1550966400, 1550966400,
1550966400, 1550361600, 1550361600, 1550361600, 1550361600, 1550361600,
1549756800, 1549756800, 1549756800, 1549756800, 1549756800, 1549152000,
1549152000, 1549152000, 1549152000, 1549152000, 1548547200, 1548547200,
1548547200, 1548547200, 1548547200, 1547942400, 1547942400, 1547942400,
1547942400, 1547942400, 1547337600, 1547337600, 1547337600, 1547337600,
1547337600, 1546732800, 1546732800, 1546732800, 1546732800, 1546732800,
1546128000, 1546128000, 1546128000, 1546128000, 1546128000, 1545523200,
1545523200, 1545523200, 1545523200, 1545523200, 1544918400, 1544918400,
1544918400, 1544918400, 1544918400, 1544313600, 1544313600, 1544313600,
1544313600, 1544313600, 1543708800, 1543708800, 1543708800, 1543708800,
1543708800, 1541894400, 1541894400, 1541894400, 1541894400, 1541894400,
1541289600, 1541289600, 1541289600, 1541289600, 1541289600, 1540684800,
1540684800, 1540684800, 1540684800, 1540684800, 1540080000, 1540080000,
1540080000, 1540080000, 1540080000, 1539475200, 1539475200, 1539475200,
1539475200, 1539475200, 1538870400, 1538870400, 1538870400, 1538870400,
1538870400), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
partidos = c("PPopular", "PSOE", "ahorapodemos", "CiudadanosCs",
"VOX", "PPopular", "PSOE", "ahorapodemos", "CiudadanosCs",
"VOX", "PPopular", "PSOE", "ahorapodemos", "CiudadanosCs",
"VOX", "PPopular", "PSOE", "ahorapodemos", "CiudadanosCs",
"VOX", "PPopular", "PSOE", "ahorapodemos", "CiudadanosCs",
"VOX", "PPopular", "PSOE", "ahorapodemos", "CiudadanosCs",
"VOX", "PPopular", "PSOE", "ahorapodemos", "CiudadanosCs",
"VOX", "PPopular", "PSOE", "ahorapodemos", "CiudadanosCs",
"VOX", "PPopular", "PSOE", "ahorapodemos", "CiudadanosCs",
"VOX", "PPopular", "PSOE", "ahorapodemos", "CiudadanosCs",
"VOX", "PPopular", "PSOE", "ahorapodemos", "CiudadanosCs",
"VOX", "PPopular", "PSOE", "ahorapodemos", "CiudadanosCs",
"VOX", "PPopular", "PSOE", "ahorapodemos", "CiudadanosCs",
"VOX", "PPopular", "PSOE", "ahorapodemos", "CiudadanosCs",
"VOX", "PPopular", "PSOE", "ahorapodemos", "CiudadanosCs",
"VOX", "PPopular", "PSOE", "ahorapodemos", "CiudadanosCs",
"VOX", "PPopular", "PSOE", "ahorapodemos", "CiudadanosCs",
"VOX", "PPopular", "PSOE", "ahorapodemos", "CiudadanosCs",
"VOX", "PPopular", "PSOE", "ahorapodemos", "CiudadanosCs",
"VOX", "PPopular", "PSOE", "ahorapodemos", "CiudadanosCs",
"VOX"), resultados = c(16.7, 33.3, 14.5, 15.3, 5.9, 21, 27.3,
14.2, 16, 11.3, 21.75, 25.85, 14.4, 17.9, 10.9, 20.5, 25.4,
13.9, 17, 11.7, 21.3, 23.9, 13.5, 20.9, 11.2, 22.25, 23.65,
14.85, 19.15, 10.05, 21.5, 23.2, 14.4, 23, 8.9, 19.2, 23.75,
16.7, 18, 8.4, 18.3, 24.1, 16.1, 18.5, 11.5, 20.6, 22.6,
15.5, 19.65, 10.75, 21.8, 23.5, 15.2, 22.7, 7.8, 21.4, 24.15,
16.15, 20.1, 8.6, 22.8, 24.4, 17.2, 19.8, 5.9, 19.7, 23.2,
17.65, 18.2, 10.7, 23.35, 26.4, 17.2, 20.15, 1.8, 22.3, 26.6,
16.6, 21.9, 3.4, 22.65, 24.95, 17.15, 21.55, 2.55, 22.6,
25.2, 17.7, 19.2, 5.1, 26.7, 26.8, 16.8, 19.5, 1.9, 23, 26.2,
18, 20.7, 0)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -100L), vars = "week", indices = list(95:99,
90:94, 85:89, 80:84, 75:79, 70:74, 65:69, 60:64, 55:59, 50:54,
45:49, 40:44, 35:39, 30:34, 25:29, 20:24, 15:19, 10:14, 5:9,
0:4), drop = TRUE, group_sizes = c(5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), biggest_group_size = 5L, labels = structure(list(
week = structure(c(1538870400, 1539475200, 1540080000, 1540684800,
1541289600, 1541894400, 1543708800, 1544313600, 1544918400,
1545523200, 1546128000, 1546732800, 1547337600, 1547942400,
1548547200, 1549152000, 1549756800, 1550361600, 1550966400,
1551571200), class = c("POSIXct", "POSIXt"), tzone = "UTC")), class = "data.frame", row.names = c(NA,
-20L), vars = "week", indices = list(315:319, 310:314, 305:309,
275:304, 270:274, 240:269, 230:239, 195:229, 155:194, 150:154,
140:149, 125:139, 95:124, 90:94, 70:89, 65:69, 50:64, 30:49,
5:29, 0:4), drop = TRUE, group_sizes = c(5L, 5L, 5L, 30L,
5L, 30L, 10L, 35L, 40L, 5L, 10L, 15L, 30L, 5L, 20L, 5L, 15L,
20L, 25L, 5L), biggest_group_size = 40L, labels = structure(list(
week = structure(c(1538870400, 1539475200, 1540080000, 1540684800,
1541289600, 1541894400, 1543708800, 1544313600, 1544918400,
1545523200, 1546128000, 1546732800, 1547337600, 1547942400,
1548547200, 1549152000, 1549756800, 1550361600, 1550966400,
1551571200), tzone = "UTC", class = c("POSIXct", "POSIXt"
))), class = "data.frame", row.names = c(NA, -20L), vars = "week", indices = list(
c(63L, 127L, 191L, 255L, 319L), c(62L, 126L, 190L, 254L,
318L), c(61L, 125L, 189L, 253L, 317L), c(55L, 56L, 57L, 58L,
59L, 60L, 119L, 120L, 121L, 122L, 123L, 124L, 183L, 184L,
185L, 186L, 187L, 188L, 247L, 248L, 249L, 250L, 251L, 252L,
311L, 312L, 313L, 314L, 315L, 316L), c(54L, 118L, 182L, 246L,
310L), c(48L, 49L, 50L, 51L, 52L, 53L, 112L, 113L, 114L,
115L, 116L, 117L, 176L, 177L, 178L, 179L, 180L, 181L, 240L,
241L, 242L, 243L, 244L, 245L, 304L, 305L, 306L, 307L, 308L,
309L), c(46L, 47L, 110L, 111L, 174L, 175L, 238L, 239L, 302L,
303L), c(39L, 40L, 41L, 42L, 43L, 44L, 45L, 103L, 104L, 105L,
106L, 107L, 108L, 109L, 167L, 168L, 169L, 170L, 171L, 172L,
173L, 231L, 232L, 233L, 234L, 235L, 236L, 237L, 295L, 296L,
297L, 298L, 299L, 300L, 301L), c(31L, 32L, 33L, 34L, 35L,
36L, 37L, 38L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L,
159L, 160L, 161L, 162L, 163L, 164L, 165L, 166L, 223L, 224L,
225L, 226L, 227L, 228L, 229L, 230L, 287L, 288L, 289L, 290L,
291L, 292L, 293L, 294L), c(30L, 94L, 158L, 222L, 286L), c(28L,
29L, 92L, 93L, 156L, 157L, 220L, 221L, 284L, 285L), c(25L,
26L, 27L, 89L, 90L, 91L, 153L, 154L, 155L, 217L, 218L, 219L,
281L, 282L, 283L), c(19L, 20L, 21L, 22L, 23L, 24L, 83L, 84L,
85L, 86L, 87L, 88L, 147L, 148L, 149L, 150L, 151L, 152L, 211L,
212L, 213L, 214L, 215L, 216L, 275L, 276L, 277L, 278L, 279L,
280L), c(18L, 82L, 146L, 210L, 274L), c(14L, 15L, 16L, 17L,
78L, 79L, 80L, 81L, 142L, 143L, 144L, 145L, 206L, 207L, 208L,
209L, 270L, 271L, 272L, 273L), c(13L, 77L, 141L, 205L, 269L
), c(10L, 11L, 12L, 74L, 75L, 76L, 138L, 139L, 140L, 202L,
203L, 204L, 266L, 267L, 268L), c(6L, 7L, 8L, 9L, 70L, 71L,
72L, 73L, 134L, 135L, 136L, 137L, 198L, 199L, 200L, 201L,
262L, 263L, 264L, 265L), c(1L, 2L, 3L, 4L, 5L, 65L, 66L,
67L, 68L, 69L, 129L, 130L, 131L, 132L, 133L, 193L, 194L,
195L, 196L, 197L, 257L, 258L, 259L, 260L, 261L), c(0L, 64L,
128L, 192L, 256L)), drop = TRUE, group_sizes = c(5L, 5L,
5L, 30L, 5L, 30L, 10L, 35L, 40L, 5L, 10L, 15L, 30L, 5L, 20L,
5L, 15L, 20L, 25L, 5L), biggest_group_size = 40L)))
anim <- ggplot(polls_, aes(semana, resultados, group = partidos)) +
geom_line() +
geom_segment(aes(xend = as.POSIXct("2019-03-08 00:00:00", tz="UTC"), yend = resultados),
linetype = 2, colour = 'grey') +
geom_point(size = 2) +
geom_text(aes(x = as.POSIXct("2019-03-15 00:00:00", tz="UTC"), label = partidos),
hjust = 0) +
transition_reveal(semana) +
coord_cartesian(clip = 'off') +
labs(title = 'Opinion polling for the 2019 Spanish general election',
y = 'Estimated results', x = 'week') +
theme_minimal() +
theme(plot.margin = margin(5.5, 40, 5.5, 5.5))
animate(anim, width = 900, height = 600, fps = 10, rewind = FALSE, duration = 15)
这里我包括动画:
最佳答案
以下两个步骤可以解决您的两个问题:
ggplot()
之前对其进行排序:polls_ <- arrange(polls_, week)
end_pause = <some positive integer>
在animate(anim, ...)
.注意:示例数据框中的列名称是 week
,而您的代码使用 semana
。我在这里选择前者。
说明:
您的数据框已按最新的 week
排列值在上面,最早的在下面。这不适用于 transition_reveal
的默认参数。 .
来自 ?transition_reveal
:
transition_reveal(along, range = NULL, keep_last = TRUE, id)
哪里keep_last
是 TRUE/FALSE 值,表示是否应为后续帧保留最后一行数据。
最早什么时候week
行位于此位置,由于它们的week
,它们被安排首先出现。值,& 由于 keep_last = TRUE
直到最后都保持可见.
当我们按 week
对行进行排序时另一方面,最新的week
相反,值会排序到底部行。现在keep_last = TRUE
这对我们有利,因为我们希望为所有后续帧保留这些值 - 最重要的是,最后帧,即 end_pause
所在的位置。变得有用。
演示:
library(dplyr)
anim <- polls_ %>%
arrange(week) %>%
ggplot(aes(week, resultados, group = partidos)) +
geom_line() +
geom_segment(aes(xend = as.POSIXct("2019-03-08 00:00:00", tz="UTC"), yend = resultados),
linetype = 2, colour = 'grey') +
geom_point(size = 2) +
geom_text(aes(x = as.POSIXct("2019-03-15 00:00:00", tz="UTC"), label = partidos),
hjust = 0) +
transition_reveal(week) +
coord_cartesian(clip = 'off') +
labs(title = 'Opinion polling for the 2019 Spanish general election',
y = 'Estimated results', x = 'week') +
theme_minimal() +
theme(plot.margin = margin(5.5, 40, 5.5, 5.5))
animate(anim, width = 900, height = 600,
end_pause = 10,
fps = 10, rewind = FALSE, duration = 15)
关于r - geom_text 和暂停动画的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54945861/
我正在制作多个倾斜条形图的实例。由于计数的大小和百分比的差异不同,在某些情况下,其中一个标签(计数)的一部分被推到栏外。在所有情况下,我都需要标签完全位于栏内。如果不重新定位以适合栏内,我需要标签按原
所以我的散点图的一些标签重叠。我曾尝试将 direct.label 与方法“smart.grid”一起使用,但它不会产生适当的结果。这是我目前的图表: 生成它的代码: ggplot(d, aes(x=
我将 ggplot 与 facet_grid 一起使用,我想在每个方面指示每个方面的观察数量。我遵循许多网站上提供的示例,但是当我让它写任何东西时,它会在所有四个图上将所有四个观察数字都写在彼此之上。
我尝试添加文本,如 Position geom_text on dodged barplot 但它不适用于我的简单数据 data=data.frame(s=c(10,13,17,8),
我想调整条形图上的文本。 我尝试调整 hjust/vjust 以按我喜欢的方式显示,但它似乎无法正常工作。 ggplot(data) + geom_bar(aes(name, coun
我正在尝试复制以下代码( thomasp85/gganimate )。我有两个问题: 我不想显示第一个 geom_point 的标签(变量:partidos); 我希望动画结束后,最后一个 geom_
我正在尝试使用 ggplot 在 R 中绘制堆积条形图。我还想在该片段的每条条中包含百分比。我尝试关注帖子 1 , 2 , 3但这些值并不完全在各自的 block 中。我的数据是 dropbox 中的
我有一个使用 ggplot2 制作的条形图.我想使用 geom_text 为每个条添加标签使得标签的文本大小与标签相对应。为此,我使用了以下代码: a <- aggregate(mpg ~ vs +
在文本中插入数学符号不应该像我想象的那么复杂! OTH,甚至看起来相似的例子 ggplot2 facet_wrap with mathematical expression 我仍然无法将Ω(欧米茄)符
我希望仅在堆积条形图的顶部添加标签。 这是我的数据框: #create data frame building <- c("Burj \nKhalifa", "Zifeng \nTower", "Ba
这里我将 colorRed 设置为 TRUE 所以文本是红色的。但是当我将它设置为 FALSE 时,颜色仍然是红色。 如何根据 colorRed 的值设置文本颜色? library(ggplot2)
所以我正在使用 Twitter API 来收集与特定主题相关的信息,我正在可视化的事情之一是设备的流行程度。 到目前为止,我有这个: https://gyazo.com/441a9ab80b943f9
我正在绘制一个图,其中使用 geom_text 将 p 值绘制为文本。 pvalues 在文件 pvaluesmir21combined 中给出。 如果值 pvaluesmir21combined
我想使用 geom_text()(其中 ^ 表示上标)将“R^2 = 70%”添加到我的绘图中。 没有 % 符号,很简单: my.data <- data.frame(x=1:5, y=1:5) p1
希望这里有人能够帮助我解决我正在尝试解决的 ggplot 脚本问题。脚本会多次使用不同的数据,所以需要比较灵活。我几乎在我想要的地方得到了它,但我遇到了一个我无法解决的问题。 该脚本用于在右侧边距中为
我有一个用 GGplot2 制作的情节。现在,当我想更改图中文本点的大小时,文本的大小不会改变。我使用以下代码行: ggplot(data = out, aes(x = V2, y = V1)) +
我需要更改 geom_text() 中的数字格式包括逗号。 我已经看到了相关的问题,但我无法让这些解决方案发挥作用。我已经尝试过“sep =”一个,count/sum(count) 类型,以及我在不知
使用 mtcars ,我正在为 cyl 创建条形图并填写 gear .没有车 cyl==8 & gear==4 .绘制时,我使用了 position_dodge(preserve = "single"
我的问题 我想用一个表达式来注释 ggplot() 的每个方面,该表达式是统计测试产生的值的组合。我无法在 geom_text() 中解析字符串。 一个类似的问题被部分覆盖了here .不幸的是,我无
请考虑以下最小示例: library(ggplot2) library(ggrepel) ggplot(mtcars) + aes(x = mpg, y = qsec) + geom_line
我是一名优秀的程序员,十分优秀!