简体   繁体   中英

Boxplot for grouped variable r

I am trying to find if x has an influence on y (number of animals) using a boxplot for each category of z (ie a, b, c, d). x is the area of z (hence continuous). Is it possible to make a boxplot to see if number of animals (y) differed in means by area size of x for each category of z given that the length of z differ. In the end, I want to have a boxplot with y as y-axis, and horizontal x-axis as unique values of z (in this case, a, b, c, d) I tried this below but I get an error that lengths of df$x differ;

z <- c("a", "d", "c", "a", "d", "c", "d", "b", "a", "c", "b", "b", "c", "d", "d", "d", "a", "a", "a", "c", "c")

y <- c(2,  1,  0,  0,  1,  2,  3,  4, 10, 12, 11,  5,  3,  2,  3,  7,  4  ,9  ,2,  2, 17)

x <- c(20, 40, 30, 38, 25, 22, 27, 29, 31, 23, 50, 80, 75 ,22, 26, 30, 31 ,33, 39, 44 ,46)

# dataframe
df <- data.frame(z,y,x)

# boxplot
boxplot(df$y ~ mean(df$x), group = df$z)

I think this is what you're tyring to do?

df$x_mean_by_z = ave(df$x, df$z, FUN = mean)
boxplot(y ~ x_mean_by_z, data = df)

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