合并数据集后绘制箱线图

时间:2021-06-19 11:38:54

标签: csv boxplot rbind segment txt

我用下面的代码合并了两个数据集,但是在我使用boxplot()函数后,它仍然存在6个boxplots,我该如何解决?

#cars.csv 
cars_original <- read.csv(file = 'cars.csv')
cars <- na.omit(cars_original)
[enter image description here][1]

#auto-data.txt
auto_original <- read.table("auto-data.txt", header=FALSE, na.strings = "?")
auto_trans <- auto_original[,-9]
names(auto_trans) <- c("mpg", "cylinders", "cubicinches", "hp", "weightlbs", 
                 "time.to.60", "year", "brand")
auto <- na.omit(auto_trans)
auto$brand <- ifelse(auto$brand==1,"US.", 
              ifelse(auto$brand==2, "Europe.", "Japan."))
[enter image description here][1]

#Combine two datasets
total <- rbind(cars, auto)
total_sd <- sapply(split(total$hp, total$brand), sd)
total_length <- sapply(split(total$hp, total$brand), length)
total_se <- total_sd / sqrt(total_length)
total_mean <- sapply(split(total$hp, total$brand), mean)


boxplot(total$hp ~ total$brand,
        xlab = "Brand", ylab = "Horsepower",
        col = "gray", lty ="dashed", lwd=1 )
segments(1:3, total_mean - 1.96 * total_se,
         1:3, total_mean + 1.96 * total_se,
         lwd=6, col="cornflowerblue")
[enter image description here][1]

0 个答案:

没有答案