我创建了一个带有“ggplot”的堆积条形图,以显示移植实验的核型(分子)结果,每个面板代表一个位置,x 轴是各种底物,而 y 轴是百分比三种核型中的每一种。
我查看了 Stack Overflow 中的几个问题和答案示例,但无法弄清楚如何执行以下操作:
- 将堆叠条的每个部分中的值(应该四舍五入到小数点后两位)居中,现在我只是让它们从顶部偏移。
- 如何将我的图例文本从“BB”更改为希腊语“lower alpha,lower alpha”,将“BD”更改为希腊语“lower alpha,lower beta”,以及将“DD”更改为希腊语“lower beta,lower beta”。
这是一些示例数据和代码以及它生成的图的副本。
Karotype.Data <- structure(list(Location = structure(c(1L, 1L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 4L, 1L, 1L, 1L, 4L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("Kampinge", "Kaseberga", "Molle", "Steninge"), class = "factor"), Substrate = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 3L, 4L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 3L, 2L, 1L, 4L, 2L), .Label = c("Kampinge", "Kaseberga", "Molle", "Steninge"), class = "factor"), Karyotype = structure(c(1L, 3L, 4L, 1L, 3L, 3L, 4L, 4L, 4L, 3L, 1L, 4L, 3L, 4L, 2L, 3L, 1L, 4L, 3L, 2L, 4L, 3L, 4L, 2L, 3L), .Label = c("", "BB", "BD", "DD"), class = "factor")), .Names = c("Location", "Substrate", "Karyotype"), row.names = c(135L, 136L, 137L, 138L, 139L, 165L, 166L, 167L, 168L, 169L, 236L, 237L, 238L, 239L, 240L, 326L, 327L, 328L, 329L, 330L, 426L, 427L, 428L, 429L, 430L), class = "data.frame")
z.counts <- Karotype.Data %>%
group_by(Location,Substrate,Karyotype) %>%
summarise(Frequency=n())
z.freq <- z.counts %>% filter(Karyotype != '') %>%
group_by(Location,Substrate) %>%
mutate(Percent=Frequency/sum(Frequency))
z.freq
ggplot(z.freq, aes(x=Substrate, y=Percent, fill=Karyotype )) +
geom_bar(stat="identity") +
geom_text(aes(label = Percent), size = 5, vjust = 1, position = "stack") +
facet_wrap(~ Location, ncol=2) +
scale_y_continuous(name="Percentage") +
theme(strip.text.x = element_text(colour="black", size=20, face="bold"),
axis.title.x = element_text(colour="black", size=20, face="bold", vjust=-0.5),
axis.text.x = element_text(colour="black", size=18),
axis.title.y = element_text(colour="black", size=20,face="bold", vjust=1),
axis.text.y = element_text(colour="black", size=18),
legend.title = element_text(colour="black", size=20, face="bold"),
legend.text = element_text(colour="black", size = 18),
legend.position="bottom")