[R] how do i creat multiple back to back histograms?
ONKELINX, Thierry
Thierry.ONKELINX at inbo.be
Tue Jan 29 13:50:39 CET 2008
Maybe this is what you'd like.
library(ggplot2)
freqs <- data.frame(value= c(
0.000,1.204,1.301,1.362,1.447,1.505,1.602,1.653,1.756,1.806,1.903,1.959,
2.053,2.107,2.204,2.258,2.354,2.408,2.505,2.559,2.656,2.709,2.806)
,
tp1= c( 8,1,0,13,0,6,0,25,0,5,0,15,0,4,0,7,0,0,0,1,0,0,0)
,
tn1= c( 17,0,0,2,0,0,0,1,0,2,0,1,0,0,0,0,0,0,0,0,0,0,0)
,
tp2= c( 10,0,2,0,9,0,8,0,19,0,4,0,5,0,2,0,5,0,2,0,1,0,2)
,
tn2= c( 13,0,1,0,1,0,2,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0)
,
tp3= c( 9,0,0,0,0,0,0,0,0,10,0,10,0,21,0,10,0,11,0,8,0,5,0)
,
tn3= c( 15,0,0,0,0,0,0,0,0,3,0,2,0,2,0,1,0,0,0,0,0,0,0)
)
freqsMelt <- melt(freqs, id = "value", measure.var = 2:7)
colnames(freqsMelt)[3] <- "n"
freqsMelt$test <- factor(paste("test", substr(freqsMelt$variable, 3,
3)))
freqsMelt$posneg <- factor(substr(freqsMelt$variable, 2, 2))
freqsMelt$n[freqsMelt$posneg == "n"] <- - freqsMelt$n[freqsMelt$posneg
== "n"]
ggplot(freqsMelt, aes(x = factor(value), y = n, fill = posneg)) +
geom_bar(stat= "identity", position = "identity") + facet_grid(. ~test)
+ coord_flip()
HTH,
Thierry
------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature
and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
Thierry.Onkelinx op inbo.be
www.inbo.be
Do not put your faith in what statistics say until you have carefully
considered what they do not say. ~William W. Watt
A statistical analysis, properly conducted, is a delicate dissection of
uncertainties, a surgery of suppositions. ~M.J.Moroney
-----Oorspronkelijk bericht-----
Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org]
Namens Tom Willems
Verzonden: dinsdag 29 januari 2008 12:44
Aan: r-help op r-project.org
Onderwerp: [R] how do i creat multiple back to back histograms?
dear R-ussers,
I would like to creeate a graph, i wich my data is presented as
verticaly oriented histograms, wich give the frequency of the measured
values, grouped per used measurement methode.
So the X axis should hold the grouping variable and the Y axis a
continuos variable. well not realy continouse, but it should show the
values, representing the clase intervals.
small example to clarify this:
This one i can create: graph 1
only the positive results per test
5 | ## |# |##
| # |## |#
4 | ### |#### |
| #### |###### |##
3 | ### |##### |####
| ##### |### |#####
2 | #### | |######
| ### |## |#####
1 | # |# |##
|_____________|_____________|_____________
0 test 1 test2 test3
Tihs is what i want to create: graph 2
a back to back histogram plot of the pos/negative results, grouped per
test
5 | |## |#
|##
| |# |##
#|#
4 | |### |#### ##|
| .....................|####..............|######
...........#|##......................
3 | #|### #|##### |####
| ##|##### ##|### ##|#####
2
|............#####|####......####|..............######|######...........
...
| ###|### ##|## ###|#####
1 | ##|# #|# ####|##
|___________|__________#|____________#|_____________
0 test 1 test2
test3
Neg. | Pos. Neg. | Pos. Neg. | Pos.
I 'd like to creat the figure of graph 2, a back to back plot of the
pos/ and negative results of a test, and this with the 3 tests in one
graf.
I have been searching for examples, the only trouble is that it is way
to complex.
(http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=109)
Here is one other example of a back to back plot (graph 2) .
(histbackback(fool)
After trying to understand wath happens in the complex example, i
identified a part that does what i need.
It does creat the graph similar to what i want (graph 1). "see code
below"
Yet i do not understand it wel enough,so i can't creat the more complex
graph 2.
What can i do to understand this graph functions beter, without spending
to much time on them?
Kind regards,
Tom.
this is the code, wich i use to creat graph 1 # data for plot:
freqs <- data.frame(value= c(
0.000,1.204,1.301,1.362,1.447,1.505,1.602,1.653,1.756,1.806,1.903,1.959,
2.053,2.107,2.204,2.258,2.354,2.408,2.505,2.559,2.656,2.709,2.806)
,
tp1= c( 8,1,0,13,0,6,0,25,0,5,0,15,0,4,0,7,0,0,0,1,0,0,0)
,
tn1= c( 17,0,0,2,0,0,0,1,0,2,0,1,0,0,0,0,0,0,0,0,0,0,0)
,
tp2= c( 10,0,2,0,9,0,8,0,19,0,4,0,5,0,2,0,5,0,2,0,1,0,2)
,
tn2= c( 13,0,1,0,1,0,2,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0)
,
tp3= c( 9,0,0,0,0,0,0,0,0,10,0,10,0,21,0,10,0,11,0,8,0,5,0)
,
tn3= c( 15,0,0,0,0,0,0,0,0,3,0,2,0,2,0,1,0,0,0,0,0,0,0)
)
test<-c(1,2,3,4,5,6)
testname <-c('test1 p','test1 n','test2 p','test2 n','test3 p','test3
n')
# parameters for plot
xlim = c(min(test),max(test))
ylim = c(0,length(freqs$value))
barscale = 0.2
barcol = 8
# plot
win.graph()
for (i in 1:length(freqs))
{
par(new = TRUE)
xmin <- -test[i] + xlim[1]
xmax <- xlim[2] - test[i]
ser <- freqs[, i+1]
ser <- ser/max(ser) * barscale
barplot(ser, horiz = TRUE, axes = FALSE, xlim = c(xmin,
xmax),
ylim = ylim, col = barcol, space = 0)
}
axis(1,labels=testname,at=c(0,0.2,0.4,0.6,0.8,1))
axis(2,labels=freqs$value ,at=c((0:22)/23) )
this is the code, wich i hoped would creat graph 2 but it doesn't work
for (i in 1:length(freqs))
{
par(new = TRUE)
xmin <- -test[i] + xlim[1]
xmax <- xlim[2] - test[i]
serx <- freqs[, i+1]
sery <- freqs[, i+2]
ser <- list((serx/sum(serx) * barscale),(sery/sum(sery)
*
barscale))
histbackback(ser,axes=FALSE ,xlim = c(xmin, xmax), ylim
=
ylim)
}
E-mail: tom.willems op var.fgov.be
Disclaimer: click here
[[alternative HTML version deleted]]
______________________________________________
R-help op 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
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list