在ggplot中向轴标签添加文本

时间:2015-03-17 23:49:37

标签: r ggplot2 axis labels

我已经绘制了下表中的图表。

        BoatPhs      fit        se    lower    upper
1         Before 3.685875 0.3287521 3.038621 4.333130
2   After0-20NTA 3.317189 0.6254079 2.085872 4.548506
3   After0-20TAA 5.579384 0.5696270 4.457890 6.700878
4   After0-20TAP 3.932360 0.4304098 3.084960 4.779760
5  After20-40NTA 4.522714 0.7771793 2.992586 6.052842
6  After20-40TAA 4.505207 0.5500699 3.422217 5.588196
7  After20-40TAP 3.602183 0.3880538 2.838174 4.366192
8    ApproachNTA 4.039599 0.5688482 2.919638 5.159560
9    ApproachTAA 4.421112 0.5176408 3.401969 5.440255
10   ApproachTAP 4.497809 0.3978328 3.714547 5.281071

船的相位是x轴,拟合在y上绘制。目前我的轴标记为Before,ApproachNTA,ApproachTAA,ApproachTAP,After0-20NTA等。

我最初将这些标签旋转了90度,因此它们更好看,但不喜欢它的外观。我目前已经重命名了标签(使用下面的代码),以便现在显示在轴上的内容是x轴上的“在NTA TAA TAP NTA TAA TAP NTA TAA TAP之前”。

我希望添加一些文字以便对标签进行分组。如第一组NTA TAA TAP下的“接近”,第二组下的“After0-20”和第三组下的“After 20-40”,但我不确定如何在情节之外做到这一点。尝试使用annotate函数,但它只允许绘图区域内的文本。

非常感谢任何帮助。

干杯

# Speed plot
Spdplot <- ggplot(y, aes(x=BoatPhs, y=fit, colour=BP, ymax=max(fit)*1.05)) + 
  geom_point(position = position_dodge(width = 0.5, height = 0), size = 4) + 
  geom_errorbar(aes(ymin=fit-se, ymax=fit+se), position = position_dodge(width = 0.5, height = 0), width = 0.5, size = 0.75) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        panel.background = element_blank(), axis.line = element_line(colour = "black"))


  #run Spdplot
Spdplot




#sets x values in results order + relables x + y axes
Spdplot + scale_x_discrete(limits=c("Before","ApproachNTA","ApproachTAA","ApproachTAP","After0-20NTA","After0-20TAA","After0-20TAP", "After20-40NTA","After20-40TAA","After20-40TAP"),
                           labels=c("Before" = "Before" ,"ApproachNTA"  ="NTA","ApproachTAA" = "TAA","ApproachTAP" = "TAP",
                                    "After0-20NTA" = "NTA","After0-20TAA" = "TAA","After0-20TAP" = "TAP",
                                    "After20-40NTA" = "NTA","After20-40TAA" = "TAA","After20-40TAP" = "TAP")) + 
  xlab("Boat phase") + ylab("Group travel speed (km/hr)") + 
  annotate("text", x=6, y=6.2, label="***", size = 6) +
  annotate("text", x=4, y=4.95, label="*", size = 6) + 
  theme(axis.title.x = element_text(face="bold", size = 16, vjust= -0.5), axis.title.y = element_text(face="bold", size = 16, vjust= 1), 
        axis.text.x = element_text(size=14, face="bold"), axis.text.y = element_text(face="bold", size=14), legend.text=element_text(size=14)) +
  theme(legend.title = element_blank()) + 
  scale_colour_manual(values=c("#3399FF","#FF0000","#33CC33","#000000"), 
                      breaks=c("Before", "Approach", "After0-20", "After20-40"),
                      labels=c("Before", "Approach", "After0-20", "After20-40"))

enter image description here

1 个答案:

答案 0 :(得分:3)

你周围的许多事情都在使用annotate,如果你给ggplot提供所需的数据,就可以避免scale_x_discrete中的手动标记。在这里,我重新创建您的数据集,然后为您的绘图添加必要的信息。

# Recreate your data
y <- read.table(textConnection('BoatPhs      fit        se    lower    upper
1         Before 3.685875 0.3287521 3.038621 4.333130
2   After0-20NTA 3.317189 0.6254079 2.085872 4.548506
3   After0-20TAA 5.579384 0.5696270 4.457890 6.700878
4   After0-20TAP 3.932360 0.4304098 3.084960 4.779760
5  After20-40NTA 4.522714 0.7771793 2.992586 6.052842
6  After20-40TAA 4.505207 0.5500699 3.422217 5.588196
7  After20-40TAP 3.602183 0.3880538 2.838174 4.366192
8    ApproachNTA 4.039599 0.5688482 2.919638 5.159560
9    ApproachTAA 4.421112 0.5176408 3.401969 5.440255
10   ApproachTAP 4.497809 0.3978328 3.714547 5.281071'))

# Separate the boat phase from the time period     
y$boat.phase <- gsub('.*(NTA|TAA|TAP)','\\1',y$BoatPhs)
y$period <- gsub('(.*)(NTA|TAA|TAP)','\\1',y$BoatPhs)

# Give the time period an ordering.
y$period <- factor(y$period, 
                   levels = c("Before", "Approach", "After0-20", "After20-40"), 
                   ordered=TRUE)

# Add the stars manually, but these could be calculated programatically.
y$stars <- c('', '', '***', '', '', '', '', '', '', '*')

现在当你使用ggplot时,你可以将星星作为美学传递,你不必手动标记任何东西。要“分组”x轴标签,可以使用facet:

ggplot(y, aes(x=boat.phase, y=fit, ymax=fit+se, ymin=fit-se)) + 
  geom_point() + geom_errorbar(width = 0.5, size = 0.75) + 
  geom_text(aes(y=fit+se+0.1, label=stars), size=6) +
  facet_grid( ~ period, scales="free_x", space="free_x") +
  xlab("Boat phase") + ylab("Group travel speed (km/hr)") + 
  theme_bw()

enter image description here

这里的着色将是多余的,但如果您愿意,可以将其添加进来。我还会在下一个示例中添加您的自定义主题。

ggplot(y, aes(x=boat.phase, y=fit, ymax=fit+se, ymin=fit-se, colour=period)) + 
  geom_point() + geom_errorbar(width = 0.5, size = 0.75) + 
  geom_text(aes(y=fit+se+0.1, label=stars), size=6) +
  facet_grid( ~ period, scales="free_x", space="free_x") +
  xlab("Boat phase") + ylab("Group travel speed (km/hr)") + 
  scale_colour_manual(values=c("#000000", "#33CC33", "#3399FF","#FF0000")) +
  theme(axis.title = element_text(face="bold", size = 16),
        axis.title.x = element_text(vjust= -0.5), 
        axis.title.y = element_text(vjust= 1), 
        axis.text = element_text(size=14, face="bold"),
        panel.grid = element_blank(), 
        panel.background = element_blank(), 
        axis.line = element_line(colour = "black"))  

enter image description here

相关问题