当 x 轴值为日期时难以注释图 [英] Difficulty annotating plot when x axis values are dates

查看:20
本文介绍了当 x 轴值为日期时难以注释图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数据框:

> s
         Date Average Treatment        SE
1  2014-10-15   4.875         1 1.6304852
2  2014-11-12   6.250         1 0.9955257
3  2014-12-11   0.250         1 0.1636634
4  2015-02-11   1.000         1 0.5000000
5  2015-03-09   0.000         1 0.0000000
6  2014-10-15  22.750         2 3.4369318
7  2014-11-12  10.625         2 2.1207942
8  2014-12-11   7.500         2 2.9215945
9  2015-02-11   7.750         2 2.3126207
10 2015-03-09   0.125         2 0.1250000

我正在使用此代码生成一个图:

I am producing a plot using this code:

s$Date <- as.Date(s$Date)
s$Treatment <- factor(s$Treatment)
cols <- c("#000000","#F5F5DC")
library(ggplot2)
solitaryrecruits <- (ggplot(s, aes(x=Date, y=Average, fill=Treatment)) + 
  geom_bar(width=20, position=position_dodge(20),
           stat="identity", colour="black") +
  geom_errorbar(aes(ymin=Average, ymax=Average+SE),
                width=10,                    
                position=position_dodge(20))+
  scale_fill_manual(name="Treatment", labels=c("No Cage", "Cage"), values = cols) +
  labs(x=("Panel Date"), y=expression(bold(paste("Mean Num. Ind. ( " ,cm^2, ")", sep = "")))) +
  theme_bw() +
  theme(axis.text=element_text(size=14),
        legend.title=element_text(size=14),
        legend.text=element_text(size=12),
         axis.title=element_text(size=16,face="bold"),
        legend.justification = c(1, 1), legend.position = c(1, 1),
        axis.title.x = element_text(vjust=-0.2),
        axis.title.y = element_text(vjust=+0.6))) +
  guides(fill = guide_legend(override.aes = list(colour = NULL))) +
 theme(legend.key = element_rect(colour = "black"))

我有几个情节要并排展示.因此,我试图通过在左上角放置文本(对于这个图a)")来注释每个图.我试图创建一个注释层,但我正在努力指定一个 x 位置.任何帮助将不胜感激,谢谢!

I have several plots that I am going to present alongside each other. Therefore, I am trying to annotate each plot by placing the text (for this plot "a)") in the top left corner. I have tried to create an annotation layer but I am struggling to specify an x position. Any assistance would be greatly appreciated, thanks!

这是一张手动编辑的照片,用于显示我想要放置在绘图中的内容:

here is a manually edited photo to show what I want to place onto the plot:

推荐答案

你可以使用 annotate 来达到你的目标:

You can use annotate to reach your goal:

solitaryrecruits +
    annotate(geom="text",x=as.Date("2014-10-05"),
    y=25,label="a",fontface="bold")

重要的一点是您将 x 坐标作为日期.

The important point is that you give your x-coordinate as a date.

或者,您可以通过给它们一个标题来标记您的图

Alternatively, you could label your plots by giving them a title

solitaryrecruits + labs(title="a")

这篇关于当 x 轴值为日期时难以注释图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆