用ggplot在R中创造刺激

用ggplot在R中创造刺激,r,ggplot2,graph,R,Ggplot2,Graph,我正在尝试使用R为一个实验生成我自己的刺激。下面的代码使用rnorm()创建我的(x,y)坐标,不同的样本大小为100,不同的平均值和sd。我还创建了另一个变量来表示由runif()确定的圆的大小 dt首先,问题的数据创建代码可以大大简化,完全不需要循环就可以重写。R是一种矢量化语言,下面将创建具有相同结构的数据帧 不要忘记设置RNG种子,以使结果可重复 library(ggplot2) set.seed(2020) # make the results reproducible sd

我正在尝试使用R为一个实验生成我自己的刺激。下面的代码使用rnorm()创建我的(x,y)坐标,不同的样本大小为100,不同的平均值和sd。我还创建了另一个变量来表示由runif()确定的圆的大小


dt首先,问题的数据创建代码可以大大简化,完全不需要循环就可以重写。R是一种矢量化语言,下面将创建具有相同结构的数据帧

不要忘记设置RNG种子,以使结果可重复

library(ggplot2)

set.seed(2020)    # make the results reproducible

sd <- rep(rep(mySD, each = 2), 10)
M <- rep(myMeans, 2*10)
x <- abs(rnorm(n = 40, mean = M, sd = sd))
y <- abs(rnorm(n = 40, mean = M, sd = sd))
size <- runif(40, 1, 25)
iter <- seq_along(x)
dt2 <- data.frame(x, y, size, M, sd, iter)
dt2$col <- c("blue", "red")
要为
n
绘制多个值,请使用
lappy
生成绘图,然后使用
grid。从package
gridExtra
排列

plot_list <- lapply(c(4,8,16,32), function(n) plot_fun(dt2, n))
gridExtra::grid.arrange(grobs = plot_list)
等等


另一种方法是使用镶嵌面。编写另一个函数,
plot\u fun\u facets
将点数分配给样本数据帧中的新变量,
n
,并将该变量用作刻面变量

plot_fun_facets <- function(X, n){
  Colors <- unique(X[["col"]])
  Colors <- setNames(Colors, Colors)
  X_list <- lapply(n, function(.n){
    i <- sample(nrow(X), .n)
    Y <- X[i,]
    Y$n <- .n
    Y
  })
  X <- do.call(rbind, X_list)
  g <- ggplot(X, aes(x, y, size = size, color = col)) +
    geom_point() +
    scale_color_manual(values = Colors) +
    facet_wrap(~ n) +
    theme_bw()
  g
}

plot_fun_facets(dt2, c(4,8,16,32))

plot\u fun\u facets这里有两种方法-取决于您是否希望每个组不包含任何其他组的点

我将使用一个虚拟数据帧,它只包含列
x
y
size

plot_fun <- function(X, n){
  Colors <- unique(X[["col"]])
  Colors <- setNames(Colors, Colors)
  i <- sample(nrow(X), n)
  g <- ggplot(X[i,], aes(x, y, size = size, color = col)) +
    geom_point() +
    scale_color_manual(values = Colors) +
    theme_bw()
  g
}

plot_fun(dt2, 8)
library(tidyverse)

dt <- tibble(x = runif(100), y = runif(100), size = runif(100))
使用面绘制

ggplot(sampled, aes(x, y, size = size)) +
  geom_point() +
  facet_wrap(~group)

要求小组有不同的观点 首先,我们需要一种生成四个
1
s、八个
2
s等的方法。这可以使用
log2
和一些技巧来完成

groups <- floor(log2(seq_len(nrow(dt)) + 3)) - 1
groups
#>  [1] 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4
#> [36] 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5
#> [71] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5

为什么
n=100
位于
x=abs(rnorm(n=100,mean=m,sd=s))
如果您在
dt$x[sa]中一次只分配一个值,谢谢您的回复。我以为rnorm()从指定的样本大小中选择了一个随机值。我想这只是runif()。我现在就要做出改变,但这仍然无法达到我最终要做的。你希望每个小组都不包含任何其他小组的分数吗?谢谢你的提问,保罗。是的,我希望它们对每个组都是唯一的(试验)。这是否意味着我应该使用:x=abs(unique(rnorm(n=1,mean=m,sd=s)))谢谢你的时间和非常有用的回复,鲁伊。我想我已经把你所做的大部分都解释清楚了。这将生成1个图形(试用)。最好将其放入for循环(我知道R讨厌for循环…)中,这样我就可以生成具有不同数据点数的多个图(试验)(n您可以将其放入
lappy
循环中,让我编辑问题。
sample_sizes <- 2^(seq_len(4) + 1)
sample_sizes
#> [1]  4  8 16 32
sampled <- map_dfr(
  sample_sizes,
  ~sample_n(dt, .),
  .id = "group"
)
ggplot(sampled, aes(x, y, size = size)) +
  geom_point() +
  facet_wrap(~group)
groups <- floor(log2(seq_len(nrow(dt)) + 3)) - 1
groups
#>  [1] 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4
#> [36] 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5
#> [71] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
dt$group <- sample(groups)
ggplot(dt, aes(x, y, size = size)) +
  geom_point() +
  facet_wrap(~group)