我可以用ggplot2制作这样的情节吗?
# data
require(vegan)
data(dune)
data(dune.env)
# RDA
RDA <- rda(dune ~ A1, data = dune.env)
# extract species scores
df <- data.frame(spec_scores = scores(RDA)$species[ , 1],
taxa = rownames(scores(RDA)$species))
df <- df[abs(df$spec_scores) > 0.05, ]
# plot
par(mar = c(5,4,4,8))
# boxplot of sites-scores along A1-axis
boxplot(scores(RDA)$sites[ , 1] ~ dune.env$Management)
abline(h = 0, lty = "dotted")
# add species scores to plot
rug(df$spec_scores, side=4)
linestack(df$spec_scores, labels=df$taxa, at = par("usr")[2], add = TRUE, hoff = 1)
Run Code Online (Sandbox Code Playgroud)

基本上我正在寻找一种方法如何在箱线图下方绘制标记的地毯.
任何提示或建议?
我有一个数据帧,其序列号与下面类似:
data <- c(1,1,1,0,0,1,1,2,2,2,0,0,0,2,1,1,0,1,0,2)
Run Code Online (Sandbox Code Playgroud)
我需要的是找到1,2或3次重复0的所有实例,其中前进和后续数字是相同的 - 即1或2都是2(例如1,0,1或2,0,0,2但是不是2,0,1).
然后我需要用周围的值填充零.
我设法找到并计算连续的零
consec <- (!data) * unlist(lapply(rle(data)$lengths, seq_len))
Run Code Online (Sandbox Code Playgroud)
然后我找到了这些连续零开始的行:
consec <- as.matrix(consec)
first_na <- which(consec==1,arr.ind=TRUE)
Run Code Online (Sandbox Code Playgroud)
但我对替换过程感到困惑
我非常感谢你对此的帮助!
卡尔
反正有没有加快R中的以下过程?
theFiles <- list.files(path="./lca_rs75_summary_logs", full.names=TRUE, pattern="*.summarylog")
listOfDataFrames <- NULL
masterDataFrame <- NULL
for (i in 1:length(theFiles)) {
tempDataFrame <- read.csv(theFiles[i], sep="\t", header=TRUE)
#Dropping some unnecessary row
toBeRemoved <- which(tempDataFrame$Name == "")
tempDataFrame <- tempDataFrame[-toBeRemoved,]
#Now stack the data frame on the master data frame
masterDataFrame <- rbind(masterDataFrame, tempDataFrame)
}
Run Code Online (Sandbox Code Playgroud)
基本上,我正在读取目录中的多个csv文件.我想通过堆叠行将所有csv文件合并到一个巨大的数据框中.随着masterDataFrame的大小增加,循环似乎更长.我在linux集群上这样做.
我在论坛中搜索了答案,但它们似乎没有用.
我正在使用knitr包在Rstudio中编写pdf文档.我正在使用Windows 7和Rstudio.
pandoc.exe: Error producing PDF from TeX source
Error: pandoc document conversion failed with error 43
In addition: Warning message:
running command '"C:/Program Files/RStudio/bin/pandoc/pandoc" PA1_template.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output PA1_template.pdf --template "C:\R\Library\rmarkdown\rmd\latex\default.tex" --highlight-style tango --latex-engine pdflatex --variable "geometry:margin=1in"' had status 43
Execution halted
Run Code Online (Sandbox Code Playgroud)
谢谢,
路易斯
我想创建一个像这样的列表/向量:
c(0,0,0,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1)
Run Code Online (Sandbox Code Playgroud)
要么
c(0,0,1,0,1,0,1,1,1,1,0,1,0,1,1,0,1)
Run Code Online (Sandbox Code Playgroud)
该向量的长度是变量"X",0和1的位置是完全随机的.
昨天我从系统中删除了R2.11(Win7,64bit),因为我正在使用R2.13.
从那时起,我收到一条错误消息:
> require(rJava)
Lade nötiges Paket: rJava
Error : .onLoad in loadNamespace() fehlgechlagen, Details:
Aufruf: rJava
Fehler: inDL(x, as.logical(local), as.logical(now), ...)
Run Code Online (Sandbox Code Playgroud)
我尝试指定PATH,因为我在互联网上发现它可能与jvm.dll有关:
c:\Rtools\bin;
c:\Rtools\perl\bin;
c:\Rtools\MinGW\bin;
c:\Rtools\MinGW64\bin;
C:\Windows\system32;
%R_HOME%\bin;
C:\Program Files\R\R-2.13.0\bin;
C:\Program Files\Java\jre6\bin\server
Run Code Online (Sandbox Code Playgroud)
但是我无法解决问题...我也无法从win命令行运行R(只需键入"R"?)
有什么建议?
我想在ggplot2中使用新的指南系统:
require(devtools)
dev_mode()
install_github("ggplot2")
require(ggplot2)
require(reshape2)
p <- ggplot(melt(outer(1:4, 1:4)), aes(x=Var1, y=Var2)) + geom_tile(aes(fill=value))
p + guides(fill = guide_colorbar())
Run Code Online (Sandbox Code Playgroud)
然而,传说看起来有点像什么(不像我见过的所有其他例子):

我究竟做错了什么?
> sessionInfo()
R version 2.14.1 (2011-12-22)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_GB.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=C LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] reshape2_1.2.1 ggplot2_0.9.0 devtools_0.6 gridSVG_0.8-1 proto_0.3-9.2 rgeos_0.2-3 stringr_0.6 gpclib_1.5-1
[9] maptools_0.8-14 lattice_0.20-0 sp_0.9-95 foreign_0.8-49
loaded via a namespace (and …Run Code Online (Sandbox Code Playgroud) 我尝试使用伽玛分布拟合幸存模型.
以下?survreg.distributions我定义了我的自定义分发:
gamma <- list(name = 'gamma',
parms = c(2,2),
init = function(x, weights, ...){
c(median(x), mad(x))
},
density = function(x, parms){
shape <- parms[1]
scale <- parms[2]
cbind(pgamma(x, shape=shape, scale=scale),
1-pgamma(x, shape=shape, scale=scale),
dgamma(x, shape=shape, scale=scale),
(shape-1)/x - 1/scale,
(shape-1)*(shape-2)/x^2 - 2*(shape-1)/(x*scale) + 1/scale^2)
},
quantile = function(p, parms) {
qgamma(p, shape=parms[1], scale=parms[2])
},
deviance = function(...) stop('deviance residuals not defined')
)
Run Code Online (Sandbox Code Playgroud)
但是我无法运行它:
require(survival)
survreg(Surv(log(time), status) ~ ph.ecog + sex, lung, dist=gamma)
#Error in coxph.wtest(t(x) %*% (wt …Run Code Online (Sandbox Code Playgroud) 如何使用#-sign标记文本?
在这里,Sweaving和编译没有问题:
<<echo=false,results=tex>>
cat("This is my text\n")
@
Run Code Online (Sandbox Code Playgroud)
但在这里,编译会出错:
<<echo=false,results=tex>>
cat("This is #my text\n")
@
Run Code Online (Sandbox Code Playgroud)
哈希标志无法在tex中编译.我必须将Sweave选项设置为"tex",因为我想在循环中打印不同的文本文件,每个文本文件应该用新的章节分隔.
<<results=tex>>
for(i in d){
tx <- readLines(i)
cat(tx, sep="\n")
\chapter{i}
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的每一个提示.
TIM
使用较大数据推断出的数据dplyr显示了有关2013年和2014年四个季度总销售额的信息.
Quarter X2013 X2014 Total.Result
1 Qtr 1 77282.13 66421.10 143703.2
2 Qtr 2 69174.64 76480.13 145654.8
3 Qtr 3 65238.47 79081.74 144320.2
4 Qtr 4 65429.73 109738.82 175168.5
Run Code Online (Sandbox Code Playgroud)
我希望ggplot通过比较条形图上的两年和每个季度的条形图组来绘制下面的条形图,如下所示.(来自MS Excel的输出)

我使用的ggplot语句如下所示(我可能错了)
ggplot(qtr2, aes(x=as.factor(Quarter),fill=c(X2013,X2014))) +
geom_bar(position="dodge")
Run Code Online (Sandbox Code Playgroud)
我收到一个错误
Error: Aesthetics must either be length one, or the same length as the dataProblems:as.factor(Quarter)
Run Code Online (Sandbox Code Playgroud)