Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以使用ggplot2中的facet_grid()将注释_logtics()仅显示在一个子地块上?_R_Ggplot2 - Fatal编程技术网

是否可以使用ggplot2中的facet_grid()将注释_logtics()仅显示在一个子地块上?

是否可以使用ggplot2中的facet_grid()将注释_logtics()仅显示在一个子地块上?,r,ggplot2,R,Ggplot2,我使用以下代码在ggplot2中使用facet_grid()创建一个包含三个子地块的绘图: 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, 10

我使用以下代码在ggplot2中使用facet_grid()创建一个包含三个子地块的绘图:

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)

day当前函数
annotation\u logticks
对所创建层中的
数据
对象进行硬编码

您可以创建自己的函数,该函数允许您传递一个
data.frame
,其中包含要添加注释的镶嵌面变量和级别

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'))

add_logticks Hi欢迎来到stackoverflow!请看一看。例如,参见
dput
功能。干杯。
ggplot2::annotation_logticks
自回答后有了一些变化。这种方法仍然可以正常工作,但您需要将
geom\u params
更改为
params
,将
show\u guide
更改为
show.legend
,删除
raw=raw
(这到底是为了什么?),添加
position=“identity”
,如果要在参数列表和函数args中添加
外部