我有这个数据帧:
set.seed(1)
x <- c(rnorm(50, mean = 1), rnorm(50, mean = 3))
y <- c(rep("site1", 50), rep("site2", 50))
xy <- data.frame(x, y)
Run Code Online (Sandbox Code Playgroud)
我已经制作了这个密度图:
library(ggplot2)
ggplot(xy, aes(x, color = y)) + geom_density()
Run Code Online (Sandbox Code Playgroud)

因为site1我需要遮蔽曲线下面积> 1%的数据.因为site2我需要在曲线下面积<75%的数据.
我期待情节看起来像这样(photoshopped).经历了堆栈溢出后,我意识到其他人已经问过如何在曲线下遮挡部分区域,但是我无法弄清楚如何按组划分曲线下的区域.

如果我跑
knit2pdf("test.Rnw")
Run Code Online (Sandbox Code Playgroud)
... knitr在R中使用.Rnw文件,我收到此错误:
Error in texi2dvi(file = file, pdf = TRUE, clean = clean, quiet = quiet, :
Running 'texi2dvi' on 'test.tex' failed.
Run Code Online (Sandbox Code Playgroud)
谁知道我怎么解决?
我有这个清单:
myList <- list(rnorm(10), rnorm(10), rnorm(10))
names(myList) <- c("one", "two", "three")
$one
[1] -0.07587506 -0.09997924 -0.41846732 1.41542651 -0.58678093 0.56909465 -1.11074541
[8] 1.94663786 0.46381799 -0.11458166
$two
[1] 0.98883679 -0.06305794 -0.78961229 1.21091484 0.19636700 0.27458057 0.12374154
[8] 0.83782946 -0.79627870 0.97675486
$three
[1] 0.67033455 -0.80243815 0.08716750 -2.90455146 -0.02433571 -0.93062428 -0.16886116
[8] -0.60927976 -1.77758270 -1.05033148
Run Code Online (Sandbox Code Playgroud)
我想从列表中删除two并且three我想使用"two"和"three"来引用这些元素.我试过了:
myList <- myList[[-c("two", "three")]]
Run Code Online (Sandbox Code Playgroud)
......这给出了一个错误.
如何从列表中删除two并three使用"两个"和"三个"引用它们?
此代码取自此页面:
library(leaflet)
leaflet(data = quakes[1:20,]) %>% addTiles() %>%
addMarkers(~long, ~lat, popup = ~as.character(mag))
Run Code Online (Sandbox Code Playgroud)
而不是标记,有没有办法mag作为文本标签绘图?
我已经制作了这张地图ggplot2:
library(maptools); library(ggplot2)
data(wrld_simpl)
world <- fortify(wrld_simpl)
worldUk <- subset(world, id=="GBR")
ggplot() +
geom_path(aes(long, lat, group=group), data=worldUk, color="black", fill=NA) +
coord_equal()
Run Code Online (Sandbox Code Playgroud)

使用photoshop,我在地图上添加了比例尺.如何使用添加类似比例尺ggplot2?这篇文章添加了一个比例尺,但该栏并未引用距离:比例尺和map-ggplot2上的北箭头
我正在使用git作为RStudio中的版本控制系统.我有几个不同版本的R脚本保存在git中.可以说,我删除了一段代码,但我现在决定将代码重新插入到当前的R脚本中.我知道代码包含该功能ddply.这是我目前的工作流程:
我在RStudio中打开终端,然后输入:
git grep ddply $(git rev-list --all)
Run Code Online (Sandbox Code Playgroud)
这会带来数百行代码,例如:
2c975e1faee880545546646648f5be2b55f60fc757c29828f1f:reports/:data <- ddply(x, .(as.factor(y), summarise, mean(count))
2c975e1faee880545546646648f5be2b55f60fc757c29828f1f:reports/:data <- ddply(x, .(as.factor(y), summarise, mean(count))
2c975e1faee880545546646648f5be2b55f60fc757c29828f1f:reports/:data <- ddply(x, .(as.factor(y), summarise, mean(count))
2c975e1faee880545546646648f5be2b55f60fc757c29828f1f:reports/:data <- ddply(x, .(as.factor(y), summarise, mean(count))
2c975e1faee880545546646648f5be2b55f60fc757c29828f1f:reports/:data <- ddply(x, .(as.factor(y), summarise, mean(count))
Run Code Online (Sandbox Code Playgroud)
我筛选代码并最终找到我正在寻找的位.我从终端复制相关的代码位并将其粘贴回我的R脚本.在代码可用之前,我需要删除这个位:2c975e1faee880545546646648f5be2b55f60fc757c29828f1f:reports/:
目前,这感觉就像是一种相当缓慢而笨重的方式来重新使用旧的版本控制代码,而且我可能最好使用Rhistory.
是否有更快的方法将版本控制的代码从git中获取并返回到RStudio中的R脚本?
我有一个看起来像这样的函数:
removeRows <- function(dataframe, rows.remove){
dataframe <- dataframe[-rows.remove,]
print(paste("The", paste0(rows.remove, "th"), "row was removed from", "xxxxxxx"))
}
Run Code Online (Sandbox Code Playgroud)
我可以使用这样的函数从数据帧中删除第5行:
removeRows(mtcars, 5)
Run Code Online (Sandbox Code Playgroud)
该函数输出此消息:
"The 5th row was removed from xxxxxxx"
Run Code Online (Sandbox Code Playgroud)
如何将xxxxxxx替换为我使用的数据帧的名称,所以在这种情况下mtcars?
我有两个数据帧.第一个看起来像这样:
value <- seq(1, 100, length.out=20)
df1 <- data.frame(id=as.character(1:20),
value=value,
stringsAsFactors=F)
Run Code Online (Sandbox Code Playgroud)
我有第二个看起来像这样的数据框
df2 <- data.frame(id=as.character(c(1:5, 21:23)),
v2=NA,
stringsAsFactors=F)
Run Code Online (Sandbox Code Playgroud)
我需要将值转移df1到df2,但仅限于哪里df1$id == df2$id.所以我需要的数据框是:
df2Needed <- data.frame(id=as.character(c(1:5, 21:23)),
v2=c(value[1:5], NA, NA, NA),
stringsAsFactors=F)
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?
我在一些.Rnw文件中的文本中包含一些sweave表达式.以下段落包含两个sweave表达式.我可以使用什么正则表达式来查找每个表达式中的R代码.所以正则表达式应该能够找到mean(mtcars$mpg)和/或summary(lm(mpg ~ hp + drat, mtcars))
Lorem ipsum dolor sit amet,consectetur adipisicing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\ {Sexpr平均(mtcars $ MPG)}.Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.Duis aute irure dolor in repreptderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.Excepteur sint occaecat cupidatat\Sexpr {summary(lm(mpg~hp + drat,mtcars))} non non proident,sunt in culpa qui officia deserunt mollit anim id est laborum.