简体   繁体   中英

R - Barchart plots empty values when using the “groups” argument

I'm plotting the amount of organisms found in several samples at different depths in the ocean using barchart.

barchart(density~depth, groups=species,
     main=list(label="Species composition in Hinlopen",cex=2),
     ylab="Species density(n pr. m3)", 
     data=species_density_depths_hinlopen,
     origin=0,
     auto.key=list(space="top",columns=3, title="Species"),
     par.settings = my_settings)

This gives me some nice bars showing species composition grouped by depth. But, not all organisms was found at every depth and barchart still makes an empty space for where that organisms bar "should've" been. Is there any way of telling barchart not to plot these empty spaces? Here is how it looks at the moment:

截图

As you can see, there are several empty spaces between the bars.

EDIT Recreatable example:

library(lattice)
dep <- c(replicate(10,10),replicate(10,20))
sp <- c(replicate(7,"Krill"),replicate(7, "Calanus"),replicate(6,"Ctenophora"))
dens <- rnorm(20,mean=5)

animals <- data.frame(species=sp,density=dens,depth=as.factor(dep))

barchart(density~depth, groups=species,
         main=list(label="Species composition in Kongsfjorden (KB3)",cex=2),
         ylab="Species density(n pr. m3)", 
         data=animals)

One way would be to create a new factor that is a cross-product of the two you are displaying, then after populating it drop the empty levels, then plot it as a bar graph while coloring it yourself.

A lot of work really. Maybe someone knows a better idea. If you post sample data someone will probably take a crack at it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM