简体   繁体   中英

R- making a figure from multiple vectors of different lengths

I have 8 vectors all with different lengths (ranging from 5 to 110) I would like to display box plots for each vector on the same figure but am running into all sorts of issues- are there any packages that make this easier? If anyone can lead me in the right direction that would be greatly appreciated! Bonus points if there's an answer using ggplot2!

Sample data:

set.seed(42)
### these are your data vectors
vec1 <- rnorm(20) ; vec2 <- rexp(30, 3) ; vec3 <- rnorm(40, 3, 5)

Combine them into one "long" frame:

### create a "long" frame
dat <- dplyr::bind_rows(A = data.frame(val = vec1), B = data.frame(val = vec2), C = data.frame(val = vec3), .id = "id")
head(dat)
#   id        val
# 1  A  1.3709584
# 2  A -0.5646982
# 3  A  0.3631284
# 4  A  0.6328626
# 5  A  0.4042683
# 6  A -0.1061245

The plot:

library(ggplot2)
ggplot(dat, aes(id, val)) + geom_boxplot()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM