小编Vic*_*ica的帖子

使用transition_reveal在gganimate中以特定帧/时间点暂停的任何方法?

利用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_lengthtransition_length参数可用,所以我不确定这是否可行.

编辑 …

r gganimate

12
推荐指数
1
解决办法
1335
查看次数

使用不同数据集时,文本相对于绘图区域的一致定位

我生成的几个地块,其中数据可能有不同xy范围。我希望在所有图中放置一个文本注释,相对于 plot area完全相同的位置。

第一标绘,其中I添加具有文本的实施例annotate和位置将其与xy在所述数据单元:

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)

在此处输入图片说明

然后根据另一个数据集创建第二个图,其中xy范围与第一个图不同。

在没有反复试验的情况下,将文本放置在相对于绘图区域完全相同的位置的最佳方法是什么?

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)

text r ggplot2 geom-text

10
推荐指数
2
解决办法
560
查看次数

如何在geom_label_repel中向后面发送覆盖其他标签的箭头?

这应该看起来相当直接,但我找不到任何论据来做到这一点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)

r ggplot2 ggrepel

6
推荐指数
1
解决办法
205
查看次数

GGanimate:带有数值的 geom_text 以十进制数而不是整数进行动画处理

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为整数类型无济于事。

在此处输入图片说明

r ggplot2 gganimate

6
推荐指数
1
解决办法
1750
查看次数

使用devtools构建软件包-抛出错误,尽管已填写“作者”和“维护者”字段为空/空

在我的DESCRIPTION文件中,我使用了AUTHORS@R语法来生成AuthorMaintainer字段,但是在R CMD Check进程中,它不断抛出一条错误消息,指出字段为空:

> checking for file 'bar/DESCRIPTION' ... ERROR
  Required fields missing or empty:
    'Author' 'Maintainer'
Run Code Online (Sandbox Code Playgroud)

以前它在工作,所以我不知道为什么现在不起作用。我已经保存了文件,并使用“清理并重建”重建了pkg。另外,我尝试手动填充AuthorMaintainer字段,但这也不起作用。

在此处输入图片说明

r devtools package rstudio

6
推荐指数
1
解决办法
223
查看次数

使用拼凑排列多个图时,如何将图例合并为图大小图?

虚拟代码:

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(),但这并不完全是我想要的。这就是我得到的:

在此输入图像描述

r ggplot2 patchwork

6
推荐指数
1
解决办法
230
查看次数

删除ggplot2中geom_boxplot中的边框

这应该看起来相对简单,但我找不到可以让我这样做的论据,我已经在 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但摆脱了中位数。

r ggplot2 plotly

5
推荐指数
1
解决办法
5441
查看次数

使用 facet_wrap 和 ggplotly 的第一个和最后一个方面比中间方面大

使用样本数据:

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)

返回类似:

在此处输入图片说明

与第一个和最后一个相比,内部刻面的缩放非常糟糕,并且有很多额外的填充。我试图从这些问题中找到解决方案:

当数量较多时,ggplotly 无法正常工作

R:facet_wrap 无法在 Shiny 应用程序中使用 ggplotly 正确呈现

通过反复试验,我使用panel.spacing.x = unit(-0.5, "line")theme()它,它看起来更好一些,很多额外的填充消失了,但内部方面仍然明显更小。

在此处输入图片说明

同样作为一个额外的问题,但不是那么重要ggplotly(),当我将它们设置在底部时,条形标签是调用中的顶部。这里似乎是一个持续存在的问题,有没有人有一个hacky的解决方法?

编辑:在我的真实数据集中,我需要每个方面的 y 轴标签,因为它们的比例非常不同,所以我将它们保留在示例中,这就是为什么我需要facet_wrap. 我的真实数据集的屏幕截图以供解释:

在此处输入图片说明

r ggplot2 plotly ggplotly

5
推荐指数
1
解决办法
419
查看次数

如何在 dplyr 中使用 recode_factor 重新编码多个因子值?

     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,其中:

具有行100m400m110mh1500mevent可变成为分组为Runs; DT, …

r dplyr recode tidyverse

1
推荐指数
1
解决办法
9614
查看次数