Reputation: 13
This is a slightly odd request, I don't know if a function is necessarily likely to exist, but I was hoping there might be. Basically, I have a number of variables that I'm looking at to give an outcome and so my typical data would look like below
Sample Daylength Expt Line Protein
1 LD L K 100
2 SD S R 150
3 LD L R 200
4 SD S K 120
5 LD L K 95
6 SD S R 160
7 LD L R 195
8 SD S K 130
So I have 3 dependant variables (daylength, expt, line) and 1 outcome variable of protein. However, what I'd like to be looking at in a graph is comparing the protein levels showing bars for L and S experiments
An example of the code I would use to draw a bar graph with ggplot2 is:
ggplot(data=results, aes(x=daylength, y=protein, fill=line)) + geom_bar(stat="identity", position=position_dodge())
and this would give me a graph showing the results for LD and SD experiments with the lines offering the separate series. However, this does not account for my third factor, the expt
.
Ordinarily in Excel I would be able to manipulate the table to give results for LD/L, LD/S, SD/L and SD/S as being separate headings and create a graph using them as the X factors, however this can be arduous, especially if I'm going to rewrite the table, save it and run it in R each time. I was looking to see if there was a simple enough method to either manipulate the table in R to group everything as below with a few commands or combine the two factors to draw a graph directly each time, especially because in some cases I may be interested in combining different factors.
Sample Daylength/Expt Line Protein
1 LD/L K 100
2 SD/S R 150
3 LD/L R 200
4 SD/S K 120
5 LD/L K 95
6 SD/S R 160
7 LD/L R 195
8 SD/S K 130
Upvotes: 1
Views: 398
Reputation: 2743
You can use the interaction operator :
on factors:
library(ggplot2)
head(CO2) # using builtin data
# Plant Type Treatment conc uptake
#1 Qn1 Quebec nonchilled 95 16.0
#2 Qn1 Quebec nonchilled 175 30.4
#3 Qn1 Quebec nonchilled 250 34.8
#4 Qn1 Quebec nonchilled 350 37.2
#5 Qn1 Quebec nonchilled 500 35.3
#6 Qn1 Quebec nonchilled 675 39.2
qplot(conc, uptake, color=Type:Treatment, data=CO2)
Upvotes: 1
Reputation: 1502
You can create the Daylength/Expt column using paste:
dl_ex <- paste(results$Daylength, results$Expt, sep='/')
(Use as.character
on the columns if R complains about factors or gives you weird numeric results)
and then add it to a dataset
results2 <- results[, -c("Daylength", "Expt")]
results2$DE <- dl_ex
However if you'd like to group your data by some attributes you should look into R's package plyr or sqldf if you're familiar with SQL syntax.
Upvotes: 0