ggplot2中的注释轴刻度和面板绘图

3
这应该很简单。如果我制作面板图并想添加annotation_logticks,则所有图中都会出现刻度线。
如果我只想在faceting的第一列上显示刻度线,怎么办?这可能吗?
例如,使用CO2数据集:
structure(list(Plant = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L), .Label = c("Qn1", "Qn2", "Qn3", "Qc1", "Qc3", "Qc2", 
"Mn3", "Mn2", "Mn1", "Mc2", "Mc3", "Mc1"), class = c("ordered", 
"factor")), Type = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Quebec", 
"Mississippi"), class = "factor"), Treatment = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L), .Label = c("nonchilled", "chilled"), class = "factor"), 
    conc = c(95, 175, 250, 350, 500, 675, 1000, 95, 175, 250, 
    350, 500, 675, 1000, 95, 175, 250, 350, 500, 675, 1000, 95, 
    175, 250, 350, 500, 675, 1000, 95, 175, 250, 350, 500, 675, 
    1000, 95, 175, 250, 350, 500, 675, 1000, 95, 175, 250, 350, 
    500, 675, 1000, 95, 175, 250, 350, 500, 675, 1000, 95, 175, 
    250, 350, 500, 675, 1000, 95, 175, 250, 350, 500, 675, 1000, 
    95, 175, 250, 350, 500, 675, 1000, 95, 175, 250, 350, 500, 
    675, 1000), uptake = c(16, 30.4, 34.8, 37.2, 35.3, 39.2, 
    39.7, 13.6, 27.3, 37.1, 41.8, 40.6, 41.4, 44.3, 16.2, 32.4, 
    40.3, 42.1, 42.9, 43.9, 45.5, 14.2, 24.1, 30.3, 34.6, 32.5, 
    35.4, 38.7, 9.3, 27.3, 35, 38.8, 38.6, 37.5, 42.4, 15.1, 
    21, 38.1, 34, 38.9, 39.6, 41.4, 10.6, 19.2, 26.2, 30, 30.9, 
    32.4, 35.5, 12, 22, 30.6, 31.8, 32.4, 31.1, 31.5, 11.3, 19.4, 
    25.8, 27.9, 28.5, 28.1, 27.8, 10.5, 14.9, 18.1, 18.9, 19.5, 
    22.2, 21.9, 7.7, 11.4, 12.3, 13, 12.5, 13.7, 14.4, 10.6, 
    18, 17.9, 17.9, 17.9, 18.9, 19.9)), .Names = c("Plant", "Type", 
"Treatment", "conc", "uptake"), row.names = 1:84, class = c("nfnGroupedData", 
"nfGroupedData", "groupedData", "data.frame"), formula = uptake ~ 
    conc | Plant, outer = ~Treatment * Type, labels = structure(list(
    x = "Ambient carbon dioxide concentration", y = "CO2 uptake rate"), .Names = c("x", 
"y")), units = structure(list(x = "(uL/L)", y = "(umol/m^2 s)"), .Names = c("x", 
"y")))

使用以下这段简短的代码:

ggplot(data, aes(conc, uptake))+
  geom_point()+
  scale_y_log10(breaks=c(1,10,100))+
  facet_grid(~Type)+
  annotation_logticks(sides="l")

请提供一些示例数据和您迄今为止使用的代码,以便使其可重现。 - Thomas
@Thomas,我添加了一个小例子。 - matteo
1个回答

2

考虑以下示例图

p <- ggplot(mtcars, aes(disp, hp)) +
  facet_wrap(~vs) +
  geom_point() +
  scale_y_log10()

您可以使用以下“hack”以仅针对由vs=c(0)识别的分面显示刻度线。

a <- annotation_logticks(sides='l')
a$data <- data.frame(x=NA, vs=c(0))
p + a

不幸的是,它会引发警告。


谢谢Rosen,这是一个不错的解决方法。但是有没有一种“正常”的方法来指定刻度线应该显示在哪里? - matteo
如果使用annotation_logticks,我认为这是最“正常”的情况。该函数仅返回一个带有固定数据data.frame(x=NA)的层。您可以复制并修改annotation_logticks以接受数据参数并将其传递到层。 - Rosen Matev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接