Question

I want to draw Boxplots of Measurement-Data grouped by a distance value. My csv-File is like this:

distance;fp1;fp2;fp3;...;fp99
30;-54;-51;-45;...;-56
40;...
...
400;...

Now I want to draw Boxplots for each row with the value of fp1-fp99 and the distance as x-Axis value, so that I have multiple boxplots for the different distance side by side ordered by their distance. Hope somebody can help me.

So here is a sample:

library(ggplot2)
library(reshape)

data <- read.csv(file='file.csv', head=TRUE, sep=';')

pdf(file="output.pdf", onefile=TRUE, pagecentre=TRUE, width=12, height=6)
data$group <- row.names(data)
data.m <- melt(data, id.vars = "group")



print(
    ggplot(data.m, aes(group, value)) + geom_boxplot()  
)

That produces this:

enter image description here

I want to exclude the distance for the box plot and use it as x-coordinate. I hope you guys understand what I mean.

Was it helpful?

Solution

An example filename.csv

distance;fp1;fp2;fp3;fp99
30;-54;-51;-45;-56
40;-30;-40;-40;-50

Here's a solution:

# read data
dat <- read.csv2("filename.csv")

# reshape data
library(reshape2)
dat_m <- melt(dat, id.vars = "distance")

# plot data
boxplot(value ~ distance, dat_m)

enter image description here

If you prefer ggplot2, you can use:

library(ggplot2)
ggplot(dat_m, aes(x = as.factor(distance), y = value)) +
  geom_boxplot()

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top