Hi All, New to R, but committed. I looked in a number of places but can't figure out my current problem. I have date of the type: Time Type1 Type2 Type3 1 .50 .25 .25 4 .55 .25 .20 5 .65 .20 .15 etc which describe the frequency of types 1, 2 and 3 (adding up to 100%) over time. I would like to create a stacked bar chart showing these frequencies, each bar of height = 1, subsections of bars proportional to the frequency, and each bar located at the correct X (time) position. One difficulty is that the desired spacing of bar locations on the x-axis is irregular. Is this possible in R? Many thanks, Marcel -- View this message in context: http://r.789695.n4.nabble.com/Stacked-bar-plot-of-frequency-vs-time-tp3659715p3659715.html Sent from the R help mailing list archive at Nabble.com.
Marcel, tC <- textConnection(" Time Type1 Type2 Type3 1 .50 .25 .25 4 .55 .25 .20 5 .65 .20 .15 ") tmp <- read.table(header=TRUE, tC) close.connection(tC) require(lattice) tmpdf <- data.frame(Time=rep(tmp$Time, 3), stack(tmp[,2:4])) tmpdf barchart(values ~ Time, group=ind, data=tmpdf, stack=TRUE, horizontal=FALSE, main="not there yet") xyplot(values ~ Time, group=ind, data=tmpdf, stack=TRUE, horizontal=FALSE, panel=panel.barchart, ylim=c(-0.05,1.05), xlim=c(0,6), main="this does what you want") Rich On Mon, Jul 11, 2011 at 10:36 AM, marcel <marcelcurlin@gmail.com> wrote:> Hi All, > New to R, but committed. I looked in a number of places but can't figure > out > my current problem. I have date of the type: > > Time Type1 Type2 Type3 > 1 .50 .25 .25 > 4 .55 .25 .20 > 5 .65 .20 .15 > etc > > which describe the frequency of types 1, 2 and 3 (adding up to 100%) over > time. I would like to create a stacked bar chart showing these frequencies, > each bar of height = 1, subsections of bars proportional to the frequency, > and each bar located at the correct X (time) position. One difficulty is > that the desired spacing of bar locations on the x-axis is irregular. Is > this possible in R? > > Many thanks, > Marcel > > -- > View this message in context: > http://r.789695.n4.nabble.com/Stacked-bar-plot-of-frequency-vs-time-tp3659715p3659715.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
On Jul 11, 2011, at 9:36 AM, marcel wrote:> Hi All, > New to R, but committed. I looked in a number of places but can't figure out > my current problem. I have date of the type: > > Time Type1 Type2 Type3 > 1 .50 .25 .25 > 4 .55 .25 .20 > 5 .65 .20 .15 > etc > > which describe the frequency of types 1, 2 and 3 (adding up to 100%) over > time. I would like to create a stacked bar chart showing these frequencies, > each bar of height = 1, subsections of bars proportional to the frequency, > and each bar located at the correct X (time) position. One difficulty is > that the desired spacing of bar locations on the x-axis is irregular. Is > this possible in R? > > Many thanks, > MarcelIn addition to Rich's solution using lattice, here is one possible approach using base graphics:> dput(DF)structure(list(Time = c(1L, 4L, 5L), Type1 = c(0.5, 0.55, 0.65 ), Type2 = c(0.25, 0.25, 0.2), Type3 = c(0.25, 0.2, 0.15)), .Names = c("Time", "Type1", "Type2", "Type3"), class = "data.frame", row.names = c(NA, -3L))> DFTime Type1 Type2 Type3 1 1 0.50 0.25 0.25 2 4 0.55 0.25 0.20 3 5 0.65 0.20 0.15 # Create a newdata frame with a column with the full Time sequence TimeFill <- data.frame(Time = with(DF, min(Time):max(Time)))> TimeFillTime 1 1 2 2 3 3 4 4 5 5 # merge the full sequence and the original DF together. See ?merge DF2 <- merge(DF, TimeFill, all = TRUE)> DF2Time Type1 Type2 Type3 1 1 0.50 0.25 0.25 2 2 NA NA NA 3 3 NA NA NA 4 4 0.55 0.25 0.20 5 5 0.65 0.20 0.15 # Now transpose the columns in DF to the matrix required for the plot barplot(t(DF2[, -1]), names.arg = DF2$Time) HTH, Marc Schwartz
Thank you for the solutions! I have the first one working and it does exactly what I am looking for. Unfortunately I have to put the plot in a common figure alongside other plots made in the basic environment (challenging!). With the second method, I was unable to make the stacked bars locate to the appropriate positions along the X axis (ie the appropriate time), which, though unconventional is required for my figure. So I am still looking for a complete solution in the basic plotting environment. I have boiled my problem down to this minimal example: # Made-up data tC <- textConnection(" Time Type1 Type2 Type3 1.3 .50 .25 .25 4.5 .55 .25 .20 5.2 .65 .20 .15 ") data1 <- read.table(header=TRUE, tC) data2 <- data.frame(Time=rep(data1$Time, 3), stack(data1[,2:4])) close.connection(tC) # PLOT1 Scatterplot attach(data1) par(mar=c(1,1,1,1)) plot(Time, Type1, frame=T, ylab="Divergence", col=rgb(0,100,0,50,maxColorValue=255), main="plot 1", xlim= c(0,6), ylimc(0, 1), axes=FALSE, xlab=" ") detach(data1) # PLOT2 barplot require(lattice) attach(data2) barchart(values ~ Time, group=ind, data=data2, stack=TRUE, horizontal=FALSE, main="not there yet") plot2 <- xyplot(values ~ Time, group=ind, data=data2, stack=TRUE, horizontal=FALSE, panel=panel.barchart, ylim=c(-0.05,1.05), xlim=c(0,6), main="Plot 2- how can I plot below plot1?") print(plot2) detach(data2) The only thing left is to get both plots to be vertically aligned, one above the other on the same figure. Is this possible? Thanks for all of your thoughts. Marcel Marcel -- View this message in context: http://r.789695.n4.nabble.com/Stacked-bar-plot-of-frequency-vs-time-tp3659715p3669311.html Sent from the R help mailing list archive at Nabble.com.