标签: tidyverse

ggplot scale_continuous如何扩展参数?

我试图弄清楚scale_continuous() expand论证是如何运作的.根据scale_continuous文档:

长度为2的数字向量给出乘法和加法扩展常数.这些常量确保数据放置在离轴一定距离的位置.连续变量的默认值为c(0.05,0),离散变量的默认值为c(0,0.6).

由于它们是"扩展常数",因此它们不是实际单位.有没有办法将它们转换为实际测量值以预测实际输出?除了0之外的任何东西,我只是尝试随机数,直到它工作.必须有更合适的方法来解决这个问题.

r ggplot2 tidyverse

13
推荐指数
2
解决办法
1万
查看次数

R Googlsheets:无法在 googlesheets 包中使用 `gs_auth()` - 使用 Google 临时禁用的应用程序登录未验证问题

我无法验证我的 googlesheets 包。每次我运行gs_auth()命令时,我都会被带到 chrome,我通常会在那里登录以启用包来访问我的 googlesheets:

在此处输入图片说明

但是,最近每次执行此操作时,Google 都会出现以下错误:

在此处输入图片说明

这是我的会话信息:

sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 9 (stretch)

Matrix products: default
BLAS/LAPACK: /usr/lib/libopenblasp-r0.2.19.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8   
 [6] LC_MESSAGES=C              LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base     

other attached packages:
[1] googlesheets_0.3.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.2        crayon_1.3.4      dplyr_0.8.3       withr_2.1.2       assertthat_0.2.1  cellranger_1.1.0  R6_2.4.0         
 [8] magrittr_1.5      httr_1.4.1        pillar_1.4.2 …
Run Code Online (Sandbox Code Playgroud)

r google-authentication google-oauth google-signin tidyverse

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

pivot_longer 成多列

尝试使用 pivot_longer。我不知道如何使用“names_sep”或“names_pattern”来解决这个问题。

dat <- tribble(
     ~group,  ~BP,  ~HS,  ~BB, ~lowerBP, ~upperBP, ~lowerHS, ~upperHS, ~lowerBB, ~upperBB,
        "1", 0.51, 0.15, 0.05,     0.16,     0.18,      0.5,     0.52,     0.14,     0.16,
      "2.1", 0.67, 0.09, 0.06,     0.09,     0.11,     0.66,     0.68,     0.08,      0.1,
      "2.2", 0.36, 0.13, 0.07,     0.12,     0.15,     0.34,     0.38,     0.12,     0.14,
      "2.3", 0.09, 0.17, 0.09,     0.13,     0.16,     0.08,     0.11,     0.15,     0.18,
      "2.4", 0.68, 0.12, 0.07,     0.12,     0.14,     0.66,     0.69,     0.11,     0.13,
        "3", 0.53, 0.15, 0.06,     0.14,     0.16,     0.52,     0.53,     0.15,     0.16)
Run Code Online (Sandbox Code Playgroud)

所需的输出(宽数据的第一行)

group names   values lower …
Run Code Online (Sandbox Code Playgroud)

pivot r tidyverse

13
推荐指数
4
解决办法
5892
查看次数

在 Ubuntu 18.04 &amp; R 4.0.2 上安装 tidyr 时出错

在尝试安装该软件包时tidyverse,我在安装依赖项时遇到错误tidyr

这是我收到的消息的尾部:

cpp11.cpp:31:100: error: ‘unmove’ is not a member of ‘cpp11’
     return cpp11::as_sexp(simplifyPieces(cpp11::unmove(cpp11::as_cpp<cpp11::list>(pieces)), cpp11::unmove(cpp11::as_cpp<int>(p)), cpp11::unmove(cpp11::as_cpp<bool>(fillLeft))));
                                                                                                    ^~~~~~
cpp11.cpp:31:138: error: ‘unmove’ is not a member of ‘cpp11’
     return cpp11::as_sexp(simplifyPieces(cpp11::unmove(cpp11::as_cpp<cpp11::list>(pieces)), cpp11::unmove(cpp11::as_cpp<int>(p)), cpp11::unmove(cpp11::as_cpp<bool>(fillLeft))));
                                                                                                                                          ^~~~~~
/usr/lib/R/etc/Makeconf:176: recipe for target 'cpp11.o' failed
make: *** [cpp11.o] Error 1
ERROR: compilation failed for package ‘tidyr’
* removing ‘/home/xxx/yyy/tidyr’
Warning in install.packages :
  installation of package ‘tidyr’ had non-zero exit status
Run Code Online (Sandbox Code Playgroud)

在我幼稚的眼中,问题似乎出在 cpp11 上。我看到了一个关于 tidyr 安装错误由于 cpp11 编译错误的类似问题(R tidyr 包安装因 cpp11 编译错误(扩展模式 x 不包含参数包)而失败 …

r tidyr tidyverse ubuntu-18.04

13
推荐指数
2
解决办法
1244
查看次数

保持连续前 3 个值,将其他所有内容更改为 NA

使用 mtcars 实现可重复性

(这是一个行操作)。我想根据它们的大小连续保留 3 个值(所以基本上前 3 个值将具有价值,其余的一切都更改为 NA)

我尝试使用 pivot_longer 转换为 long 然后过滤,但问题是我想再次转换为宽,因为我想保留数据的结构。

   mtcars %>% 
    pivot_longer(cols = everything()) %>% 
    group_by(name) %>% top_n(3) 
Run Code Online (Sandbox Code Playgroud)

3 行 mtcars 上的示例输出以下

注意:在 mtcars 中,所有 3 行都具有与非 NA 相同的列名值,但在原始数据集中会有所不同。(最好是tidyverse解决方案)

r dplyr tidyverse

13
推荐指数
3
解决办法
350
查看次数

使用 dplyr rename(across(

嘿,我正在尝试通过使用新版本的 dplyr 添加“Last_”来重命名某些列,但我一直收到此错误

Error: `across()` must only be used inside dplyr verbs.
Run Code Online (Sandbox Code Playgroud)

这是我的代码

data %>% rename(across(everything(), ~paste0("Last_", .)))
Run Code Online (Sandbox Code Playgroud)

dplyr 版本:v1.0.2

r rename dplyr tidyverse across

13
推荐指数
2
解决办法
3337
查看次数

tidyverse干扰ggplot2?无法访问map_data

在控制台中运行这些命令,输出为:

> cty0 = ggplot2::map_data("county")
> library(tidyverse)
Loading tidyverse: ggplot2
Loading tidyverse: tibble
Loading tidyverse: tidyr
Loading tidyverse: readr
Loading tidyverse: purrr
Loading tidyverse: dplyr
Conflicts with tidy packages -----------------------------------------------------------------------------------------------
filter(): dplyr, stats
lag():    dplyr, stats
map():    purrr, maps
> cty0 = ggplot2::map_data("county")
Error: ggplot2 doesn't know how to deal with data of class list
Run Code Online (Sandbox Code Playgroud)

我可以调用,map_data("county")直到tidyverse加载,然后失败.如何加载县地图数据tidyverse

r ggplot2 tidyverse

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

R:转换为与 case_when 相同的级别顺序的因子

在进行数据分析时,有时需要将值重新编码为因子以进行组分析。我想保持因子的顺序与case_when. 在这种情况下,顺序应该是"Excellent" "Good" "Fail"。我怎样才能做到这一点而不像在levels=c('Excellent', 'Good', 'Fail')

非常感谢。

library(dplyr, warn.conflicts = FALSE)             

set.seed(1234)                                     
score <- runif(100, min = 0, max = 100)     

Performance <- function(x) {                       
  case_when(                                         
    is.na(x) ~ NA_character_,                          
    x > 80   ~ 'Excellent',                            
    x > 50   ~ 'Good',                                 
    TRUE     ~ 'Fail'                                  
  ) %>% factor(levels=c('Excellent', 'Good', 'Fail'))
}                                                  

performance <- Performance(score)                  
levels(performance)                                
#> [1] "Excellent" "Good"      "Fail"
table(performance)                                 
#> performance
#> Excellent      Good      Fail 
#>        15        30        55
Run Code Online (Sandbox Code Playgroud)

编辑:我的解决方案

最后,我想出了一个解决方案。对于那些有兴趣的人,这是我的解决方案。我写了一个函数fct_case_when(假装是 中的函数forcats …

r data-analysis dplyr tidyverse forcats

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

'不能在顶层使用`!!!` 是什么意思。是什么意思以及如何解决?

我正在尝试创建一个用于创建棒棒糖图的函数ggplot2。我想将所有参数传递...aes()inside geom_point()。但是,我想排除size传递到aes()内部参数geom_segment()(如果您查看a()下面的输出,原因很明显)。因此,我捕获...usingrlang::enquos()而不是按原样传递它。在功能上a(),我传递dotsaes()ggplot()这个作品没有问题。但在功能上b()我得到了错误Can't use '!!!' at top level.

我被困在这一点上,并感谢任何输入来解决这个问题。

library(ggplot2)
data("mtcars")

d <- dplyr::count(mtcars, cyl, am)

a <- function(data, x, y, ...) {
  x <- rlang::enquo(x)
  y <- rlang::enquo(y)
  dots <- rlang::enquos(...)

  ggplot(data, aes(!!x, !!y, !!!dots)) +
    geom_segment(aes(y = 0, xend = !!x, yend = !!y)) +
    geom_point() …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 non-standard-evaluation tidyverse rlang

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

哪些 tidyverse 函数返回小标题?

有些tidyverse函数返回数据帧,但有些函数返回小标题。我找不到关于哪些函数返回哪种数据类型的任何资源,也看不到任何真正可预测的模式。以下是一些示例以供说明:

library(tidyverse)

# Returns dataframe
df1 <- iris %>% mutate(newcol = mean(Sepal.Length))
df2 <- iris %>% filter(Sepal.Length > 5)
df3 <- iris %>% select(Sepal.Length, Species)

class(df1); class(df2); class(df3)

[1] "data.frame"
[1] "data.frame"
[1] "data.frame"

# Returns tibble
df4 <- iris %>% add_count(Species)
df5 <- iris %>% pivot_longer(cols = -Species)
df6 <- iris %>% group_by(Species) %>% mutate(newcol = mean(Sepal.Length))

class(df4); class(df5); class(df6)

[1] "tbl_df"     "tbl"        "data.frame"
[1] "tbl_df"     "tbl"        "data.frame"
[1] "grouped_df" "tbl_df"     "tbl"        "data.frame"
Run Code Online (Sandbox Code Playgroud)

有没有办法告诉函数的返回值类型是什么?或者,在 tidyverse 中有哪些函数返回哪种数据类型的资源?许多 R …

r tidyverse tibble

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