我会创建几个日期。
library(lubridate)
x <- ymd(c("2012-03-26", "2012-05-04", "2012-09-23", "2012-12-31"))
Run Code Online (Sandbox Code Playgroud)
我可以从这些x值中提取年份和季度。
quarter(x, with_year = TRUE, fiscal_start = 10)
[1] 2012.2 2012.3 2012.4 2013.1
但我似乎不能只提取财政年度。这行不通,但什么会呢?
year(x, with_year = TRUE, fiscal_start = 10)
我收到以下错误消息:
年份错误(x,with_year = TRUE,Financial_start = 10):未使用的参数(with_year = TRUE,Financial_start = 10)
library(tidyverse)
ggplot(mpg, aes(displ, cty)) +
geom_point() +
facet_grid(rows = vars(drv), scales = "free")
Run Code Online (Sandbox Code Playgroud)

上面的代码ggplot包括三个板4,f和r。我希望每个面板的y轴限制如下:
Panel y-min y-max breaks
----- ----- ----- ------
4 5 25 5
f 0 40 10
r 10 20 2
Run Code Online (Sandbox Code Playgroud)
如何修改我的代码来完成此任务?不知道是否scale_y_continuous更有意义,或coord_cartesian,或两者的某种组合。
我刚刚读完“用 dplyr 编程”和“以编程方式定义美学映射”,开始掌握非标准的函数评估。这个职位的具体问题是,“我如何直接写下面的代码使用的tidyverse(例如quo(),!!等)”,而不是基础-R的方法eval(),substitute,etc.。
library(tidyverse)
xy <- data.frame(xvar = 1:10, yvar = 11:20)
plotfunc <- function(data, x, y){
y.sqr <- (eval(substitute(y), envir = data))^2
print(
ggplot(data, aes_q(x = substitute(x), y = substitute(y.sqr))) +
geom_line()
)
}
plotfunc(xy, xvar, yvar)
Run Code Online (Sandbox Code Playgroud)
你能提供答案吗?如果您可以在以下概念中工作,那将是一个奖励,即为什么上面的函数是非标准的,而下面的另一个函数是标准的?我阅读了关于函数和非标准评估的高级 R章节,但此时它在我的脑海中。你能用通俗的语言解释一下吗?下面的函数清晰简洁(对我来说),而上面的函数是一团糟。
rescale01 <- function(x) {
rng <- range(x, na.rm = TRUE)
(x - rng[1]) / (rng[2] - rng[1]) …Run Code Online (Sandbox Code Playgroud) 我的第一个代码块工作正常,尽管我的一次性值999与优雅相反。我试图让第一个酒吧变成红色,然后交替使用蓝色和绿色。可能不是交替我的蓝色和绿色的最佳方式,但它有效。
# FIRST CODE CHUNK
library(tidyverse)
ggplot(mpg, aes(fl)) +
geom_bar(aes(fill = fl)) +
scale_fill_manual(
values = c("red", rep(c("blue", "green"), 999))
)
Run Code Online (Sandbox Code Playgroud)
我想scale_fill_manual()无限地回收蓝色和绿色,但这不起作用。我收到“5 只需要提供 3 个”错误。
# SECOND CODE CHUNK
ggplot(mpg, aes(fl)) +
geom_bar(aes(fill = fl)) +
scale_fill_manual(
values = c(red, c("blue", "green"))
)
Run Code Online (Sandbox Code Playgroud)
如何回收scale_fill_manual()命令中的蓝色和绿色?我想它会是这样的
scale_fill_manual(values = c(red, rep(c("blue", "green"), recycle.infinite)))
Run Code Online (Sandbox Code Playgroud) 如何暂时禁用 Rprofile。缺少重命名文件?有没有一种优雅的方法可以在 RStudio 启动时打开和关闭我的 Rprofile?
我想绘制多面条形图并从左到右从最大到最小值对它们进行排序。我应该能够使用类似的代码来做到这一点:
library(ggplot2)
ggplot(mpg, aes(reorder(cyl, -hwy), hwy)) +
geom_col() +
facet_wrap(~ manufacturer, scales = "free")
Run Code Online (Sandbox Code Playgroud)
相反,我得到的是按 x 轴排序,恰好是“cyl”,从最小值到最大值。如何按 y 轴降序排列,使其看起来像帕累托图?它也必须是多面的。谢谢你。
我刚刚浏览完 RStudio 的键盘快捷键,但找不到View(). 这是否以任何形式存在?
我想在我的源窗格中突出显示任何数据框,然后点击Ctrl+ X+ Y+Z或其他任何东西以在 RStudio 中查看我的数据框的电子表格视图。我经常创建数据框,我必须跳转到控制台并键入View(data_frame)以理解数据框。因为 tibble 视图太浓缩了。我知道我可以在 source 和Ctrl+ 中突出显示数据框,Enter但这只会产生压缩的 tibble 视图。
我知道这***是Pandoc的水平尺减价。这条水平线在HTML上看起来不错,但是如果我将Markdown编织成pdf,则水平标尺仅运行pdf宽度的一半,并且居中。这种组合只会使水平线看起来很难看。
如何在我的R Markdown中正确放置水平规则以同时正确呈现HTML和pdf?Properly == full length/full width。当我在使用它时,是否可以格式化水平线(颜色,厚度等),而不必使用很多我一无所知的CSS。
下面的代码使用R中的基本绘图函数创建一个帕累托图.如何使用ggplot创建相同的图表?
*我知道有些人会讨厌两个y轴的情节.请不要在这篇文章中包含这个辩论.谢谢
## Creating the d tribble
library(tidyverse)
d <- tribble(
~ category, ~defect,
"price", 80,
"schedule", 27,
"supplier", 66,
"contact", 94,
"item", 33
)
## Creating new columns
d <- arrange(d, desc(defect)) %>%
mutate(
cumsum = cumsum(defect),
freq = round(defect / sum(defect), 3),
cum_freq = cumsum(freq)
)
## Saving Parameters
def_par <- par()
## New margins
par(mar=c(5,5,4,5))
## bar plot, pc will hold x values for bars
pc = barplot(d$defect,
width = 1, space = 0.2, border = …Run Code Online (Sandbox Code Playgroud) library(tidyverse)
mtcars %>%
count(cyl) %>%
mutate(prop = n / sum(n)) %>%
ggplot(aes(x = cyl, y = prop)) +
geom_point() +
scale_y_continuous(labels = scales::percent_format(accuracy = 5L))
Run Code Online (Sandbox Code Playgroud)
如果我使用scales::percent()上面而不是scales::percent_format(accuracy = 5L)在我的百分比标签中得到小数位,这是我不想要的。
问题 - 5L在我上面的例子中做了什么?为什么我需要使用整数 5L 而不是 5?为什么 6L 将最高 y 值从 40% 更改为 42%?这简直太奇怪了。