这是我的输入数据集:
> names(breakingbad.episodes)
[1] "season" "episode" "epnum" "epid" "title"
[6] "url.trakt" "firstaired.utc" "id.tvdb" "rating" "votes"
[11] "loved" "hated" "overview" "firstaired.posix" "year"
[16] "zrating.season" "src"
Run Code Online (Sandbox Code Playgroud)
对于我ggvis,我用下面的变量firstaired.posix和rating:
> str(breakingbad.episodes[c("firstaired.posix", "rating")])
'data.frame': 62 obs. of 2 variables:
$ firstaired.posix: POSIXct, format: "2008-01-21 02:00:00" "2008-01-28 02:00:00" "2008-02- 11 02:00:00" ...
$ rating : num 87 85 84 84 83 90 87 85 88 83 ...
Run Code Online (Sandbox Code Playgroud)
我ggvis使用包含以下rating信息的工具提示成功创建了我:
> breakingbad.episodes %>%
ggvis(x = ~firstaired.posix,
y = …Run Code Online (Sandbox Code Playgroud) 示例代码:
rsq <- round(cor(mtcars$disp, mtcars$mpg)^2, 2) # rsq = 0.72
ggplot(mtcars, aes(x = disp, y = mpg)) +
geom_point() +
geom_smooth(method = lm, aes(color = "Linear")) +
scale_color_discrete(labels = expression(paste("R"^2, " = ", rsq)))
Run Code Online (Sandbox Code Playgroud)
我希望将图例显示为R² = 0.72.
我知道我可以使用unicode符号来获取上标,但总的来说我认为必须有一种方法来组合数学表达式和存储在对象中的计算值.
我试图玩eval和各种组合paste,但似乎我一直在遇到同样的问题.
编辑#1:
我试着使用bquote按照这个答案是这样的:
scale_color_discrete(labels = bquote(R^2 == .(rsq)))
原来只会将图例渲染为==.
编辑#2:
即使下面的答案有效,似乎......对于更复杂的表达式来说非常不方便,比如:

我仍然希望有一个更简单的解决方案.
假设您得到了一个如下所示的汇总交叉表:
kdat <- data.frame(positive = c(8, 4), negative = c(3, 6),
row.names = c("positive", "negative"))
kdat
#> positive negative
#> positive 8 3
#> negative 4 6
Run Code Online (Sandbox Code Playgroud)
现在您想要计算 Cohen 的 Kappa,这是一个用于确定两个评估者之间一致性的统计数据。给定这种格式的数据,您可以使用psych::cohen.kappa:
psych::cohen.kappa(kdat)$kappa
#> Warning in any(abs(bounds)): coercing argument of type 'double' to logical
#> [1] 0.3287671
Run Code Online (Sandbox Code Playgroud)
这让我很恼火,因为我更喜欢我的数据又长又薄,这样我就可以使用irr::kappa2. 出于某种原因我更喜欢类似的功能。所以我组装了这个函数来重新格式化我的数据:
longify_xtab <- function(x) {
nm <- names(x)
# Convert to table
x_tab <- as.table(as.matrix(x))
# Just in case there are now rownames, required for conversion
rownames(x_tab) <- nm …Run Code Online (Sandbox Code Playgroud) 我试图从openpaths.cc中提取我的位置数据以将其与R一起使用.API使用OAuth并在此处进行了说明,但是,它仅提供了Python中的示例.
在查看了如何处理R中的OAuth(我几乎不熟悉)之后,我找到了ROAuth,因此我使用了提供的用法示例作为基础.
根据API的文档,所有请求的端点是https://openpaths.cc/api/1的,我有我的访问密钥和访问秘密,所以我天真地插入他们在cKey,cSecret,reqURL,accessURL,authURL,和testURL,但只得到了"错误的请求"从结果credentials$handshake()线.
reqURL <- "https://openpaths.cc/api/1"
accessURL <- "https://openpaths.cc/api/1"
authURL <- "https://openpaths.cc/api/1"
cKey <- "key"
cSecret <- "secret"
testURL <- "https://openpaths.cc/api/1"
credentials <- OAuthFactory$new(consumerKey=cKey,
consumerSecret=cSecret,
requestURL=reqURL,
accessURL=accessURL,
authURL=authURL,
needsVerifier=TRUE)
credentials$handshake()
## the GET isn’t strictly necessary as that’s the default
credentials$OAuthRequest(testURL, "GET")
Run Code Online (Sandbox Code Playgroud)
虽然我觉得我不知道自己在做什么,但我至少证实了ROAuth能够使用HMAC-SHA1方法,这是openpath所要求的.
编辑:我安装了ROAuth版本0.9.3
EDIT2:在了解了httr后,我认为这可能是适合该任务的库,但是我仍然无法产生任何可用的结果,因为令牌创建oauth1.0_token只会导致Bad request再次出现.我认为我的主要问题是缺少openpaths.cc的API文档.使用所有这些工具,我仍然不知道如何正确使用它们.