将文本添加到构面网格

时间:2017-05-24 20:07:00

标签: r

我直到遇到这个问题,所以我编辑了我的问题(下面)以包含我的数据。

我想为网格的每个部分添加两个不同的标签(相应的样本大小)。

我知道我需要使用geom_text,但我似乎无法让它工作。我尝试过的所有内容都返回了错误。这是我到目前为止制作图表的代码:

ggplot(data = Q, mapping = aes(y = Rating, x = week, group= StudentFactor, colour=StudentFactor))+ 
  geom_point()+
  geom_line()+
  facet_grid(Type ~.)+
  geom_smooth(method = 'lm', formula = y ~ poly(x), colour= "black", aes(group=1), se= FALSE)+
  theme(legend.position="none") +
  labs (x= "Week", y="Rating of Q: Seniors Supportive vs. Supporting First Years")

有人提到如果我包含以下内容可能会有所帮助:

> dput(Q)
structure(list(StudentFactor = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 6L, 6L, 6L, 
6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 
9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 
14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 
16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L, 18L, 18L, 18L, 18L, 18L, 
18L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 
20L, 20L, 20L, 20L, 20L, 20L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 
21L, 21L, 21L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 23L, 23L, 
23L, 23L, 23L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 25L, 26L, 
26L, 26L, 26L, 26L, 27L, 27L, 27L, 28L, 28L, 29L, 29L, 29L, 29L, 
29L, 29L, 29L, 29L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 
30L, 31L, 31L, 31L, 32L, 32L, 32L, 32L, 32L, 32L, 33L, 33L, 33L, 
33L, 33L, 33L, 33L, 33L, 34L, 34L, 34L, 34L, 35L, 35L, 35L, 35L, 
35L, 35L, 35L, 35L, 35L, 35L, 36L, 36L, 36L), .Label = c("789331", 
"796882", "805933", "826523", "827911", "830271", "831487", "832929", 
"834598", "836364", "838607", "839802", "841903", "843618", "852125", 
"855524", "873527", "876406", "879972", "885409", "885650", "888712", 
"894218", "903303", "928026", "952797", "955389", "956952", "957206", 
"957759", "959200", "962490", "965873", "967416", "968728", "969005"
), class = "factor"), Question = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1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 7L, 8L, 9L, 10L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 
    3L, 4L, 7L, 8L, 9L, 10L, 3L, 1L, 5L, 6L, 7L, 8L, 3L, 8L, 
    9L, 7L, 8L, 1L, 2L, 3L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 
    4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 6L, 3L, 4L, 5L, 6L, 
    9L, 10L, 1L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 6L, 7L, 8L, 
    1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 3L, 4L, 8L), Rating = c(0, 
    0, 0, 1, -2, 1, 1, -1, 0, 1, 0, 0, 0, 2, -1, -3, 2, 0, 0, 
    -1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 1, 0, 0, -1, 1, 0, 1, 0, 
    0, 0, 0, 1, -1, 1, 0, 0, 0, 0, 0, 0, -2, -1, 0, 0, 0, 0, 
    0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -2, 
    0, 4, -3, 1, 1, -1, -2, 0, 2, 0, -1, 1, 0, 0, 1, 0, 0, 0, 
    0, 0, -1, 1, 0, 0, 0, -1, 0, 1, -1, 0, 0, 0, 1, -1, 1, -1, 
    1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, -1, 1, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, -1, 1, 0, 0, 1, 0, 0, 0, 
    0, 0, 0, 0, 1, -1, 1, 0, 0, 1, 0, 0, 0, 0, -2, 2, 0, 0, 0, 
    0, 0, 1, 0, 1, -1, 0, 1, -1, 0, 1, 2, -1, 0, 1, 0, 1, -1, 
    1, 0, -1, 0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 0, 1, -1, 1, -1, 
    1, -1, -3, 3, 0, 0, -1, 0, -1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 
    -1, 1, -1, 0, -2, 2, -1, -1, 1, 0, 0)), class = "data.frame", row.names = c(NA, 
-231L), .Names = c("StudentFactor", "Question", "Type", "week", 
"Rating"))

早期问题:

我使用以下代码制作此图表:

enter image description here

ggplot(data = Q, mapping = aes(y = Rating, x = week, group= StudentFactor, colour=StudentFactor))+ 
  geom_point()+
  geom_line()+
  facet_grid(Q$Type ~.)+
  geom_smooth(method = "lm", se=FALSE, color="black", aes(group=1))+
  theme(legend.position="none") +
  labs (x= "Week", y="Rating of Q: Seniors Supportive vs. Supporting First Years")

但是,只要我尝试添加注释以包含每个群组的样本大小,我就会收到以下错误:

Error in `$<-.data.frame`(`*tmp*`, "PANEL", value = c(2L, 2L, 2L, 2L,  : 
  replacement has 231 rows, data has 1

这是我尝试使用的代码:

ggplot(data = Q, mapping = aes(y = Rating, x = week, group= StudentFactor, colour=StudentFactor))+ 
  geom_point()+
  geom_line()+
  facet_grid(Q$Type ~.)+
  geom_smooth(method = "lm", se=FALSE, color="black", aes(group=1))+
  theme(legend.position="none") +
  labs (x= "Week", y="Rating of Q: Seniors Supportive vs. Supporting First Years")+
  annotate("text", x = 1, y=4, label = "N=")

我是R的初学者。任何帮助都将不胜感激!提前谢谢!

2 个答案:

答案 0 :(得分:2)

我想,您希望使用相应的样本大小标记每个面板,或者只在一个位置添加总样本大小。在这种情况下,annotate将毫无用处(例如参见this question),但您可以改为使用geom_text

您可以为标签创建单独的data.frame,然后在geom_text内使用

library(ggplot2)

labels <- tibble::tribble(~mpg, ~hp, ~cyl, ~label,
                          15, 200, 4, "label 1",
                          15, 150, 6, "label 2")

ggplot(mtcars, aes(mpg, hp)) +
  geom_point() +
  facet_wrap(~cyl) +
  geom_text(data = labels, aes(label = label))

(由于您没有提供可以重现图表的数据,我只是使用mtcars来说明问题。)

编辑:

使用您的数据我调整了代码。首先,为标签创建data.frame

labels <- data.frame(
  x = 2.5,
  y = 3,
  Type = "FYS",
  label = "mylabel"
)

然后,您只需在代码中添加以下行:

... +
  geom_text(data = labels, aes(x = x, y = y, label = label), inherit.aes = FALSE)

这种方法与我上面显示的方法略有不同:要么根据变量提供坐标,要么在图的其余部分使用,要么给它们新名称,使用inherit.aes = FALSE并设置与您的数据一起展示aes的新变量。

完整代码:

labels <- data.frame(
  x = 2.5,
  y = 3,
  Type = "FYS",
  label = "mylabel"
)

ggplot(data = Q, mapping = aes(y = Rating, x = week, group= StudentFactor, colour=StudentFactor))+ 
  geom_point()+
  geom_line()+
  facet_grid(Type ~ .)+
  geom_smooth(method = 'lm', formula = y ~ poly(x), colour= "black", aes(group=1), se= FALSE)+
  theme(legend.position="none") +
  labs (x= "Week", y="Rating of Q: Seniors Supportive vs. Supporting First Years") +
  geom_text(data = labels, aes(x = x, y = y, label = label), inherit.aes = FALSE)

答案 1 :(得分:1)

您的错误是由于错误地使用facet_grid造成的。将您的行替换为facet_grid(Type ~.)+而不是Q$Type。例如:

ggplot(iris, aes(Sepal.Length, Sepal.Width)) + 
  geom_point() + 
  facet_grid(iris$Species ~ .) + 
  annotate("text", x = 7, y = 4, label = "foo")
  

Error in '$<-.data.frame'('*tmp*', "PANEL", value = c(1L, 1L, 1L, 1L, : replacement has 150 rows, data has 1

ggplot(iris, aes(Sepal.Length, Sepal.Width)) + 
  geom_point() + 
  facet_grid(Species ~ .) + 
  annotate("text", x = 7, y = 4, label = "foo")

没有错误: enter image description here