0

我创建了一个带有“ggplot”的堆积条形图,以显示移植实验的核型(分子)结果,每个面板代表一个位置,x 轴是各种底物,而 y 轴是百分比三种核型中的每一种。

我查看了 Stack Overflow 中的几个问题和答案示例,但无法弄清楚如何执行以下操作:

  1. 将堆叠条的每个部分中的值(应该四舍五入到小数点后两位)居中,现在我只是让它们从顶部偏移。
  2. 如何将我的图例文本从“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")

在此处输入图像描述

4

1 回答 1

1

要将希腊字母添加到图例中,可以使用 scale_colour_manual() 更改色标:

test = data.frame(x=1:30,y=1:30,label=rep(c("BB","BD","DD"),each=10))
ggplot(test) + geom_point(aes(x=x,y=y,color=label)) + scale_colour_manual(values=c(1,2,3),breaks = c("BB","BD","DD"),labels = list(bquote(alpha~alpha),bquote(alpha~beta),bquote(beta~beta)))

参数values设置颜色,breaks设置断点(BB、BD 和 DD)并labels设置所需的希腊字母。

要舍入图例,您可以在数据框中添加另一列,将值设置为 round(Percent,digits=3),并在 geom_text 中使用此列。

可以在此处找到有关 ggplot2 中希腊字母的信息

于 2015-03-17T10:22:57.273 回答