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)")
a barplot with two different variables on R
Domanda
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.
Thank you:)
Soluzione
Altri suggerimenti
Your barplot is known as a "grouped barplot" (in contrast to a "stacked barplot").
Arrange your data in a matrix
and use beside=TRUE
in your call to barplot()
. Here is an example using a built-in dataset:
> VADeaths
Rural Male Rural Female Urban Male Urban Female
50-54 11.7 8.7 15.4 8.4
55-59 18.1 11.7 24.3 13.6
60-64 26.9 20.3 37.0 19.3
65-69 41.0 30.9 54.6 35.1
70-74 66.0 54.3 71.1 50.0
> barplot(VADeaths,beside=TRUE)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow