在ggplot2中,如何绘制散点的平均点并连接这些平均点?

时间:2016-06-19 13:10:26

标签: r ggplot2

require(ggplot2)
F1 <- c(915.6425,776.2108,786.5994,656.7274,790.5350,949.0578,1053.3216,971.0848,863.6778,738.1062,
    884.1085,904.4734,872.4323,749.6835,736.6229,773.6344,816.7553,858.8569,853.3249,891.7048,
    850.5705,754.4007,354.7462,343.2167,337.7637,330.5004,309.0369,318.4770,335.2704,346.9049,
    422.9287,385.5191,410.3909,416.1298,423.8248,353.9624,264.2674,199.2254,342.5222,319.3892,
    325.5104,293.2321,289.0946,294.4882,307.3506,295.4908,344.1240,326.8981,326.8264,356.7491,
    362.6374,459.3103,431.8374,436.6566,433.1298,430.9657,419.5703,494.0244,370.0444,439.8624,
    296.8736,546.3018,311.0276,330.3982,346.5127,292.0111,392.9396,336.4151,310.8202)

F2 <- c (1708.907,1703.188,1763.067,1757.835,1768.614,1651.012,1550.874,1593.289,1641.620,1238.515,1674.767,
     1679.122,1638.291,1213.908,1793.527,1948.038,1782.379,1665.103,1646.012,1412.545,1746.250,1924.981,
     2715.633,2593.596,2657.560,2609.197,2553.094,2712.059,2688.420,2668.596,2574.913,2576.952,2615.174,
     2570.738,2520.431,2635.001,2726.716,2491.044,2638.541,2741.096,2708.415,2661.317,2725.348,2684.339,
     2644.370,2724.902,2574.692,1092.502,1154.349,1698.169,1753.618,1138.324,1200.801,1592.622,1788.171,
     1668.637,1442.751,1587.081,1871.528,1783.440,1429.906,1606.038,1318.925,1623.297,1452.331,1822.482,
     1972.793,2017.814,1291.637)

register<-c("ADS","ADS","ADS","ADS","ADS","ADS","ClearSpeech","ClearSpeech","ClearSpeech","ClearSpeech","ClearSpeech","ClearSpeech","ClearSpeech","IDS","IDS","IDS","IDS","IDS","IDS","IDS","IDS","IDS","ADS","ADS","ADS","ADS","ADS","ADS","ADS","ClearSpeech","ClearSpeech","ClearSpeech","ClearSpeech","ClearSpeech","ClearSpeech","ClearSpeech","IDS","IDS","IDS","IDS","IDS","IDS","IDS","IDS","IDS","IDS","IDS","ADS","ADS","ADS","ADS","ClearSpeech","ClearSpeech","ClearSpeech","ClearSpeech","ClearSpeech","ClearSpeech","ClearSpeech","ClearSpeech","ClearSpeech","IDS","IDS","IDS","IDS","IDS","IDS","IDS","IDS","IDS")


syllable <- c ("a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2")

data2 <- cbind(syllable, register, F1, F2)
data2 <- as.data.frame(data2)


p<-ggplot (data2, aes (x = F1, y = F2, shape = syllable, color = register))+
   geom_point(aes (color = register))

p

这是我的数据。这些点按两个因素分类:音节和寄存器。

如何做到这一点:在“寄存器”因素的每个级别内,我想画三个点分别显示三个音节“a2”,“i2”和“u2”的平均点,并连接三个点三角形。因此,三个寄存器应分别有三个三角形。像这样:

enter image description here

1 个答案:

答案 0 :(得分:4)

我首先使用dplyr计算平均值。该图包含一个显示原始数据的图层。 &#34;平均三角形的角落&#34;由geom_point()标记(尺寸大于正常点)。三角形本身由geom_polygon绘制。

如果某人有另一个解决方案,我希望看到(upvote :))它。

library(ggplot2)
library(dplyr)

F1 <- c(915.6425,776.2108,786.5994,656.7274,790.5350,949.0578,1053.3216,971.0848,863.6778,738.1062,
        884.1085,904.4734,872.4323,749.6835,736.6229,773.6344,816.7553,858.8569,853.3249,891.7048,
        850.5705,754.4007,354.7462,343.2167,337.7637,330.5004,309.0369,318.4770,335.2704,346.9049,
        422.9287,385.5191,410.3909,416.1298,423.8248,353.9624,264.2674,199.2254,342.5222,319.3892,
        325.5104,293.2321,289.0946,294.4882,307.3506,295.4908,344.1240,326.8981,326.8264,356.7491,
        362.6374,459.3103,431.8374,436.6566,433.1298,430.9657,419.5703,494.0244,370.0444,439.8624,
        296.8736,546.3018,311.0276,330.3982,346.5127,292.0111,392.9396,336.4151,310.8202)

F2 <- c (1708.907,1703.188,1763.067,1757.835,1768.614,1651.012,1550.874,1593.289,1641.620,1238.515,1674.767,
         1679.122,1638.291,1213.908,1793.527,1948.038,1782.379,1665.103,1646.012,1412.545,1746.250,1924.981,
         2715.633,2593.596,2657.560,2609.197,2553.094,2712.059,2688.420,2668.596,2574.913,2576.952,2615.174,
         2570.738,2520.431,2635.001,2726.716,2491.044,2638.541,2741.096,2708.415,2661.317,2725.348,2684.339,
         2644.370,2724.902,2574.692,1092.502,1154.349,1698.169,1753.618,1138.324,1200.801,1592.622,1788.171,
         1668.637,1442.751,1587.081,1871.528,1783.440,1429.906,1606.038,1318.925,1623.297,1452.331,1822.482,
         1972.793,2017.814,1291.637)

register<-c("ADS","ADS","ADS","ADS","ADS","ADS","ClearSpeech","ClearSpeech","ClearSpeech","ClearSpeech","ClearSpeech","ClearSpeech","ClearSpeech","IDS","IDS","IDS","IDS","IDS","IDS","IDS","IDS","IDS","ADS","ADS","ADS","ADS","ADS","ADS","ADS","ClearSpeech","ClearSpeech","ClearSpeech","ClearSpeech","ClearSpeech","ClearSpeech","ClearSpeech","IDS","IDS","IDS","IDS","IDS","IDS","IDS","IDS","IDS","IDS","IDS","ADS","ADS","ADS","ADS","ClearSpeech","ClearSpeech","ClearSpeech","ClearSpeech","ClearSpeech","ClearSpeech","ClearSpeech","ClearSpeech","ClearSpeech","IDS","IDS","IDS","IDS","IDS","IDS","IDS","IDS","IDS")
syllable <- c ("a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "a2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "i2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2", "u2")

data <- data.frame(syllable, register, F1, F2)

# calculate mean values using the dplyr package    
mean_values <- data %>% group_by(register, syllable) %>% summarise_each(funs(mean))

p <- ggplot() + 
  geom_point(aes(x = F1, y = F2, shape = syllable, color = register), data = data) +
  geom_point(aes(x = F1, y = F2, shape = syllable, color = register), size = 3, data = mean_values) +
  geom_polygon(aes(x = F1, y = F2, group = register, color = register), fill = NA, data = mean_values)

enter image description here