Julien,
> require(ggplot2)
>
> ####Loading database####
> data2 <- structure(list(Semaine = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
> 47L, 48L, 49L, 50L, 51L, 52L, 53L), Obs = c(61L, 25L, 0L, 2L,
> 7L, 3L, 8L, 5L, 2L, 0L, 11L, 8L, 4L, 8L, 153L, 0L), Mega = c(17L,
> 7L, 0L, 3L, 9L, 3L, 4L, 1L, 1L, 0L, 4L, 28L, 12L, 11L, 3L, 1L
> ), Marmo = c(401L, 199L, 10L, 50L, 147L, 52L, 34L, 89L, 49L,
> 4L, 114L, 555L, 369L, 325L, 270L, 14L)), .Names = c("Semaine",
> "Obs", "Mega", "Marmo"), class = "data.frame", row.names = c(NA,
> -16L))
>
> ####Graph####
> x11()
> qplot(Semaine, data=data2, geom="bar", fill=Espece)
Either your data or your qplot command are wrong. There is no Espece
in the data.frame provided so the fill will fail. The data appear
to represent counts with the columns being the species, so there should
be something in the qplot command to tell it not to bin the data within
Semaine, but the data are still in the wrong format even then.
I suspect you have sent a summary of your data, which will result in a
different answer to sending the actual data.
But here goes...
> #My first issue is exactly what you understood, i want all of these bars
> to total 100%
Given the data supplied I would convert the the three species columns
into percentages to begin with:
data1 <- data2
data2[,c("Obs","Mega","Marmo")] <-
data2[,c("Obs","Mega","Marmo")]*100/rowSums(data2[,c("Obs","Mega","Marmo")])
then change the data.frame to long format...
library(reshape2)
mdata<-melt(data2,id.vars="Semaine",
variable.name="Espece")
and then (sorry I don't use qplot, so I'm sticking with ggplot here)..
ggplot(mdata,aes(x=Semaine,y=value,fill=Espece))+geom_bar(stat="identity")
Alternatively, without converting the data into percentages:
mdata<-melt(data1,id.vars="Semaine",
variable.name="Espece")
ggplot(mdata,aes(x=Semaine,y=value,fill=Espece))+geom_bar(stat="identity",position="Fill")
But that gives a 0-1 rather than 0-100 scale (could probably be
modified by relabelling a scale?).
> #My second one is purely visual, i want my bars on the middle of the
> graph (centered from week 47 to 9) and not at both sides.
I assume therefore that the data are weeks of the year and
that the 1-9 are from the year after the 47-53? You could
make week a factor:
mdata1$week <- factor(mdata1$Semaine,levels=c(47:53,1:9))
ggplot(mdata1,aes(x=week,y=value,fill=Espece))+geom_bar(stat="identity",position="Fill")
If your actual data aren't in the form of the data2 counts sent, I
suspect they are more like:
ddd<-do.call(rbind,apply(data1,1,function(x){data.frame(Semaine=rep(x[1],sum(x[2:4])),Espece=rep(c("Obs","Mega","Marmo"),times=x[2:4]))}))
In which case your original qplot command works (substituting ddd for
data2) as does this:
ddd$week <- factor(ddd$Semaine,levels=c(47:53,1:9))
ggplot(ddd,aes(x=week))+geom_bar(aes(fill=Espece),position="Fill")+ylab("Proportion")+xlab("Week")
> Hope it works
No it didn't, and you really should have checked that before sending it.
I'm only having a play with this as I have some other stuff running,
otherwise I would have stopped as soon as the example failed.
I'm not as grumpy as that last paragraph sounded.
Ron.