I have data that I need to graph using R. Unfortunately, the examples I have found in books and on the web are too simplistic and do not give me much insight into the process. I have never used R before, and have been trying for hours and hours to figure out how to do what I need to do. I also already read the previous posts about R here on Ask MeFi, but I am still stumped.
I've made a fake set of data that represents the structure of my real data (which you don't want to see, it's too confusing).
Here is my sample data.
Take a quick look at that.
There is a main factor/category, Factor1 with values of "BOB" and "JILL", and a grouping within that called SubFactor with values "Q" and "P". I also have blocked reps of everything, labeled A, B, C, D. My measurements are Measurement1, Measurement2, Measurement3, etc which I will abbreviate as M1, M2, M3, etc.
To cut to the chase, I made a few hand drawings of the graphs I need. Please note that the graphs are illustrative only, and do not correspond to the data above.
Please walk me through this step-by-step and assume I know absolutely nothing about how to get my data into R, how to manipulate it as needed, and finally, how to produce the following graphs.
ALSO if any of you R graphing experts want to take me under your wing and help me learn more than I have outlined here (surely I will have future questions), please send me a MeMail.
----------
GRAPH 1...
In the first, I want to make a bar graph of just
the average of the M1 measurements for each treatment, split into categories "BOB" and "JILL" with the grouped "P" and "Q" subfactors.
So that we're on the same page here, the mean of BOB(Q) for M1 = (100+500+200+300)/4 = 275, for example.
Here is my drawing for this:
Bar Graph 1
----------
GRAPH 2...
In the second, I want a plot of the measurements for "BOB", with M1, M2, M3, M4, and M5 on the X-axis. The lines will represent the "P" and "Q" subfactors.
Here is my drawing for this:
Plot 1
----------
GRAPH 3...
In another bar graph, I want to have M1, M2, M3, M4, M5 be the bars, and have them grouped by BOB (Q, P) and JILL (Q, P) on the x-axis. The Y-axis would show the means.
Here is my drawing for this:
Bar Graph 2
----------
GRAPH 4...
In the final plot, I want to graph only the mean measurements for "BOB" sub-level "P"
over time. Let's say that each M1, M2, M3, M4, M5 was taken at unique times T1, T2, T3, T4, T5 where the times could have values T1=15, T2=45, T3=55, T4=65, T5=70 and they need to be spaced properly on a timeline (i.e. the distance between T1 and T2 should be 30 units, T2 and T3 10 units, T3 and T4 10 units, T4 and T5 5 units) --
I spaced them evenly on the graph, so ignore that.
Here is my drawing for this:
Plot 2
----------
THANKS!!!
If you get stuck on a command, type
?cmdfor help. For example,?rectwill tell you about drawing rectangles, and?linesfor drawing lines.I'll do the first graph to help you get started. The rest of your graphs are variations on a theme or can be modified fairly easily from your initial case:
outputFile <- "barGraph1.png"
outputFileWidth <- 6
outputFileHeight <- 6
resolution <- 150
samples <- read.table("sample_data.txt", header=TRUE, stringsAsFactors=TRUE, row.names=FALSE, col.names=c("rep", "factor", "subfactor", "m1", "m2", "m3", "m4"), sep="\t", fill=TRUE, colClasses=c('numeric', 'character', 'character', 'numeric', 'numeric', 'numeric', 'numeric'))
# set up subsets
bobPSubset <- subset(samples, ((samples$factor == 'BOB') && (samples$subfactor == 'P')))
bobQSubset <- subset(samples, ((samples$factor == 'BOB') && (samples$subfactor == 'Q')))
jillPSubset <- subset(samples, ((samples$factor == 'JILL') && (samples$subfactor == 'P')))
jillQSubset <- subset(samples, ((samples$factor == 'JILL') && (samples$subfactor == 'Q')))
# calculate values
bobPMean <- mean(bobPSubset$m1)
bobQMean <- mean(bobQSubset$m1)
jillPMean <- mean(jillPSubset$m1)
jillQMean <- mean(jillQSubset$m1)
bobMax <- ifelse(bobPMean > bobQMean, bobPMean, bobQMean)
jillMax <- ifelse(jillPMean > jillQMean, jillPMean, jillQMean)
# set up the dimensions of the plot
xmin <- 0
xmax <- ifelse(bobMax > jillMax, bobMax, jillMax)
ymin <- 0
ymax <- 5
boxWidth <- 0.4;
# plot the graph
bitmap(file=outputFile, type="png256", width=outputFileWidth, height=outputFileHeight, res=resolution)
samplePlot <- plot (range(xmin, xmax), range(ymin, ymax), type="n", axes=FALSE, mar=c(1,1,1,1))
bobPRect <- rect(1-boxWidth, 0, 1+boxWidth, bobPMean, col="red", border=NA)
bobQRect <- rect(2-boxWidth, 0, 2+boxWidth, bobQMean, col="red", border=NA)
jillPRect <- rect(3-boxWidth, 0, 3+boxWidth, jillPMean, col="red", border=NA)
jillQRect <- rect(4-boxWidth, 0, 4+boxWidth, jillQMean, col="red", border=NA)
horizLabel <- text(seq(1:4), 0-(xmax * 0.05), labels=c("BobP", "BobQ", "JillP", "JillQ"), adj=0.5)
dev.off()
posted by Blazecock Pileon at 5:29 PM on April 9, 2009