How to scale points in R plot? -
i have 40 pairs of birds each male , female scored colour. colour score categorical variable (range of 1 9). plot frequency of number of males , female pairs colour combinations. have created 'table' number of each combination (1/1, 1/2, 1/3, ... 9/7, 9/8, 9/9), converted vector called 'colour_count'. use 'colour_count' 'cex' parameter in 'plot' scale size of each combination of colours. not work because of order data read table. how create vector frequency of each colour combination scale plot points?
see data , code below:
## dataset pairs of males , females , colour classes pair_colours <- structure(list(male = c(7, 6, 4, 6, 8, 8, 5, 6, 6, 8, 6, 6, 5, 7, 9, 5, 8, 7, 5, 5, 4, 6, 7, 7, 3, 6, 5, 4, 7, 4, 3, 9, 4, 4, 4, 4, 9, 6, 6, 6), female = c(9, 8, 8, 9, 3, 6, 8, 5, 8, 9, 7, 3, 6, 5, 8, 9, 7, 3, 6, 4, 4, 4, 8, 8, 6, 7, 4, 2, 8, 9, 5, 6, 8, 8, 4, 4, 5, 9, 7, 8)), .names = c("male", "female"), class = "data.frame", row.names = c(na, 40l)) pair_colours[] <- as.data.frame(lapply(pair_colours, factor, levels=1:9)) ## table of pair colour values (colours 1 9 - categoricial variable) table(pair_colours$male, pair_colours$female) colour_count <- as.vector(table(pair_colours$male, pair_colours$female)) #<- problem occurs here ## plot results visisually possible assortative mating colour op<-par(mfrow=c(1,1), oma=c(2,4,0,0), mar=c(4,5,1,2), pty = "s") plot(1,1, xlim = c(1, 9), ylim = c(1, 9), type="n", xaxt = "n", yaxt = "n", las=1, bty="n", cex.lab = 1.75, cex.axis = 1.5, main = null, xlab = "male colour", ylab = "female colour", pty = "s") axis(1, @ = seq(1, 9, = 1), labels = t, cex.lab = 1.5, cex.axis = 1.5, tick = true, tck = -0.015, lwd = 1.25, lwd.ticks = 1.25) axis(2, @ = seq(1, 9, = 1), labels = t, cex.lab = 1.5, cex.axis = 1.5, tick = true, tck = -0.015, lwd = 1.25, lwd.ticks = 1.25, las =2) points(pair_colours$male, pair_colours$female, pch = 21, cex = colour_count, bg = "darkgray", col = "black", lwd = 1)
you can summarise data function ddply()
of library plyr , use new data frame plot data. counts in column v1
of new data frame.
library(plyr) df<-ddply(pair_colours,.(male,female),nrow) df male female v1 1 3 5 1 2 3 6 1 3 4 2 1 4 4 4 3 points(df$male, df$female, pch = 21, cex = df$v1, bg = "darkgray", col = "black", lwd = 1)
update - solution using aggregate
other possibility use function aggregate()
. first, add new column n
contains values 1. aggregate()
sum n
values each male
, female
combination.
pair_colours$n<-1 aggregate(n~male+female,data=pair_colours,fun=sum) male female n 1 4 2 1 2 6 3 1 3 7 3 1 4 8 3 1 5 4 4 3
Comments
Post a Comment