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

标签 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

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

如何做到这一点:在“register”因素的每个级别内,我想绘制三个点分别显示三个音节“a2”、“i2”和“u2”的平均点,并将这三个连接起来用三角形点。所以三个寄存器应该分别有三个三角形。像这样:

enter image description here

最佳答案

我首先使用 dplyr 计算平均值。该图包含一层显示原始数据。 “平均三角形”的角由 geom_point() 标记(尺寸比法线点大)。三角形本身由 geom_polygon 绘制。

如果有人对此有其他解决方案,我想看看(赞成 :))。

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

关于r - 在ggplot2中,如何绘制散点的平均点并连接这些平均点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37907595/

相关文章:

r - 如何根据变量数量(var1、var2 等)自动调整 R 脚本

r - 使用 dplyr、自定义函数或 purr 的多个条件 if-else

r - 如何对矩阵的所有对角线求和?

r - 如何防止 grid.arrange 在 R 中的 4 个面板图上切断 x 轴标签?

r - 使用 R 和 ggplot2 时连续缩放的错误

r - ggplot2 标题中的多行 `expression()`

r - 为什么在 dplyr 中使用 list() 对 .dots = setNames() 使用至关重要?

r - 使用并行时如何 `print`或 `cat`

r - `data` 必须是一个数据框,或者其他被 `fortify()` 强制的对象,而不是一个带有 ranger 类的 S3 对象

r - 如何在 ggplot 中添加注释