利用Github上包的wiki中的这个例子:
airq <- airquality
airq$Month <- format(ISOdate(2004,1:12,1),"%B")[airq$Month]
ggplot(airq, aes(Day, Temp, group = Month)) +
geom_line() +
geom_segment(aes(xend = 31, yend = Temp), linetype = 2, colour = 'grey') +
geom_point(size = 2) +
geom_text(aes(x = 31.1, label = Month), hjust = 0) +
transition_reveal(Month, Day) +
coord_cartesian(clip = 'off') +
labs(title = 'Temperature in New York', y = 'Temperature (°F)') +
theme_minimal() +
theme(plot.margin = margin(5.5, 40, 5.5, 5.5))
Run Code Online (Sandbox Code Playgroud)
产生类似的东西:
我想知道是否有任何方法可以在特定点定义动画中的暂停.例如,在第10天,然后是20,然后在动画结束时,再次循环之前.geom_reveal没有state_length或transition_length参数可用,所以我不确定这是否可行.
编辑 …
我生成的几个地块,其中数据可能有不同x和y范围。我希望在所有图中放置一个文本注释,相对于 plot area完全相同的位置。
第一标绘,其中I添加具有文本的实施例annotate和位置将其与x和y在所述数据单元:
library(tidyverse)
ggplot(mpg) +
geom_point(aes(displ, hwy)) +
annotate("text", x = 6, y = 20, label = "example watermark", size = 8) +
ggsave(filename = "mpg.jpg", width = 10, height = 9, dpi = 60)
Run Code Online (Sandbox Code Playgroud)
然后根据另一个数据集创建第二个图,其中x和y范围与第一个图不同。
在没有反复试验的情况下,将文本放置在相对于绘图区域完全相同的位置的最佳方法是什么?
ggplot(iris) +
geom_point(aes(Petal.Width, Petal.Length)) +
# I don't want to hardcode x and y in annotate
# annotate("text", x = 6, y = …Run Code Online (Sandbox Code Playgroud) 这应该看起来相当直接,但我找不到任何论据来做到这一点ggrepel::geom_label_repel().
数据样本:
df <- structure(list(Athletename = c("Aries Merritt", "Damian Warner"
), Score = c(12.8, 13.44), Event = c("110m hurdles", "110m hurdles"
), Points = c(1135, 1048), Record = c("World Record", "Decathlon Record"
), score_and_points = c("12.8s, 1135pts", "13.44s, 1048pts")), row.names = c(NA,
-2L), class = c("tbl_df", "tbl", "data.frame"), .Names = c("Athletename",
"Score", "Event", "Points", "Record", "score_and_points"))
Run Code Online (Sandbox Code Playgroud)
ggplot2代码:
ggplot(data = data.frame(x = 0), mapping = aes(x = x)) +
geom_point(data = df, aes(x=Score, y=Points, colour=Record)) +
geom_label_repel(data = df, …Run Code Online (Sandbox Code Playgroud) Data <- data.frame(Time = c(1, 1, 1, 2, 2, 2, 3, 3, 3),
Group = c("A", "B", "C", "A", "B", "C", "A", "B", "C"),
Value = c(20, 10, 15, 20, 20, 20, 30, 25, 35))
Run Code Online (Sandbox Code Playgroud)
我在Time.
library(ggplot2)
library(gganimate)
p <- ggplot(Data, aes(Group, Value)) +
geom_col(position = "identity") +
geom_text(aes(label = Value), vjust = -1) +
coord_cartesian(ylim = c(0, 40)) +
transition_time(Time)
p
Run Code Online (Sandbox Code Playgroud)
上面的代码很好地生成了条形转换的动画,但是 geom_text 中的变化还有很多不足之处,因为 geom_text 补间/转换具有 > 10 个小数位。理想情况下,我希望 geom_text 数值在转换时保持为整数,或者以某种方式控制舍入程度。
编辑:更改Value为整数类型无济于事。
在我的DESCRIPTION文件中,我使用了AUTHORS@R语法来生成Author和Maintainer字段,但是在R CMD Check进程中,它不断抛出一条错误消息,指出字段为空:
> checking for file 'bar/DESCRIPTION' ... ERROR
Required fields missing or empty:
'Author' 'Maintainer'
Run Code Online (Sandbox Code Playgroud)
以前它在工作,所以我不知道为什么现在不起作用。我已经保存了文件,并使用“清理并重建”重建了pkg。另外,我尝试手动填充Author和Maintainer字段,但这也不起作用。
虚拟代码:
library(ggplot2)
library(patchwork)
plot1 <- mpg %>% ggplot()
plot2 <- mpg %>% ggplot()
plot3 <- mpg %>% ggplot(aes(cyl, displ)) +
geom_point(aes(colour = manufacturer)) +
guides(colour=guide_legend(ncol=4))
(plot1 + plot2) / plot3
Run Code Online (Sandbox Code Playgroud)
我不是在寻找组合的图例,而是看起来像这样的东西:
图例被“视为”情节的一部分。theme()我尝试过使用and添加边距plot_spacer(),但这并不完全是我想要的。这就是我得到的:
这应该看起来相对简单,但我找不到可以让我这样做的论据,我已经在 Google 和 Stack 上搜索了答案。
示例代码:
library(ggplot2)
library(plotly)
dat <- data.frame(cond = factor(rep(c("A","B"), each=200)), rating = c(rnorm(200),rnorm(200, mean=.8)))
p <- ggplot(dat, aes(x=cond, y=rating, fill=cond)) + geom_boxplot()
p <- ggplotly(p)
Run Code Online (Sandbox Code Playgroud)
这会输出第一个图,我想要第二个图。

我尝试包括colour=cond但摆脱了中位数。
使用样本数据:
library(tidyverse)
library(plotly)
myplot <- diamonds %>% ggplot(aes(clarity, price)) +
geom_boxplot() +
facet_wrap(~ clarity, ncol = 8, scales = "free", strip.position = "bottom") +
theme(axis.ticks.x = element_blank(),
axis.text.x = element_blank(),
axis.title.x = element_blank())
ggplotly(myplot)
Run Code Online (Sandbox Code Playgroud)
返回类似:
与第一个和最后一个相比,内部刻面的缩放非常糟糕,并且有很多额外的填充。我试图从这些问题中找到解决方案:
R:facet_wrap 无法在 Shiny 应用程序中使用 ggplotly 正确呈现
通过反复试验,我使用panel.spacing.x = unit(-0.5, "line")了theme()它,它看起来更好一些,很多额外的填充消失了,但内部方面仍然明显更小。
同样作为一个额外的问题,但不是那么重要ggplotly(),当我将它们设置在底部时,条形标签是调用中的顶部。这里似乎是一个持续存在的问题,有没有人有一个hacky的解决方法?
编辑:在我的真实数据集中,我需要每个方面的 y 轴标签,因为它们的比例非常不同,所以我将它们保留在示例中,这就是为什么我需要facet_wrap. 我的真实数据集的屏幕截图以供解释:
countrycode event
1713 ESP 110mh
1009 NED HJ
536 BLR LJ
2882 FRA 1500m
509 EST LJ
2449 BEL PV
1022 EST HJ
2530 USA JT
2714 CUB JT
1236 HUN 400m
238 BLR 100m
2518 USA JT
1575 FRA 110mh
615 JPN LJ
1144 GER HJ
596 CAN LJ
2477 HUN JT
1046 GER HJ
2501 FIN DT
2176 KAZ PV
Run Code Online (Sandbox Code Playgroud)
我想在我的数据框中创建一个新的因子向量eventtype,其中:
具有行100m,400m,110mh,1500m在event可变成为分组为Runs; DT, …