我试图在R中绘制一个boxplot,其中输入文件有多列,每列有不同的行数。在以下链接的帮助下提供帮助:
boxplot of vectors with different length
我在尝试:
x <- read.csv( 'filename.csv', header = T )
plot(
1, 1,
xlim=c(1,ncol(x)), ylim=range(x[-1,], na.rm=TRUE),
xaxt='n', xlab='', ylab=''
)
axis(1, labels=colnames(x), at=1:ncol(x))
for(i in 1:ncol(x)) {
p <- x[,i]
boxplot(p, add=T, at=i)
}
我试图以对数比例绘制值。但是定义log =“y”,我收到以下错误:
xypolygon中的错误(xx,yy,lty =“blank”,col = boxfill [i]): plot.new还没有被调用
以下是我输入的csv数据示例:
A B C D
2345.42 932.19 40.8 26.19
138.48 1074.1 4405.62 4077.16
849.35 0.0 1451.66 1637.39
451.38 146.22 4579.6 5133.14
5749.01 7250.08 12.23 0.09
4125.48 129.46 49.51
440.38 6405.02
答案 0 :(得分:3)
注意我必须删除一个额外的元素
library(data.table)
df <- fread("A,B,C,D
2345.42,932.19,40.8,26.19
138.48,1074.1,4405.62,4077.16
849.35,0.0,1451.66,1637.39
451.38,146.22,4579.6,5133.14
5749.01,7250.08,12.23,0.09
4125.48,129.46,49.51,440.38", sep=",", header=T)
library(dplyr)
library(tidyr)
df1 <- df %>%
replace(.==0,NA) %>% # make 0 into NA
gather(var,values,A:D) %>% # convert from wide (4-col) to long (2-col) format
mutate(values = log10(values)) # log10 transform
如果您需要log2
,只需将log10
替换为log2
boxplot(values ~ var, df1)
对于log10比例,我希望将1
添加到我的值中以消除自log10(0 < x < 1) = -value
以来的负值。由于0
和0 + 1 = 1
log10(1) = 0