本文介绍了使用ggplot2中的facet_grid(),是否可以只在一个子图上显示annotation_logtics()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码在ggplot2中使用facet_grid()创建具有三个子图的图:

I am using the following code to create a plot with three subplots using facet_grid() within ggplot2:

day <- c('5-Aug', '5-Aug','5-Aug','10-Aug','10-Aug','10-Aug','17-Aug','17-Aug','17-Aug')
station <- c(1:3,1:3,1:3)
Mean <- c(382, 1017, 1519, 698, 5398, 2458, 346, 5722, 6253)
StErr<- c(83, 100, 73, 284, 3417, 689, 53, 1796, 732)
df <- data.frame(day,station,Mean,StErr)

library(ggplot2)
library(scales)
ggplot(df, aes(x=station, y=Mean)) + 
geom_errorbar(aes(ymin=Mean-StErr, ymax=Mean+StErr), colour="black", width=.1) +
geom_point(size=2)+
xlab(NULL) +
ylab(expression(paste('Copepods,'~'#/m'^3))) + 
theme_bw() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
) + 
scale_x_continuous(expand=c(.3,0), breaks=c(1:3), labels=c("In", "FL", "Off")) + 
annotation_logticks(sides = "l") + 
scale_y_log10(limit=c(100,10000)) +
theme(axis.text.x=element_text(size=12)) +
theme(axis.text.y=element_text(size=12)) +
facet_grid(.~day)

但是,我使用annotation_logticks()创建的对数轴刻度线出现在所有三个子图上. 问题:有人知道如何控制facet_grid()注解_logticks()中的哪些子图出现吗?

However, the log axis ticks I've created using annotation_logticks() appear on all three subplots. Question: Does anyone know how to control which subplots within facet_grid() annotation_logticks() appear on?

我向代码中添加了数据,并希望这是一种包含示例数据的更可接受和有用的方法.我将此代码复制到一个新的R会话中,它似乎可以完成我期望的工作.非常感谢您提供的有用链接!

I added data to the code and hope that this is a more acceptable and useful way to include sample data. I copied this code into a new R session and it appears to do what I expect. Thank you very much for the helpful link!

推荐答案

当前,函数annotation_logticks在创建的层中对data对象进行硬编码.

Currently the function annotation_logticks hard codes the data object in the created layer.

您可以创建自己的函数,使您可以传递data.frame,其中包含要添加注释的构面变量和级别

You can create your own function that allows you to pass a data.frame containing the faceting variable and levels you wish to add the annotation

add_logticks  <- function (base = 10, sides = "bl", scaled = TRUE, 
   short = unit(0.1, "cm"), mid = unit(0.2, "cm"),  long = unit(0.3, "cm"), 
   colour = "black",  size = 0.5, linetype = 1, alpha = 1, color = NULL, 
   data =data.frame(x = NA),... )   {
  if (!is.null(color)) 
    colour <- color
  layer(geom = "logticks", geom_params = list(base = base, 
          sides = sides, raw = raw, scaled = scaled, short = short, 
          mid = mid, long = long, colour = colour, size = size, 
          linetype = linetype, alpha = alpha, ...), 
        stat = "identity", data =data , mapping = NULL, inherit.aes = FALSE, 
        show_guide = FALSE)
}
# The plot without logticks
overall <- ggplot(df, aes(x=station, y=Mean)) + 
  geom_errorbar(aes(ymin=Mean-StErr, ymax=Mean+StErr), colour="black", width=.1) +
  geom_point(size=2)+
  xlab(NULL) +
  ylab(expression(paste('Copepods,'~'#/m'^3))) + 
  theme_bw() +
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank()
  ) + 
  scale_x_continuous(expand=c(.3,0), breaks=c(1:3), labels=c("In", "FL", "Off")) + 
  scale_y_log10(limit=c(100,10000)) +
  theme(axis.text.x=element_text(size=12)) +
  theme(axis.text.y=element_text(size=12)) +
  facet_grid(.~day)

overall + add_logticks(side = 'l', data = data.frame(x= NA, day = '5-Aug'))

这篇关于使用ggplot2中的facet_grid(),是否可以只在一个子图上显示annotation_logtics()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 09:00