【问题标题】:How to center values in stackbar plot and add greek text to legend with ggplot如何在堆积条形图中居中值并使用ggplot将希腊文本添加到图例
【发布时间】:2015-03-17 08:50:34
【问题描述】:

我创建了一个带有“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")

【问题讨论】:

    标签: r ggplot2 legend stackedbarseries


    【解决方案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中有关希腊字母的信息可以在here找到

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-11
      相关资源
      最近更新 更多