Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在ggplot2中注释刻面打印_R_Ggplot2_Label_Facet - Fatal编程技术网

R 在ggplot2中注释刻面打印

R 在ggplot2中注释刻面打印,r,ggplot2,label,facet,R,Ggplot2,Label,Facet,我正在处理以下pre.sss报告的数据集 pre.sss <- pre.sss <- structure(list(Pretest.num = c(63, 62, 61, 60, 59, 58, 57, 4,2, 1), stress = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L,1L), .Label = c("[0,6]", "(6,9]"), class = "factor"), time = c(1L,1L, 1L, 1L,

我正在处理以下pre.sss报告的数据集

pre.sss <- pre.sss <- structure(list(Pretest.num = c(63, 62, 61, 60, 59, 58, 57, 4,2, 1), stress = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L,1L), .Label = c("[0,6]", "(6,9]"), class = "factor"), time = c(1L,1L, 1L, 1L, 1L, 1L, 1L, 8L, 8L, 8L), after = structure(c(2L,2L, 2L, 2L, 2L, 2L, 1L, 1L, NA, 1L), .Label = c("no", "yes"), class = "factor"),id = c("call_fam", "call_fam", "call_fam", "call_fam", "call_fam","call_fam", "call_fam", "counselor", "counselor", "counselor")), .Names = c("Pretest.num", "stress", "time", "after","id"), reshapeLong = structure(list(varying = structure(list(after = c("after.call.fam", "after.speak", "after.send.email","after.send.card", "after.attend", "after.fam.mtg", "after.sup.grp","after.counselor")), .Names = "after", v.names = "after", times = 1:8),v.names = "after", idvar = "Pretest.num", timevar = "time"), .Names = c("varying","v.names", "idvar", "timevar")), row.names = c("63.1", "62.1","61.1", "60.1", "59.1", "58.1", "57.1", "4.8", "2.8", "1.8"), class = "data.frame")
我得到了下图,完全符合我的要求

现在,我想用相应数据出现在数据集中的计数来标记每个气泡

dat = data.frame(ggplot_build(p)$data[[1]][, c('x', 'y', 'PANEL', 'n')])
dat$PANEL = ifelse(dat$PANEL==1, "[0,6]", "(6-9]")
colnames(dat) = c('x', 'y', 'stress', 'n')

p + geom_text(aes(x, y, label = n, group = NULL), data = dat)
这给了我以下的错误,我真的无法理解

> p +  geom_text(aes(x, y, label=n, group=NULL), data=dat)
Error in `$<-.data.frame`(`*tmp*`, "PANEL", value = c(1L, 1L, 1L, 1L,  : 
  replacement has 504 rows, data has 46
有人能帮我吗

谢谢


EM

此示例中仍然缺少您称为labeller函数的函数。geom_count使用stat_sum,它计算一个参数n,即该点的观测数。因为可以使用此计算参数,所以实际上不必像使用ggplot_build那样将绘图指定给变量并提取其数据

这应该满足您的需求:

ggplot(pre.sss, aes(x = after, y = id)) + 
    geom_count(alpha = 0.5, col = 'darkblue') + 
# note the following line
    stat_sum(mapping = aes(label = ..n..), geom = "text") +
    scale_size(range = c(1,30)) +
    theme(legend.position = 'none') +
    xlab("Response") + 
    ylab("What did you do after learning about death?") + 
    scale_y_discrete(labels = ylabels) +
    facet_grid(.~ stress)

我添加的行计算的内容与geom_count中的幕后内容相同,但却给它一个文本geom,标签映射到该计算参数n。

如果没有数据样本,我无法复制它。但是猜测一下,您正在初始ggplot和geom_文本调用中定义数据x和y。您可以尝试ggplot+geom\u countdata=pre.sss,aesx=after,y=id+geom\u textdata=pre.sss,aesx=after,y=id,label=n,group=null请不要使用dropbox共享您的数据。不是每个人都会从外部链接下载一些东西到自己的计算机上。最好将dputpre.sss的结果包含在您的文档中。这给了我一个我真的无法理解的错误。我们也不能,因为你没有发。没有一些数据我无法重新创建。谢谢,并为这篇糟糕的帖子道歉。我附上了一个更有希望重现的例子。
ggplot(pre.sss, aes(x = after, y = id)) + 
    geom_count(alpha = 0.5, col = 'darkblue') + 
# note the following line
    stat_sum(mapping = aes(label = ..n..), geom = "text") +
    scale_size(range = c(1,30)) +
    theme(legend.position = 'none') +
    xlab("Response") + 
    ylab("What did you do after learning about death?") + 
    scale_y_discrete(labels = ylabels) +
    facet_grid(.~ stress)