随着grid.arrange我可以安排多个ggplot在网格中人物使用类似,实现了多屏图:
library(ggplot2)
library(grid)
library(gridExtra)
Run Code Online (Sandbox Code Playgroud)
然后生成一些ggplot2图
plot5 <- grid.arrange(plot4, plot1, heights=c(3/4, 1/4), ncol=1, nrow=2)
Run Code Online (Sandbox Code Playgroud)
如何获得"不平衡"2 col布局,其中第一个col中有一个绘图,第二个col中有三个绘图?我玩弄了一个'网格网格'方法,试图用grid.arrange一个网格(例如plot5,上面)绘制另一个图,但得到了:
在ArrangeGrob中出错(...,as.table = as.table,clip = clip,main = main,:输入必须是grobs!
更新:
感谢您的建议.我会考虑viewports和grid.与此同时,感谢@DWin,layOut'wq'包中的函数非常适合我Sweave文档中的编译图:

更新2:
该arrangeGrob命令(由@baptiste建议)也运行良好,看起来非常直观 - 至少很容易改变两列的宽度.它还具有不需要`wq'包的好处.
这是我的Sweave文件中的代码:
<<label=fig5plot, echo=F, results=hide>>=
plot5<-grid.arrange(plot4, arrangeGrob(plot1, plot2, plot3, ncol=1),
ncol=2, widths=c(1,1.2))
@
\begin{figure}[]
\begin{center}
<<label=fig5,fig=TRUE,echo=T, width=10,height=12>>=
<<fig5plot>>
@
\end{center}
\caption{Combined plots using the `arrangeGrob' command.}
\label{fig:five}
\end{figure}
Run Code Online (Sandbox Code Playgroud)
产生以下输出:

顺便说一下,有人告诉我为什么出现'> NA'?
我在下面有一个简单的应用程序,它显示了一个ggplot.ggplot在控制台中生成警告(参见底部图片).我想捕获警告,并在应用程序中,在情节下显示它.
这是我的代码:
library(shiny)
library(ggplot2)
ui <- fluidPage(
titlePanel("How do i output ggplot warnings? :("),
mainPanel(
plotOutput("my_plot_that_generates_warnings"),
tags$br(),
verbatimTextOutput(outputId='ggplot_warnings')
)
)
server <- function(input, output) {
messages <- reactiveValues(ggplot_warning = 'How to capture warning and display it?')
output$my_plot_that_generates_warnings <- renderPlot({
tryCatch({
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) +
geom_point() +
geom_smooth()
}, message = function(e) {
messages$ggplot_warning <- e$message
}, warning = function(e) {
messages$ggplot_warning <- e$message
}, error = function(e) {
messages$ggplot_warning <- e$message
})
})
output$ggplot_warnings <- renderPrint({
cat(messages$ggplot_warning)
})
}
shinyApp(ui …Run Code Online (Sandbox Code Playgroud) 我想找到下面第x3列最接近的值.
data=data.frame(x1=c(24,12,76),x2=c(15,30,20),x3=c(45,27,15))
data
x1 x2 x3
1 24 15 45
2 12 30 27
3 76 20 15
Run Code Online (Sandbox Code Playgroud)
所以期望的输出将是
Closest_Value_to_x3
24
30
20
Run Code Online (Sandbox Code Playgroud)
请帮忙.谢谢
我有多个带有NA的向量,并且打算填充NA,它们距有效数据点0大于2个间隔。例如:
x <- c(3, 4, NA, NA, NA, 3, 3)
Run Code Online (Sandbox Code Playgroud)
预期的输出是
3, 4, NA, 0, NA, 3, 3
Run Code Online (Sandbox Code Playgroud) 我想找到嵌套列表和目标值(11)匹配项的索引。
数据:
f <- list(
list(1, 2, 3),
list(4, 5, list(8, list(11, 12)))
)
Run Code Online (Sandbox Code Playgroud)
目标值= 11的所需输出:
c(2, 3, 2, 1)
Run Code Online (Sandbox Code Playgroud)
打印:
f
[[1]]
[[1]][[1]]
[1] 1
[[1]][[2]]
[1] 2
[[1]][[3]]
[1] 3
[[2]]
[[2]][[1]]
[1] 4
[[2]][[2]]
[1] 5
[[2]][[3]]
[[2]][[3]][[1]]
[1] 8
[[2]][[3]][[2]]
--->>> [[2]][[3]][[2]][[1]] <<<----
-->>>> [1] 11 <<<----
[[2]][[3]][[2]][[2]]
[1] 12
Run Code Online (Sandbox Code Playgroud)
我试过的
匹配unlist(data),寻找参数which()并寻找arrInd()。
我会避免在R中运行多个for循环。最好在C,C ++中执行所有循环。...在自己实现之前,我想检查一下我是否缺少一个函数。
我有一堆colnames
L_1_3
L_2_23
L_3_91
L_3_16
Run Code Online (Sandbox Code Playgroud)
我想使用 _ 后面的最后一位数字用新名称替换这些列名,如下所示:
3
23
91
16
Run Code Online (Sandbox Code Playgroud)
我试过colnames(X) <- gsub("L_\\d\\d_", "", colnames(X))这适用于末尾有两位数的字符串。我想要一个既适用于一位数又适用于两位数的人。
谢谢!
我想用"行距离"填充缺失值到最近的非NA值.换句话说,如何将此示例数据框中的列x转换为y列?
# x y
#1 0 0
#2 NA 1
#3 0 0
#4 NA 1
#5 NA 2
#6 NA 1
#7 0 0
#8 NA 1
#9 NA 2
#10 NA 3
#11 NA 2
#12 NA 1
#13 0 0
Run Code Online (Sandbox Code Playgroud)
我似乎无法找到dplyr group_by和mutate row_number()语句的正确组合来完成这个技巧.我调查过的各种插补包都是针对更复杂的场景而设计的,其中使用统计和其他变量进行插补.
d<-data.frame(x=c(0,NA,0,rep(NA,3),0,rep(NA,5),0),y=c(0,1,0,1,2,1,0,1,2,3,2,1,0))
Run Code Online (Sandbox Code Playgroud) 如何检查向量是否具有所有相同的元素?
例如,假设我有:
vec1 = rep(10,20)
vec2 = seq(1:20)
Run Code Online (Sandbox Code Playgroud)
我怎样才能证明它vec1具有所有相同的元素?
假设有以下数据表:
DT <- data.table(a=c(1,2,3,4,5,6),b=c(NaN,NaN,NaN,4,5,6),c=c(NaN,3,3,3,NaN,NaN))
Run Code Online (Sandbox Code Playgroud)
如何将每列中的所有NaN值替换为, 即?NA寻找一种优雅(简短)的方法来做到这一点,但我也想知道是否可以使用lapply,就像我在下面尝试的那样。
到目前为止我的方法:
DT[,lapply(SD,function(x){x[is.nan(x)] := NA}),.SDcols=c("a","b","c")]
Run Code Online (Sandbox Code Playgroud)
我的代码的实际结果是:
(x[is.nan(x)], NA)中的错误
:=:检查 is.data.table(DT) == TRUE。否则,:= 和:=(...) 被定义为在 j 中使用,仅一次且以特定方式使用。请参阅帮助(“:=”)。
当我设置函数时geom_smooth(..., se = TURE),置信区间为灰色,但我想设置一些其他颜色。
貌似是geom_smooth里面不能设置颜色的参数。我应该创建一个新的geom吗?或者是否可以使用一些 ggplot2 扩展?谢谢