有没有快速的方法将表添加到我的ggplot2图表?我希望这个表的每个行的值在指定的相同断点处scale_x_continuous(),但旁边有百分比(%)符号.我的最终目标是创建如下图所示的内容.但是,我不知道如何添加表格.
下面的代码块只在ggplot2中生成两行,应该足以为我提供一个示例:
require(ggplot2)
df <- data.frame(a = seq(0, 90, 10), b = seq(10, 100, 10))
df.plot <- ggplot(data = df, aes(x = seq(1, 100, 10))) + geom_line(aes(y = a), colour = 'red') +
geom_line(aes(y = b), colour = 'blue') + scale_x_continuous(breaks = seq(0,100,10))
df.plot
Run Code Online (Sandbox Code Playgroud)
这里提出了一个类似的问题,但给出的答案更多的是一种解决方法,对于一个有2行的表来说看起来不太好.我将要弄乱Brian Diggs提供的线索,但我想我会发布这个以防任何人已经做过这样的事情.任何帮助将不胜感激!
编辑:感谢@baptiste帮助我解决这个问题.我在下面发布了自己的回复,完成了他的开始.
5天仍然没有答案
我一直在努力解决这个问题,任何帮助都会非常感激.我正在尝试编写一个运行几个逐步回归的函数,并将所有这些函数输出到列表中.但是,R在读取我在函数参数中指定的数据集时遇到问题.我在各种电路板上发现了几个类似的错误(这里,这里和这里),但是它们似乎都没有得到解决.这一切都归结为在用户定义的函数中调用step()的一些奇怪问题.我使用以下脚本来测试我的代码.多次运行整个过程,直到出现错误(相信我,它会):
test.df <- data.frame(a = sample(0:1, 100, rep = T),
b = as.factor(sample(0:5, 100, rep = T)),
c = runif(100, 0, 100),
d = rnorm(100, 50, 50))
test.df$b[10:100] <- test.df$a[10:100] #making sure that at least one of the variables has some predictive power
stepModel <- function(modeling.formula, dataset, outfile = NULL) {
if (is.null(outfile) == FALSE){
sink(file = outfile,
append = TRUE, type = "output")
print("")
print("Models run at:")
print(Sys.time())
}
model.initial <- …Run Code Online (Sandbox Code Playgroud) 我写了一个小函数来将我的数据集划分为训练和测试集.但是,在处理因子变量时遇到了麻烦.在我的代码的模型验证阶段,如果模型是在没有来自每个级别的因子的表示的数据集上构建的,那么我会收到错误.如何修复此partition()函数以包含来自因子变量的每个级别的至少一个观察?
test.df <- data.frame(a = sample(c(0,1),100, rep = T),
b = factor(sample(letters, 100, rep = T)),
c = factor(sample(c("apple", "orange"), 100, rep = T)))
set.seed(123)
partition <- function(data, train.size = .7){
train <- data[sample(1:nrow(data), round(train.size*nrow(data)), rep= FALSE), ]
test <- data[-as.numeric(row.names(train)), ]
partitioned.data <- list(train = train, test = test)
return(partitioned.data)
}
part.data <- partition(test.df)
table(part.data$train[,'b'])
table(part.data$test[,'b'])
Run Code Online (Sandbox Code Playgroud)
编辑 - 使用'caret'包和createDataPartition()的新函数:
partition <- function(data, factor=NULL, train.size = .7){
if (("package:caret" %in% search()) == FALSE){
stop("Install and Load 'caret' package")
}
if …Run Code Online (Sandbox Code Playgroud) 我试图采用格式不佳的美元值的字符向量并将其转换为数字.值的格式如下所示,带有前导和尾随空格,逗号和美元符号:
x <- c(" 18,000.50 ", " $1,240.30 ", " $125.00 ")
Run Code Online (Sandbox Code Playgroud)
我试图使用以下函数来摆脱除数字和点之外的所有字符,但它不起作用:
trim_currency <- function(x) grep("\$([0-9.]*)\,([0-9.]*)", x, values=TRUE)
Run Code Online (Sandbox Code Playgroud)
我得到了正则表达式代码
\$([0-9.]*)\,([0-9.]*)
Run Code Online (Sandbox Code Playgroud)
使用此正则表达式测试程序http://regex101.com/r/qM2uG0成功运行
当我在R中运行它时,我收到以下错误:
Error: '\$' is an unrecognized escape in character string starting "\$"
Run Code Online (Sandbox Code Playgroud)
关于如何在R中做到这一点的任何想法?
感谢ndoogan的回应.这解决了这个特殊问题.但是,如果我想让它更通用,我会问:
我如何使用R/regex通过过滤器运行向量,只允许数字和句点通过?
我写了一个函数来计算向量中每行的十分位数.我这样做的目的是创建图形来评估预测模型的功效.必须有一种更简单的方法来做到这一点,但我暂时还没弄清楚.有没有人知道如何在没有这么多嵌套的ifelse()语句的情况下以这种方式对矢量进行评分?我包含了该函数以及一些代码来复制我的结果.
# function
decile <- function(x){
deciles <- vector(length=10)
for (i in seq(0.1,1,.1)){
deciles[i*10] <- quantile(x, i)
}
return (ifelse(x<deciles[1], 1,
ifelse(x<deciles[2], 2,
ifelse(x<deciles[3], 3,
ifelse(x<deciles[4], 4,
ifelse(x<deciles[5], 5,
ifelse(x<deciles[6], 6,
ifelse(x<deciles[7], 7,
ifelse(x<deciles[8], 8,
ifelse(x<deciles[9], 9, 10))))))))))
}
# check functionality
test.df <- data.frame(a = 1:10, b = rnorm(10, 0, 1))
test.df$deciles <- decile(test.df$b)
test.df
# order data frame
test.df[with(test.df, order(b)),]
Run Code Online (Sandbox Code Playgroud) r ×5
statistics ×2
factors ×1
ggplot2 ×1
glm ×1
gsub ×1
partitioning ×1
quantile ×1
regex ×1
regression ×1