使用facet_grid()为两个因变量设置y轴上的限制 [英] Set limits on y axis for two dependent variables using facet_grid()
问题描述
我有以下数据框:
pre $ 条件一致性距离度量值得分liminf limsup ConditionF MeasureF
1零中性0 RTs 445.000 435.000 455.000零RTs
2零全等1 RT 445.000 435.000 456.000零RTs
3零全等2 RT 441.000 430.000 451.000零RTs
4零全等3 RT 432.000 422.000 442.000零RTs
5零不一致1 RT 449.000 439.000 459.000零RTs
6零不一致2 RT 449.000 438.000 460.000零RTs
7零不一致3 RTs 453.000 440.000 465.000零RTs
8所有不同的中性0 RT 446.000 436.000 456.000所有不同的RTs
9全部不同的一次RT 1 445.000 434.000 455.000所有不同的RT
10所有不同的同余2 RT 449.000 438.000 461.000所有不同的RT
11所有不同的同余3 RT 449.000 438.000 461.000所有不同的RT
12所有不同的不一致1 RTs 446.000 436.000 456.000所有不同的RTs
13所有不同的Incongruent 2 RT 447.000 436.000 458.000所有不同的RTs
14所有不同的不一致的RT 3 450.000 440.000 461.000所有不同的RT
15零中性0错误0.029 0.018 0.039零错误
16零全等1错误0.023 0.015 0.031零错误
17零全等2错误0.023 0.014 0.033零错误
18零全等3错误0.027 0.018 0.036 Z ero错误
19零不一致1错误0.034 0.024 0.044零错误
20零不一致2错误0.036 0.024 0.048零错误
21零不一致3错误0.024 0.013 0.035零错误
22全部不同的0中性0错误0.019 0.013 0.026所有不同的错误
23所有不同的一致性1错误0.028 0.017 0.038所有不同的错误
24所有不同的一致性2错误0.021 0.011 0.032所有不同的错误
25所有不同的一致性3错误0.031 0.022 0.041所有不同的错误
26所有不同的错误1错误0.024 0.010 0.037所有不同的错误
27所有不同的错误2错误0.020 0.011 0.029所有不同的错误
28所有不同的错误3错误0。 019 0.010 0.028所有不同的错误
我想为RT和错误(我的DV)制作折线图。因为我想把它们放在同一个图表空间中,所以我使用了face_grid:
$ g $ p $ ggplot(matriz,aes(距离,得分,形状=一致性,线型=一致性))+
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()
。
如果要在ggplot2中使用faceting来扩展plot的y-limits,可以采用以下方法: / b>
library(ggplot2)
library(reshape2)
set.seed(1)
N < - 24
日期= seq(as.Date(2014-01-01),as.Date(2015-12-01),by =1个月)$ b $ dummy_data dummy_data_m < - melt(dummy_data,id.vars = c(dates,group))
plot = ggplot(data = dummy_data_m,aes(x = date,y = value,color = variable))+ geom_point()+ facet_grid(group〜。,scales =free_y)
print(plot)#想要修改此轴绘制每个面板
#以扩展绘图轴,在data.frame中构建y范围并使用geom_blank添加新图层
#在dummy_data的x值范围内选择x.value
x.value = as.Date(2014-01-01 )
x.value = as.Date(2014-01-01)
#假设您想扩展不同子面板的y轴范围为(-5, 5),(-4,4),( - 2,2)。
#如果你简单地在这一点上绘制,那么对于每个绘图
,y限制大致为〜(-1.5,1.5)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(日期,组))
plot = plot + geom_blank(data = y_ranges_m,aes(x = dates,y = value,color = variable))
print (plot)
对于相反的情况,如果您想减小y范围,则从您想要缩小y轴的面板中的数据。例如(而不是像上面那样展开y轴):
#不想绘制任何值,其中y < 0对于组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,color = variable))+ geom_point()+ facet_grid(group〜。,scales = free_y)
print(plot)#这将显示一个情节,其中A的面板的y轴下限高于0
I have the following dataframe
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
I want to make a line graph for RTs and Errors (my DVs. Because I want to put them together in the same graph space, I am using face_grid for this:
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")
the thing is that I would like to customize each y axis. Specifically, I want to set limits for this axis. This is quite simple if I had a simple graph (I would only need to include the instruction coord_cartesian()
), but I don't have idea of how to do it when I use facet_grid()
.
If you want to expand the y-limits for a plot with faceting in ggplot2, you could do the following approach:
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)
For the opposite case, if you want to decrease the y range, then remove observations from the data in the panels for which you want to shrink the y-axis. For example (instead of expanding the y-axis as above):
# 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
这篇关于使用facet_grid()为两个因变量设置y轴上的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!