Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a barplot with two different variables on R

Tags:

graph

r

bar-chart

would like to plot the following data on the same barplot. it is a length frequency barplot showing the males and the females of a population with respect to their length classes:

I am new to this and i dont know how to put my data here, but here is an example:

Lengthclass Both    Males   Females
60  7   5   2
70  10  5   5
80  11  6   5
90  4   2   2
100 3   3   0
110 3   0   3
120 1   1   0
130 0   0   0
140 1   0   1
150 2   0   2

If i use this code: {barplot()} it does not give me all three variables on the same plot.

i need a graph the looks like this but on R. enter image description here

Thank you:)

like image 750
Myr Avatar asked Oct 31 '25 16:10

Myr


1 Answers

classes <- levels(cut(60:100, breaks = c(60,70,80,90,100),
                      right =FALSE))

my.df <- data.frame(lengthclass = classes,
                     both = c(7,10,11,4),
                     male = c(5,5,6,2),
                     female = c(2,5,5,2))

barplot(t(as.matrix(my.df[, 2:4])), 
        beside = TRUE,
        names.arg = my.df$lengthclass,
        legend.text = TRUE,
        ylim = c(0,12),
        ylab = "number of individuals",
        xlab = "Length class (cm)")
like image 70
Luca Braglia Avatar answered Nov 02 '25 08:11

Luca Braglia