2014-02-26 33 views
2

这应该非常简单。如果我生成方块图,并且想要添加annotation_logticks,则所有图中都会显示刻度。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") 
+0

请提供一些示例数据和迄今为止使用的代码,以使其具有可重现性。 – Thomas

+0

@Thomas,我添加了一个小例子。 – matteo

回答

0

鉴于示例性绘图

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

,你可以使用下面的“黑客”有蜱只有通过鉴定VS = C(0)

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

不幸的是,它挑起的警告方面。

+0

感谢罗森,这是一个很好的解决方法。但是,为了指定蜱的显示位置,是否有“正常”的方式? – matteo

+0

我认为这是“正常的”,如果你使用'annotation_logticks'就可以得到。该函数仅返回一个固定数据为'data.frame(x = NA)'的图层。您可以复制和修改'annotation_logticks'来接受数据参数并将其传递给图层。 –