我是一名生物学家,但几年前我不得不自学python和R在不同的地方工作。在我目前的工作中出现了一种情况,R对我来说非常有用,所以我拼凑了一个程序。令人惊讶的是,它做的正是我想要的,除了它生成的图形在开始时有一个额外的条。好了!
我没有输入与第一条相对应的数据:

我希望这是我在设置绘图参数时出现的一些简单错误。会不会是因为我使用的是plot而不是boxplot?它是在绘制标题吗?更令人担忧的是,在读取和合并我的3个数据帧时,我可能会创建某种工件数据,这也会影响统计测试,让我非常沮丧,尽管当我让它将矩阵写入文件时,我没有看到任何类似的东西。我非常感谢大家的帮助!
下面是它的外观,然后是它调用的函数(在另一个脚本中)。(我真的不是程序员,所以如果下面的代码很糟糕,我向您道歉。)我们的目标是将我们的数据(在csv的第10-17列中)与一个大型临床数据表中的所有数据依次进行比较。然后,如果存在显著的相关性(p值小于.05),则将两者相互对比。这给了我一种快速的方法来发现这个大数据集中是否有值得进一步研究的东西。
first <- read.csv(labdata)
second <- read.csv(mrntoimacskey)
third <- read.csv(imacsdata)
firsthalf<-merge(first,second)
mp <-merge(firsthalf, third, by="PATIENTIDNUMBER")
setwd(aplaceforus)
pfile2<- sprintf("%spvalues", todayis)
setwd("fulldataset")
for (m in 10:17) {
n<-m-9
pretty= pretties[n]
for (i in 1:length(colnames(mp))) {
tryCatch(sigsearchA(pfile2,mp, m, i, crayon=pretty), error= function(e)
{cat("ERROR :", conditionMessage(e), "\n")})
tryCatch(sigsearchC(pfile2,mp, m, i, crayon=pretty), error= function(e)
{cat("ERROR :", conditionMessage(e), "\n")})
}
}
sigsearchA<-function(n, mp, y, x, crayon="deepskyblue"){
#anova, plots if significant. takes name of file, name of database,
#and the count of the columns to use for x and y
stat<-oneway.test(mp[[y]]~mp[[x]])
pval<-stat[3]
heads<-colnames(mp)
a<-heads[y]
b<-heads[x]
ps<-c(a, b, pval)
write.table(ps, file=n, append= TRUE, sep =",", col.names=FALSE)
feedback<- paste(c("Added", b, "to", n), collapse=" ")
if (pval <= 0.05 & pval>0) {
#horizontal lables
callit<-paste(c(a,b,".pdf"), collapse="")
val<-sprintf("p=%.5f", pval)
pdf(callit)
plot(mp[[x]], mp[[y]], ylab=a, main=b, col=crayon)
mtext(val, adj=1)
dev.off()
#with vertical lables, in case of many groups
callit<-paste(c(a,b,"V.pdf"), collapse="")
pdf(callit)
plot(mp[[x]], mp[[y]], ylab=a, main=b,las=2,cex.axis=0.7, col=crayon)
mtext(val, adj=1)
dev.off()
}
print(feedback) }
graphics.off()发布于 2016-06-25 05:47:18
如果没有一个可重现的示例,我不能绝对确定,但它看起来像您绘图中的x变量(让我们将其称为x,假设您的数据框称为df)中至少有一行包含一个空字符串("")或一个空格字符(" "),并且x也被编码为一个因子。即使您从数据框中删除所有""值,该值的级别仍将是因子编码的一部分,并将显示在曲线图中。要删除标高,请执行df$x = droplevels(df$x),然后再次运行绘图。
为了说明,这里有一个内置iris数据框的类似示例:
# Shows that Species is coded as a factor
str(iris)
# Species is a factor with three levels
levels(iris$Species)
# There are 50 rows for each level of Species
table(iris$Species)
# Three boxplots, one for each level of Species
boxplot(iris$Sepal.Width ~ iris$Species)
# Now let's remove all the rows with Species = "setosa"
iris = iris[iris$Species != "setosa",]
# The "setosa" rows are gone, but the factor level remains and shows up
# in the table and the boxplot
levels(iris$Species)
table(iris$Species)
boxplot(iris$Sepal.Width ~ iris$Species)
# Remove empty levels
iris$Species = droplevels(iris$Species)
# Now the "setosa" level is gone from all plots and summaries
levels(iris$Species)
table(iris$Species)
boxplot(iris$Sepal.Width ~ iris$Species)https://stackoverflow.com/questions/38017433
复制相似问题