小编bir*_*ird的帖子

如何在 ggplot2 图例中显示向后和向前的箭头?

我有一个dataframe带有箭头坐标的:

arrows = data.frame(direction = factor(c("forward", "backward", "backward")),
                x = c(0, 0, 0),
                xend = c(1, 1, 1),
                y = c(1, 1.2, 1.1),
                yend = c(1, 1.2, 1.1))
Run Code Online (Sandbox Code Playgroud)

我想使用这些数据来绘制箭头 - 有些是向前的,反之亦然。

到目前为止我尝试过:

library(ggplot2)

ggplot() +
        geom_segment(data = arrows, 
                     aes(x, y, xend = xend, yend = yend, col = direction),
                     arrow = arrow(length = unit(0.3, "cm"), type = "closed", ends = c("last", "first")))
Run Code Online (Sandbox Code Playgroud)

其产生:

在此输入图像描述

我想在这个图中解决两个问题:

  1. 我如何确保ggplot“理解”哪个情节是“向前”和“向后”,以便为他们提供正确的图例?

  2. 如何更改图例,使其显示前进方向,并带有向前的箭头和向后的向后箭头?

r ggplot2

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

如何自动递归地绘制 3 个不同的正多边形?

我想绘制 3 个正多边形 - 正方形(4 条边)、六边形(6 条边)和十二边形(12 条边),其方式与下图类似:

在此输入图像描述

到目前为止,我一直在对ggforce包进行硬编码以实现我的目标:

library(ggplot2)
library(ggforce)

df = data.frame(name = c("dodecagon", "square", "hexagon"),
                x0 = c(0.5, 0.5, 0.63),
                y0 = c(0.5, 0.745, 0.74),
                sides = c(12, 4, 6),
                angle = c(0, 0, -0.5),
                r = c(0.2, 0.07, 0.09))

ggplot(data = df) +
        geom_regon(aes(x0 = x0, y0 = y0, sides = sides, angle = angle, r = r, fill = name)) +
        coord_fixed(xlim = c(0, 1), ylim = c(0, 1))
Run Code Online (Sandbox Code Playgroud)

其产生:

在此输入图像描述

正如您所看到的,多边形没有很好地对齐,并且需要非常长的时间才能真正实现我想要实现的目标。

本质上,我想要一个函数,它以十二边形(12 边多边形)的数量作为参数,并在十二边形周围绘制正方形(4 …

r polygon ggplot2 ggforce

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

为什么 quarto 的 pdf 输出中没有显示从属关系?

在我的一份文件yaml中:quarto

\n
---\ntitle: "Hey!"\nauthor:\n  - name: Birdy Bird\n    affiliations:\n      - University of Birds\n      - University of Hummingbirds\nformat: pdf\neditor: visual\n---\n
Run Code Online (Sandbox Code Playgroud)\n

当我使用 IDE 渲染此文件时Rstudio,我得到:

\n

在此输入图像描述

\n

我期待他们的数字的从属关系(例如 1、2 作为上标)会出现在某个地方。

\n

R版本

\n
> sessionInfo()\nR version 4.2.2 (2022-10-31)\nPlatform: aarch64-apple-darwin20 (64-bit)\nRunning under: macOS Ventura 13.1\n\nMatrix products: default\nLAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib\n\nlocale:\n[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8\n\nattached base packages:\n[1] stats     graphics  grDevices utils     datasets  methods   base     \n\nloaded via a namespace (and not attached):\n[1] compiler_4.2.2 tools_4.2.2   \n
Run Code Online (Sandbox Code Playgroud)\n

Rstudio版本

\n
> rstudioapi::versionInfo()\n$citation\n\nTo cite RStudio in publications …
Run Code Online (Sandbox Code Playgroud)

pdf latex r pdflatex quarto

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

RCROSSREF API 错误:res$response_headers$`content-type` ==“application/json;charset=UTF-8”不是 TRUE

代码:

cr_journals(issn=c('1803-2427','2326-4225'))
Run Code Online (Sandbox Code Playgroud)

结果:

Error in cr_GET(endpoint = path, args, todf = FALSE, parse = TRUE, ...) : 
  res$response_headers$`content-type` == "application/json;charset=UTF-8" is not TRUE
Run Code Online (Sandbox Code Playgroud)

不知何故,RCROSSREF API 不会突然工作,并返回一个错误消息,如上所述,用于一个简单的查询,它之前工作得很好,现在我认为可能是我超出了限制,或者 API 方面发生了一些事情。

有谁知道如何解决这样的问题?

api r

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

在 gganimate 中没有获得所需的标题 - 带有表情的动画标签

我想m^2 m^-2使用gganimate. 例如,当我只使用它时,下面给了我我想要的输出ggplot2

library(ggplot2)
ggplot(mtcars, aes(wt, mpg)) + 
        geom_point() +
        labs(title = "Hp" ~ m^2 ~ m^-2)
Run Code Online (Sandbox Code Playgroud)

但是,当我执行以下操作时,gganimate我没有得到 Hp 的变化值,但是{closest_state}

library(gganimate)
ggplot(mtcars, aes(wt, mpg)) + 
        geom_point() + 
        transition_states(hp) +
        labs(title = expression('Hp = {closest_state}' ~ m^2 ~ m^-2))
Run Code Online (Sandbox Code Playgroud)

我怎么能解决这个问题?

r ggplot2 gganimate

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

使用 Rstudio 服务器加载某些包不起作用,而使用 R 服务器则可以

我正在Rstudio server远程服务器上使用,并且已经安装了一些软件包。

\n

当我尝试加载类似rasterterrausing 之类的库时Rstudio server,出现错误:

\n
> library(terra)\n\nError: package or namespace load failed for \xe2\x80\x98terra\xe2\x80\x99 in dyn.load(file, DLLpath = DLLpath, ...):\n unable to load shared object '/home/pearless/R/x86_64-pc-linux-gnu-library/4.1/terra/libs/terra.so':\n  libproj.so.15: cannot open shared object file: No such file or directory\n\n> library(raster)\n\nError: package or namespace load failed for \xe2\x80\x98raster\xe2\x80\x99 in dyn.load(file, DLLpath = DLLpath, ...):\n unable to load shared object '/home/pearless/R/x86_64-pc-linux-gnu-library/4.1/terra/libs/terra.so':\n  libproj.so.15: cannot open shared object file: No such file or directory\n
Run Code Online (Sandbox Code Playgroud)\n

请注意,使用 …

linux r rstudio-server terra

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

将主管和其他信息添加到 bookdown pdf_book 中

我想在 bookdown 中写的论文中包含我的导师的姓名、课程名称等。我特别希望这些信息位于不同的行中。到目前为止,我已经在 yaml 中尝试过:

--- 
title: "Term Paper Title"
author:
        - "Bird"
        - "Supervisor: Hummingbird"
        - "Course: Ornithology"
        - "University: XXX State University"
date: "Summer Semester: 2021"
site: bookdown::bookdown_site
output:
  bookdown::pdf_book:
    includes:
      in_header: preamble.tex
documentclass: book
classoption: openany
bibliography: [book.bib, packages.bib]
biblio-style: apalike
link-citations: yes
mainfont: Times New Roman
fontsize: 12pt
geometry: "left=3cm, right=2cm, top=2.5cm, bottom=2.5cm"
linestretch: 1.5
toc-depth: 1
secnumdepth: 1
---
Run Code Online (Sandbox Code Playgroud)

结果如下:

在此输入图像描述

这里我的名字和主管的名字等等都在一行。我怎样才能强迫他们处于不同的行?例如我想要这样的东西(摘自这篇文章

latex pdflatex pandoc r-markdown bookdown

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

如何避免我非常大的嵌套 for 循环?

我必须使用物理公式来实现一些模拟。在一个公式中,有许多变量。我想用 100 个样本改变这些变量。对于每个样本,我必须使用所有组合进行计算。简化的 for 循环更好地解释了我想要做什么:

set.seed(3)
a = rnorm(100)
b = rnorm(100)
c = rnorm(100)
d = rnorm(100)
f = rnorm(100)

for (a in 1:length(a)) {
        
        for (b in 1:length(b)) {
                
                for (c in 1:length(c)) {
                        
                        for (d in 1:length(d)) {
                                
                                for (f in 1:length(f)) {
                                        
                                        value = a + b / c * d - f # for illustrative purposes only
                                        # .... 
                                        # ... then I append the value to a vector etc.
                                        
                                }
                                
                        }
                        
                }
                
        }
        
} …
Run Code Online (Sandbox Code Playgroud)

loops for-loop r nested-loops

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

如何在不编写正则表达式的情况下解析字符串中的日期?

readr包中有一个名为的函数parse_number,它返回 a 中的数字string

readr::parse_number("Hello 2022!")

[1] 2022
Run Code Online (Sandbox Code Playgroud)

是否有类似的方法从 a 返回日期string?有readr一个名为的函数parse_date,但它做了一些不同的事情:

readr::parse_date("X2018-01-11_poland")

Warning: 1 parsing failure.
row col   expected             actual
  1  -- date like  X2018-01-11_poland

[1] NA
Run Code Online (Sandbox Code Playgroud)

期望的输出:

# the raw string is "X2018-01-11_poland"
2018-01-11
Run Code Online (Sandbox Code Playgroud)

PS 我对使用正则表达式执行此操作不感兴趣。

r date readr

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

为什么 mtcars[, -c("mpg"), drop = F] 不起作用?

这有效:

mtcars[, c("mpg"), drop = F]

                     mpg
Mazda RX4           21.0
Mazda RX4 Wag       21.0
Datsun 710          22.8
Hornet 4 Drive      21.4
Hornet Sportabout   18.7
Valiant             18.1
Duster 360          14.3
Merc 240D           24.4
Merc 230            22.8
Merc 280            19.2
Merc 280C           17.8
Merc 450SE          16.4
Merc 450SL          17.3
Merc 450SLC         15.2
Cadillac Fleetwood  10.4
Lincoln Continental 10.4
Chrysler Imperial   14.7
Fiat 128            32.4
Honda Civic         30.4
Toyota Corolla      33.9
Toyota Corona       21.5
Dodge Challenger    15.5
AMC Javelin         15.2
Camaro Z28          13.3 …
Run Code Online (Sandbox Code Playgroud)

r dataframe

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