上Y轴)设置的限度使用facet_grid两个因变量((Set limits on y axis f

2019-10-19 02:21发布

我有以下的数据帧

            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

我想为RTS和错误(我的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()

Answer 1:

如果你想扩大情节的y限制在小面GGPLOT2,你可以做以下的方法:

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


文章来源: Set limits on y axis for two dependent variables using facet_grid()
标签: r ggplot2