我有一个圆形空间,角度0和360是等价的.我想在这个空间中绘制矩形,使矩形可以穿过这个值.但是,我遇到了ggplot2的问题.
base <- ggplot() +
scale_x_continuous(breaks = seq(45, 360, 45), limits = c(0, 360)) +
scale_y_continuous(breaks = seq(0, 1, 0.2), limits = c(0, 1)) +
coord_polar(theta = "x", start = 1.5 * pi, direction = -1)
Run Code Online (Sandbox Code Playgroud)
1.尝试绘制超过xlim的值:
base + geom_rect(aes(xmin = 340, xmax = 380, ymin = 0.4, ymax = 0.6),
color = "darkblue", fill = "steelblue")
#> Warning message:
#> Removed 1 rows containing missing values (geom_rect).
Run Code Online (Sandbox Code Playgroud)
xlim之外的所有值都被删除,因此这不起作用.
2.尝试使用重新调整的值进行绘图
base + geom_rect(aes(xmin = 340, xmax = 380 %% 360, …Run Code Online (Sandbox Code Playgroud) 我正在编写一个 R 包,并且有几个数值向量,用户经常将其用作各种包函数的参数。将这些向量存储在包中以便用户可以轻松访问它们的最佳方式是什么?
我的一个想法是将每个向量保存为inst/data中的数据文件。然后,用户可以在需要时使用数据文件的名称来代替向量(至少,我可以在开发过程中这样做)。我喜欢这个想法,但不确定这个解决方案是否会违反 CRAN 规则/规范或导致任何问题。
# To create one such vector as a data file
octants <- c(90, 135, 180, 225, 270, 315, 360, 45)
devtools::use_data(octants)
# To access this vector in usage
my_function(data, octants)
Run Code Online (Sandbox Code Playgroud)
我的另一个想法是创建一个单独的函数来返回所需的向量。然后用户就可以在需要时调用适当的函数。由于某种原因,这可能比数据更好,但我担心用户忘记()函数名称后面的内容。
# To create the vector within a function
octants <- function() c(90, 135, 180, 225, 270, 315, 360, 45}
# To access this vector in usage
my_function(data, octants()) # works
my_function(data, octants) # doesn't work
Run Code Online (Sandbox Code Playgroud)
有谁知道哪种解决方案更可取或有更好的替代方案吗?
我觉得这应该是一件容易的事了ggplot,tidyverse,lubridate,但我似乎无法找到一个很好的解决方案.
目标:根据年份和月份创建汇总/汇总/分组数据的条形图.
#Libraries
library(tidyverse)
library(lubridate)
# Data
date <- sample(seq(as_date('2013-06-01'), as_date('2014-5-31'), by="day"), 10000, replace = TRUE)
value <- rnorm(10000)
df <- tibble(date, value)
# Summarise
df2 <- df %>%
mutate(year = year(date), month = month(date)) %>%
unite(year_month,year,month) %>%
group_by(year_month) %>%
summarise(avg = mean(value),
cnt = n())
# Plot
ggplot(df2) +
geom_bar(aes(x=year_month, y = avg), stat = 'identity')
Run Code Online (Sandbox Code Playgroud)
当我创建year_month变量时,它自然变成一个字符变量而不是一个日期变量.我也试过分组,year(date), month(date)但后来我无法弄清楚如何使用两个变量作为x轴ggplot.也许这可以通过将日期安排到本月的第一天来解决......?
假设我有一个函数,它使用非标准评估(NSE)从该数据框中获取数据框和不同数量的变量.是否有更快/更直接的方法来计算提供的变量数量而不是select()这些变量并计算列数?
# Works but seems non-ideal
nvar <- function(df, vars) {
vars_en <- rlang::enquo(vars)
df_sub <- dplyr::select(df, !!vars_en)
ncol(df_sub)
}
nvar(mtcars, mpg:hp)
#> 4
Run Code Online (Sandbox Code Playgroud) 我已经阅读了几个关于dplyr编程的指南,我仍然对如何解决使用非标准评估(NSE)评估构造/连接字符串的问题感到困惑.我意识到有更好的方法来解决这个例子,而不是使用NSE,但想要学习如何.
t <- tibble( x_01 = c(1, 2, 3), x_02 = c(4, 5, 6))
i <- 1
Run Code Online (Sandbox Code Playgroud)
这是我想要的结果,但是想要mutate()构造变量:
t %>% mutate(d_01 = x_01 * 2)
#> A tibble: 3 x 3
#> x_01 x_02 d_01
#> <dbl> <dbl> <dbl>
#> 1 1.00 4.00 2.00
#> 2 2.00 5.00 4.00
#> 3 3.00 6.00 6.00
Run Code Online (Sandbox Code Playgroud)
这是我第一次尝试使用字符串:
new <- sprintf("d_%02d", i)
var <- sprintf("x_%02d", i)
t %>% mutate(new = var * 2)
#> Error in mutate_impl(.data, dots) : …Run Code Online (Sandbox Code Playgroud)