我试图在x轴上制作一个由两个离散变量排序的刻面ggplot.问题是我想让垂直相邻的条目全部接触.目前,行之间存在空间,基于该因子在顶部图中与底部的哪个级别.抱歉,这个可重复的例子有点冗长.
npats=20
simsympt=c(id=1,date=1,tx="control",sympt=0)
for(i in 1:npats)
{ days=abs(round(rnorm(1,100,40),0))
id=rep(as.character(i),days)
date=1:days
tx=rep(sample(c("control","treatment"),1),days)
sympt= sample(0:10, days,p=c(12,3,3,2,1,1,1,1,1,1,1),rep=T)
simsympt= rbind(simsympt, cbind(id,date,tx,sympt) )
}
####tidy things up a bit
simsympt=data.frame(simsympt)[-1,]
colnames(simsympt)=c('id','date','tx','level')
simsympt$date=as.numeric(as.character(simsympt$date))
simsympt$level=as.numeric(as.character(simsympt$level))
#simsympt$id=as.numeric(as.character(simsympt$id))
head(simsympt)
##now the important stuff
p <- ggplot(simsympt, aes(x=date,y=id))
p= p + geom_tile(aes(fill=level)) +
facet_grid(tx~.,drop=T,space="free")+
scale_y_discrete(expand=c(0,0),drop=T)
p
Run Code Online (Sandbox Code Playgroud)

我只需要删除顶部和底部图形(facet)中行之间的所有垂直空间.例如,由于id号15在对照组中,因此在治疗组中不应该有她的行.谢谢,赛斯
我有一个 pdf 文件路径的 R 向量,我想放入我的 knitr 文档并编织到 html。我看到我可以得到一个单独的 pdf 文件
knitr::include_graphics(filepaths[1])
Run Code Online (Sandbox Code Playgroud)
我的文件路径向量很长并且在文档编辑之间改变大小。有没有一种方法可以一次性将它们全部包含在内。我原以为这会奏效。
for(i in filepaths){knitr::include_graphics(i)}
Run Code Online (Sandbox Code Playgroud)
也试过:
for(i in filepaths){ print("" ) }
Run Code Online (Sandbox Code Playgroud) 我有一个州的每个县的每周数据.我想创建一个循环遍历每周的动画,显示绘制到地图上的数据,颜色表示强度和/或上周的变化.
library(ggplot2); library(animation); library(maps); library(plyr)
county <- map_data("county")
wy <- county[county$region =="wyoming",]
l = length((wy$subregion))
#Add random variales
wy <- mutate(wy, a = runif(length(region)),
b = runif(length(region)),
c= runif(length(region)))
test <- function(j){
ggplot(wy, aes(long, lat, group = group))+
geom_path() +
geom_polygon(aes_string(fill=j))
}
test("c")
test("b")
v = c("a","b","c"))
oopt <- animation::ani.options(interval = 0.1)
FUN2 <- function() {
lapply(v, function(i) {
test(i)
animation::ani.pause()
})
}
FUN2()
saveHTML(FUN2(), autoplay = FALSE, loop = FALSE, verbose = FALSE, outdir = "images/animate/new",
single.opts = "'controls': …Run Code Online (Sandbox Code Playgroud)