这是一个可以为您提供大部分所需内容的解决方案。但请注意,ggplot 并非旨在允许在单个图中使用单独的“阴影”和“颜色”参数。相反,我使用灰色填充颜色为您的类别obs
和proc
类别着色,并将物种分组为多个方面(而不是对它们进行不同的着色)。
library(ggplot2)
library(reshape2)
ss.data = data.frame(
pop=c("E1", "E2", "E3", "E4", "E5", "E6", "E7", "C1", "C2", "C3", "C4"),
obs=c(0.0027, 0.0018, 0.0464, 0.0095, 0.0034, 0.0117, 0.017, 0.1178,
0.0449, 0.039, 0.0903),
proc=c(0.0319, 0.0196, 0.0511, 0.0143, 0.0048, 0.0078, 0.0396, 0.1662,
0.074, 0.1681, 0.1358), stringsAsFactors=FALSE)
# Add new column 'species' by removing the trailing digits from 'pop'.
ss.data$species = gsub("\\d", "", ss.data$pop)
# Convert data to long-form with 'melt' from the reshape2 package.
mdat = melt(ss.data, id.vars=c("pop", "species"),
measure.vars=c("obs", "proc"))
plot_1 = ggplot(mdat, aes(x=pop, y=value, fill=variable)) +
theme_bw() +
geom_bar(position="stack", stat="identity") +
scale_fill_manual(values=c("grey50", "grey80")) +
facet_grid(. ~ species, space="free_x", scales="free_x",
labeller=label_both)
ggsave("plot_1.png", plot=plot_1, width=6.5, height=4)