使用facet_grid()为y轴设置两个因变量的限制

时间:2013-12-06 15:29:50

标签: r ggplot2

我有以下数据框

            Condition  congruency Distance Measure   Score  liminf  limsup    ConditionF MeasureF
     1           Zero     Neutral        0     RTs 445.000 435.000 455.000          Zero      RTs
     2           Zero   Congruent        1     RTs 445.000 435.000 456.000          Zero      RTs
     3           Zero   Congruent        2     RTs 441.000 430.000 451.000          Zero      RTs
     4           Zero   Congruent        3     RTs 432.000 422.000 442.000          Zero      RTs
     5           Zero Incongruent        1     RTs 449.000 439.000 459.000          Zero      RTs
     6           Zero Incongruent        2     RTs 449.000 438.000 460.000          Zero      RTs
     7           Zero Incongruent        3     RTs 453.000 440.000 465.000          Zero      RTs
     8  All different     Neutral        0     RTs 446.000 436.000 456.000 All different      RTs
     9  All different   Congruent        1     RTs 445.000 434.000 455.000 All different      RTs
     10 All different   Congruent        2     RTs 449.000 438.000 461.000 All different      RTs
     11 All different   Congruent        3     RTs 449.000 438.000 461.000 All different      RTs
     12 All different Incongruent        1     RTs 446.000 436.000 456.000 All different      RTs
     13 All different Incongruent        2     RTs 447.000 436.000 458.000 All different      RTs
     14 All different Incongruent        3     RTs 450.000 440.000 461.000 All different      RTs
     15          Zero     Neutral        0  Errors   0.029   0.018   0.039          Zero   Errors
     16          Zero   Congruent        1  Errors   0.023   0.015   0.031          Zero   Errors
     17          Zero   Congruent        2  Errors   0.023   0.014   0.033          Zero   Errors
     18          Zero   Congruent        3  Errors   0.027   0.018   0.036          Zero   Errors
     19          Zero Incongruent        1  Errors   0.034   0.024   0.044          Zero   Errors
     20          Zero Incongruent        2  Errors   0.036   0.024   0.048          Zero   Errors
     21          Zero Incongruent        3  Errors   0.024   0.013   0.035          Zero   Errors
     22 All different     Neutral        0  Errors   0.019   0.013   0.026 All different   Errors
     23 All different   Congruent        1  Errors   0.028   0.017   0.038 All different   Errors
     24 All different   Congruent        2  Errors   0.021   0.011   0.032 All different   Errors
     25 All different   Congruent        3  Errors   0.031   0.022   0.041 All different   Errors
     26 All different Incongruent        1  Errors   0.024   0.010   0.037 All different   Errors
     27 All different Incongruent        2  Errors   0.020   0.011   0.029 All different   Errors
     28 All different Incongruent        3  Errors   0.019   0.010   0.028 All different   Errors

我想为RT和错误制作一个折线图(我的DV。因为我想将它们放在同一个图形空间中,我使用face_grid:

ggplot(matriz, aes(Distance, Score, shape= congruency, linetype=congruency)) + 
    geom_point(size=5) + 
    geom_line(size=1) + 
    geom_errorbar(aes(ymax = limsup, ymin= liminf), width=0.25, linetype=1)  + 
    facet_grid(MeasureF~ConditionF,scales= "free")

问题是我想自定义每个y轴。具体来说,我想为此轴设置限制。如果我有一个简单的图形(我只需要包含指令coord_cartesian()),这很简单,但是当我使用facet_grid()时,我不知道如何做到这一点。

1 个答案:

答案 0 :(得分:1)

如果要在ggplot2中扩展具有分面的绘图的y限制,可以执行以下方法:

library(ggplot2)
library(reshape2)
set.seed(1)
N <- 24

dates = seq(as.Date("2014-01-01"), as.Date("2015-12-01"), by = "1 month")
dummy_data <- data.frame(dates = dates, x = rnorm(N,0,1), group = sample(c("A", "B", "C"), size = N, replace = TRUE))
dummy_data_m  <- melt(dummy_data, id.vars = c("dates", "group"))

plot = ggplot(data = dummy_data_m, aes(x = dates, y = value, colour = variable)) + geom_point() + facet_grid(group ~., scales="free_y")

print(plot) # Want to modify y-axis on this plot for each panel


# to expand plot axes, build the y ranges in a data.frame and add a new layer to the plot using geom_blank

# pick an x.value in the range of your x-values in dummy_data
x.value = as.Date("2014-01-01")
x.value = as.Date("2014-01-01")

# Say you want to expand the y-axis ranges for the different subpanels to be (-5, 5), (-4, 4), (-2, 2).  
# If you simply plot at this point the y limits are roughly ~(-1.5, 1.5) for each plot
lower_y = data.frame(dates = x.value, group = c("A", "B", "C"),  value = c(5, 4, 2))

y_ranges = rbind(lower_y, upper_y)
y_ranges_m = melt(y_ranges, id.vars = c("dates", "group"))
plot = plot + geom_blank(data = y_ranges_m, aes(x = dates, y = value, colour = variable))

print(plot)

对于相反的情况,如果要减小y范围,则从要缩小y轴的面板中的数据中删除观察值。例如(而不是如上所述扩展y轴):

# do not want to plot any values where y < 0 for group A

dummy_data2 = dummy_data

x_adjusted = dummy_data2$x

x_adjusted[x_adjusted < 0 & dummy_data2$group == "A"] = NA

dummy_data2$x = x_adjusted

dummy_data_m2  <- melt(dummy_data2, id.vars = c("dates", "group"))

plot = ggplot(data = dummy_data_m2, aes(x = dates, y = value, colour = variable)) + geom_point() + facet_grid(group ~., scales="free_y")
print(plot) # this will show a plot where the panel for A has a y-axis lower limit above 0