考虑这个例子:
library(ggplot2)
library(lubridate)
set.seed(4)
date <- seq(from = as.POSIXct("2012-01-01"), to = as.POSIXct("2014-12-31"), by = "days")
value <- c(rnorm(274, 50, 1), rnorm(274, 55, 1), rnorm(274, 55, 2), rnorm(274, 60, 2))
df <- data.frame(date, value)
head(df)
# date value
# 1 2012-01-01 50.21675
# 2 2012-01-02 49.45751
# 3 2012-01-03 50.89114
# 4 2012-01-04 50.59598
# 5 2012-01-05 51.63562
# 6 2012-01-06 50.68928
ggplot(df, aes(x=yday(date), y=value, color=factor(year(date)))) +
geom_line()
Run Code Online (Sandbox Code Playgroud)
这产生了这个情节:

有哪些方法可以使轴按月格式化为日期?我试图确定一种干净的方式来利用两者lubridate,scale_x_date如果可能的话?
也许有更好的方法来创建这种类型的图形?也就是说,按年创造因素并将它们叠加在一起?(注意:我不想使用facet_wrap或facet_grid用于此示例).
考虑以下rmarkdown html_notebook示例:
---
output: html_notebook
runtime: shiny
---
```{r}
library(ggplot2)
library(shiny)
blank1 <- renderPlot({ ggplot() + labs(title = "Plot 1") })
blank2 <- renderPlot({ ggplot() + labs(title = "Plot 2") })
blank3 <- renderPlot({ ggplot() + labs(title = "Plot 3") })
column(6, blank1, blank2)
column(6, blank3)
```
Run Code Online (Sandbox Code Playgroud)
我想将地块显示为:
我尝试了一些事情,包括:
fluidRow(
column(6, blank1, blank2),
column(6, blank3)
)
Run Code Online (Sandbox Code Playgroud)
但是我无法Plot 3跨越多行。
附加说明(每个注释):
cowplot或patchwork解决方案,但我需要从shiny(例如ggplot(aes(x = input$var_select)) + ...。column()和/或fluidRow()保留响应式设计方面。我有以下功能,但我觉得有一个更快(矢量化或可能是一个包或内置?)的方式来写这个?
create_seq <- function(n, len) {
mat <- matrix(nrow = length(0:(n-len)), ncol = n)
for(i in 0:(n-len)) {
mat[i + 1, ] <- c(rep(0L, i), rep(1L, len), rep(0L, n - (len + i)))
}
return(mat)
}
create_seq(10, 3)
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#> [1,] 1 1 1 0 0 0 0 0 0 0
#> [2,] 0 1 1 1 0 0 0 0 0 0
#> [3,] 0 0 1 1 1 …Run Code Online (Sandbox Code Playgroud) 问题: 要点是,任何人都可以使用dc.js +谷歌地图提供一个玩具示例,当我在dc.js图表上进行笔刷时,地图的标记会根据图表中选择/绘制的内容进行更新?
到目前为止,我所拥有的:pages.github。完整的仓库在这里。我还找到了这个很酷的小吃仪表板示例,但这使用了传单。如果可能的话,我试图避免传单。
我正在尝试将dc.js(交叉过滤器)绑定到Google Maps。我看过这个视频,我能够适应这个例子。
但是,当我尝试使其适应使用dc.js时,无法将交叉滤镜绑定回Google Maps。(我仍然可以将地图绑定到crossfilter / dc.js,只是不能反过来)。也就是说,在地图上滚动时,图表会调整,但是当我刷图表时,似乎无法启动我的updateMarkers()功能。
function init() {
initMap();
initCrossFilter();
// bind map bounds to lat/lng ndx dimensions
latDim = ndx.dimension(function(p) { return p.lat; });
lngDim = ndx.dimension(function(p) { return p.lng; });
google.maps.event.addListener(map, 'bounds_changed', function() {
var bounds = this.getBounds();
var northEast = bounds.getNorthEast();
var southWest = bounds.getSouthWest();
// NOTE: need to be careful with the dateline here
lngDim.filterRange([southWest.lng(), northEast.lng()]);
latDim.filterRange([southWest.lat(), northEast.lat()]);
// …Run Code Online (Sandbox Code Playgroud) library(ggplot2)
library(dplyr)
library(scales)
data <- data.frame(THEME_NAME = c(rep("A", 10), rep("B", 20), rep("C", 15)))
data %>%
group_by(THEME_NAME) %>%
summarise(n = n()) %>%
mutate(freq = n / sum(n)) %>%
# THE NEXT LINE !!! #
ggplot(., aes(x = reorder(THEME_NAME, desc(freq)), y = freq)) +
geom_bar(stat="identity") +
scale_y_continuous(labels=percent)
Run Code Online (Sandbox Code Playgroud)
我怎么能以THEME_NAME编程方式参考?我可以做的.$THEME_NAME,但我想称之为.[1]或select(., 1)或东西的性质是什么?
这样做的原因是我想在更大的上下文中使用这个管道 - 比如通过这个管道传递一堆因子变量.类似于:vars.to.plot <- sapply(data, is.factor)然后运行vars.to.plot通过此管道的每个元素.
有没有办法让ggplot2的geom_density()函数模仿ggvis的行为layer_densities()?也就是说,在没有调用的情况下使它p1看起来如此p3(见下文)xlim()?具体来说,我更喜欢使密度曲线的尾部平滑的视图.
library(ggvis)
library(ggplot2)
faithful %>%
ggvis(~waiting) %>%
layer_densities(fill := "green") -> p1
ggplot(faithful, aes(x = waiting)) +
geom_density(fill = "green", alpha = 0.2) -> p2
ggplot(faithful, aes(x = waiting)) +
geom_density(fill = "green", alpha = 0.2) +
xlim(c(30, 110)) -> p3
p1
p2
p3
Run Code Online (Sandbox Code Playgroud)
ggvis输出:

ggplot2"默认":

ggplot2"需要":

注意:可以通过以下(使用trim=TRUE)制作ggvis mimic ggplot2 ,但我想转向另一个方向......
faithful %>%
compute_density(~waiting, trim=TRUE) %>%
ggvis(~pred_, ~resp_) %>%
layer_lines()
Run Code Online (Sandbox Code Playgroud) 以下面的一结、一级样条为例:
library(splines)
library(ISLR)
age.grid = seq(range(Wage$age)[1], range(Wage$age)[2])
fit.spline = lm(wage~bs(age, knots=c(30), degree=1), data=Wage)
pred.spline = predict(fit.spline, newdata=list(age=age.grid), se=T)
plot(Wage$age, Wage$wage, col="gray")
lines(age.grid, pred.spline$fit, col="red")
# NOTE: This is **NOT** the same as fitting two piece-wise linear models becase
# the spline will add the contraint that the function is continuous at age=30
# fit.1 = lm(wage~age, data=subset(Wage,age<30))
# fit.2 = lm(wage~age, data=subset(Wage,age>=30))
Run Code Online (Sandbox Code Playgroud)

有没有办法提取结之前和之后的线性模型(及其系数)?即如何提取 的切点前后的两个线性模型age=30?
使用summary(fit.spline)产生系数,但(据我理解)它们对于解释没有意义。
在我的server.R文件中,我有一个被叫的myNet生成一个visNetwork.在我的ui.R,我有多个选项卡面板,理想情况下将有不同的输入小部件,影响visNetwork.
是否可以重复使用相同的绑定?
目前,当我尝试运行类似于下面的代码时,我收到一个错误:Uncaught Duplicate binding for ID vis.
server.R myNet <- reactive({
nodes <- df_nodes
edges <- df_edges
visNetwork(nodes, edges, height = '800px')
})
output$vis <- renderVisNetwork(
myNet()
)
Run Code Online (Sandbox Code Playgroud)
ui.R ...
tabPanel("First Panel",
sidebarLayout(
sidebarPanel(
sliderInput("input1", "Title 1",
min=1, max=10, value=1),
sliderInput("input2", "Title 2",
min=1, max=10, value=1),
sliderInput("input3", "Title 3",
min=1, max=10, value=1)
),
mainPanel(
visNetworkOutput("vis", height = '800px') # *** ISSUE HERE***
) …Run Code Online (Sandbox Code Playgroud) 我想确定序列是否包含任何间隙或不规则的步骤?不知道这是否是正确的表达方式,而且很有可能重复出现(但是我找不到很好的问题)。
下面的has_gap函数给我正确的结果,但是似乎有点笨拙?也许我没有发现内置的东西?
x1 <- c(1:5, 7:10)
x2 <- 1:10
x3 <- seq(1, 10, by = 2)
x4 <- c(seq(1, 6, by = 2), 6, seq(7, 10, by = 2))
has_gap <- function(vec) length(unique(diff(vec))) != 1
vecs <- list(x1, x2, x3, x4)
sapply(vecs, has_gap)
# [1] TRUE FALSE FALSE TRUE
Run Code Online (Sandbox Code Playgroud) 当我启动R运行一个脚本shiny从应用程序cmd行,它似乎推出2个的实例Rscript.exe?我总是可以杀死两者中较小的一个并且应用程序继续运行?有人可以详细说明幕后实际发生的事情,或者告诉我我做错了什么导致双重流程?
require(shiny)
app <- shinyApp(
ui = bootstrapPage(
numericInput('n', 'Number of obs', 100),
plotOutput('plot')
),
server = function(input, output) {
output$plot <- renderPlot({ hist(runif(input$n)) })
}
)
runApp(app, launch.browser = FALSE, port = 1234, host = "10.123.4.56")
Run Code Online (Sandbox Code Playgroud)
现在,如果我通过以下cmd线路启动它:
START Rscript --vanilla C:\Users\Jason\Projects\super_simple.R
Run Code Online (Sandbox Code Playgroud)
此时,我可以将浏览器指向http://10.123.4.56:1234并查看该应用程序。但是,如果我通过以下方式tasklist /FI "imagename eq rscript*"查看正在运行的进程:我看到两个 Rscript.exe进程:
然而,我确定的是,我总是可以杀死两者中较小的一个(例如taskkill /pid 9360 /f),而该应用程序仍然可以完整运行?注意:我尝试通过在后台启动命令,START \b ...但结果是一样的。
r ×9
ggplot2 ×3
shiny ×3
bspline ×1
cmd ×1
crossfilter ×1
d3.js ×1
dc.js ×1
dplyr ×1
for-loop ×1
ggvis ×1
google-maps ×1
javascript ×1
lubridate ×1
r-markdown ×1
regression ×1
rnotebook ×1
sequence ×1
shiny-server ×1