2015-05-20 121 views
4

我想連接在每個層面上的(兩個)點在每個層面上的集合。這裏是一個重複的例子:繪製線段連接點集

datum <- structure(list(frequency = c(8L, 7L, 6L, 18L, 5L, 11L, 16L, 15L, 
9L, 8L, 8L, 10L, 2L, 20L, 14L, 3L, 6L, 2L, 2L, 11L, 10L, 6L, 
15L, 19L, 18L, 18L, 8L, 2L, 10L, 15L, 12L, 17L, 1L, 18L, 7L, 
8L, 16L, 4L, 9L, 2L, 7L, 3L, 16L, 7L, 18L, 20L, 9L, 10L, 13L, 
2L, 15L, 7L, 3L, 20L, 4L, 15L, 5L, 7L, 9L, 16L, 5L, 8L, 10L, 
10L, 7L, 10L, 10L, 17L, 7L, 8L, 13L, 13L, 16L, 5L, 20L, 18L, 
13L, 19L, 3L, 8L, 14L, 12L, 20L, 2L, 9L, 13L, 7L, 2L, 5L, 5L, 
13L, 9L, 13L, 7L, 9L, 4L, 4L, 20L, 1L, 4L), band = structure(c(2L, 
4L, 2L, 3L, 2L, 1L, 4L, 1L, 2L, 1L, 3L, 4L, 2L, 4L, 3L, 4L, 3L, 
2L, 3L, 2L, 2L, 4L, 2L, 1L, 1L, 2L, 1L, 4L, 4L, 1L, 4L, 4L, 2L, 
1L, 4L, 4L, 3L, 4L, 1L, 1L, 3L, 4L, 1L, 3L, 4L, 1L, 2L, 1L, 1L, 
2L, 2L, 1L, 3L, 4L, 2L, 1L, 2L, 4L, 2L, 2L, 4L, 4L, 2L, 4L, 4L, 
1L, 1L, 4L, 2L, 3L, 4L, 1L, 2L, 4L, 1L, 2L, 4L, 1L, 1L, 3L, 4L, 
4L, 2L, 2L, 2L, 1L, 3L, 2L, 2L, 2L, 3L, 3L, 1L, 3L, 4L, 3L, 3L, 
1L, 3L, 4L), .Label = c("1", "2", "3", "4"), class = "factor"), 
test = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 
2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 
1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 
2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 
2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L 
), .Label = c("1", "2"), class = "factor"), knowledge = structure(c(2L, 
3L, 1L, 3L, 1L, 1L, 3L, 3L, 1L, 3L, 1L, 3L, 2L, 2L, 1L, 1L, 
1L, 1L, 3L, 3L, 1L, 2L, 3L, 1L, 1L, 2L, 2L, 1L, 1L, 3L, 2L, 
3L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 3L, 3L, 1L, 1L, 2L, 3L, 
3L, 2L, 2L, 3L, 1L, 1L, 2L, 2L, 2L, 3L, 1L, 3L, 1L, 1L, 2L, 
1L, 1L, 2L, 3L, 1L, 1L, 1L, 1L, 3L, 2L, 2L, 1L, 2L, 3L, 2L, 
1L, 2L, 3L, 3L, 2L, 1L, 3L, 1L, 3L, 2L, 1L, 3L, 2L, 2L, 3L, 
1L, 1L, 2L, 1L, 2L, 3L, 1L, 3L, 1L), .Label = c("1", "2", 
"3"), class = "factor")), .Names = c("frequency", "band", 
"test", "knowledge"), row.names = c(NA, -100L), class = "data.frame") 

這裏是代碼我到目前爲止:

ggplot(datum, aes(knowledge, frequency, color=test)) + 
stat_summary(fun.y='mean', geom='point', position=position_dodge(width=.9), size=3) + 
facet_grid(~band) + 
labs(y='number of words (max = 20)', x='self-report knowledge') + 
scale_x_discrete(labels=c('none', 'form', 'meaning')) 

看着圖中的最左邊的小(「1」),我想一個線在none列中連接預測試和posttest,在表單列中連接pretest和posttest的另一行,以及在意義列中連接pretest和posttest的行。我希望在每個方面都做到這一點。

我希望有道理,謝謝!

回答

1

我發現過於依賴ggplot進行數據處理/彙總會傷害更多而不是幫助。我不知道如何將位置躲避點與線連接起來。相反,我會這樣做:

library(dplyr) 
datsum = datum %>% 
    group_by(band, knowledge, test) %>% 
    summarize(mean = mean(frequency)) %>% 
    ungroup %>% 
    mutate(knowledge_fac = factor(knowledge, labels = c('none', 'form', 'meaning'))) 

ggplot(datsum, aes(x = test, y = mean)) + 
    geom_path(aes(group = band:knowledge)) + 
    geom_point(aes(color = factor(test))) + 
    facet_grid(band ~ knowledge_fac) + 
    labs(y='number of words (max = 20)', x='self-report knowledge') 
+0

非常感謝您的幫助。我在想,我不應該像現在那樣依賴ggplot,所以謝謝你的確認。即使將位置躲避點與線連接的問題沒有解決,我仍會將其標記爲已解決。 – Tjuggles

+0

@Tjuggles謝謝。是的,如果你想學習如何做到這一點,你應該問一個問題,那就是標題中的問題,對於這個問題來說它是最小的(簡單的模擬數據,沒有刻面或其他併發症)。 – Gregor

1

從Gregor的工作中獲取數據,我認爲這是做了什麼要求。 mutate()塊將Test創建爲測試1的-0.1和測試2的0.1的數字偏移量。然後將其添加到知識的數值中。結果是傳遞給ggplot2的數字x。格雷戈爾正確定義了這些組,所以剩下的就很簡單。

library(dplyr) 
datsum <- datum %>% 
    group_by(band, knowledge, test) %>% 
    summarize(mean = mean(frequency)) %>% 
    mutate(Test = 0.1 * (2 * (test == 2) - 1), 
     Knowledge = as.numeric(knowledge) + Test) %>% 
    ungroup 

ggplot(datsum, aes(x = Knowledge, y = mean, color = test)) + 
    geom_path(aes(group = band:knowledge), color = "black") + 
    geom_point(size = 3) + 
    facet_wrap(~ band, nrow = 1) + 
    labs(y='number of words (max = 20)', x='self-report knowledge') + 
    scale_color_manual(values = c("orange", "blue")) + 
    scale_x_continuous(limits = c(0.5, 3.5), breaks = 1:3, 
        labels = c("none", "form", "meaning"))