ggplot2中的annotation_logticks和facet图

时间:2014-02-26 11:17:02

标签: r ggplot2

这应该很简单。如果我生成构面图并且我想添加annotation_logticks,则在所有图中都会显示刻度。

如果我想仅在刻面的第一个“列”上打勾,该怎么办?有可能吗?

enter image description here

例如,使用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")

1 个答案:

答案 0 :(得分:0)

给出示例情节

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

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