如何在R中合并/重叠使用两个不同数据帧构建的两个图?

时间:2020-05-26 09:01:31

标签: r ggplot2

这是我非常不专业的代码。有什么办法可以将两个图合并成一个图?

setwd("F:/Research/Biliteracy Experiment/CSVs/Stroop")
G1CongNep <- read.csv("Group 1 CongNep 101 Timesteps.csv")
G1IncongNep <- read.csv("Group 1 IncongNep 101 Timesteps.csv")


library(ggplot2)

#Literally all timesteps from G1CongNep
G1CongNepX1 <- mean(G1CongNep$X_1 , na.rm = T)
G1CongNepX2 <- mean(G1CongNep$X_2 , na.rm = T)
G1CongNepX3 <- mean(G1CongNep$X_3 , na.rm = T)
G1CongNepX4 <- mean(G1CongNep$X_4 , na.rm = T)
G1CongNepX5 <- mean(G1CongNep$X_5 , na.rm = T)
G1CongNepX6 <- mean(G1CongNep$X_6 , na.rm = T)
G1CongNepX7 <- mean(G1CongNep$X_7 , na.rm = T)
G1CongNepX8 <- mean(G1CongNep$X_8 , na.rm = T)
G1CongNepX9 <- mean(G1CongNep$X_9 , na.rm = T)
G1CongNepX10 <- mean(G1CongNep$X_10 , na.rm = T)
G1CongNepX11 <- mean(G1CongNep$X_11 , na.rm = T)
G1CongNepX12 <- mean(G1CongNep$X_12, na.rm = T)
G1CongNepX13 <- mean(G1CongNep$X_13 , na.rm = T)
G1CongNepX14 <- mean(G1CongNep$X_14 , na.rm = T)
G1CongNepX15 <- mean(G1CongNep$X_15 , na.rm = T)
G1CongNepX16 <- mean(G1CongNep$X_16 , na.rm = T)
G1CongNepX17 <- mean(G1CongNep$X_17 , na.rm = T)
G1CongNepX18 <- mean(G1CongNep$X_18 , na.rm = T)
G1CongNepX19 <- mean(G1CongNep$X_19 , na.rm = T)
G1CongNepX20 <- mean(G1CongNep$X_20 , na.rm = T)
G1CongNepX21 <- mean(G1CongNep$X_21 , na.rm = T)
G1CongNepX22 <- mean(G1CongNep$X_22 , na.rm = T)
G1CongNepX23 <- mean(G1CongNep$X_23 , na.rm = T)
G1CongNepX24 <- mean(G1CongNep$X_24 , na.rm = T)
G1CongNepX25 <- mean(G1CongNep$X_25 , na.rm = T)
G1CongNepX26 <- mean(G1CongNep$X_26 , na.rm = T)
G1CongNepX27 <- mean(G1CongNep$X_27 , na.rm = T)
G1CongNepX28 <- mean(G1CongNep$X_28 , na.rm = T)
G1CongNepX29 <- mean(G1CongNep$X_29 , na.rm = T)
G1CongNepX30 <- mean(G1CongNep$X_30 , na.rm = T)
G1CongNepX31 <- mean(G1CongNep$X_31 , na.rm = T)
G1CongNepX32 <- mean(G1CongNep$X_32 , na.rm = T)
G1CongNepX33 <- mean(G1CongNep$X_33 , na.rm = T)
G1CongNepX34<- mean(G1CongNep$X_34 , na.rm = T)
G1CongNepX35 <- mean(G1CongNep$X_35 , na.rm = T)
G1CongNepX36 <- mean(G1CongNep$X_36 , na.rm = T)
G1CongNepX37 <- mean(G1CongNep$X_37 , na.rm = T)
G1CongNepX38 <- mean(G1CongNep$X_38 , na.rm = T)
G1CongNepX39 <- mean(G1CongNep$X_39 , na.rm = T)
G1CongNepX40 <- mean(G1CongNep$X_40 , na.rm = T)
G1CongNepX41 <- mean(G1CongNep$X_41 , na.rm = T)
G1CongNepX42 <- mean(G1CongNep$X_42 , na.rm = T)
G1CongNepX43 <- mean(G1CongNep$X_43 , na.rm = T)
G1CongNepX44 <- mean(G1CongNep$X_44 , na.rm = T)
G1CongNepX45 <- mean(G1CongNep$X_45 , na.rm = T)
G1CongNepX46 <- mean(G1CongNep$X_46 , na.rm = T)
G1CongNepX47 <- mean(G1CongNep$X_47 , na.rm = T)
G1CongNepX48 <- mean(G1CongNep$X_48 , na.rm = T)
G1CongNepX49 <- mean(G1CongNep$X_49 , na.rm = T)
G1CongNepX50 <- mean(G1CongNep$X_50 , na.rm = T)
G1CongNepX51 <- mean(G1CongNep$X_51 , na.rm = T)
G1CongNepX52 <- mean(G1CongNep$X_52 , na.rm = T)
G1CongNepX53 <- mean(G1CongNep$X_53 , na.rm = T)
G1CongNepX54 <- mean(G1CongNep$X_54 , na.rm = T)
G1CongNepX55 <- mean(G1CongNep$X_55 , na.rm = T)
G1CongNepX56 <- mean(G1CongNep$X_56 , na.rm = T)
G1CongNepX57 <- mean(G1CongNep$X_57 , na.rm = T)
G1CongNepX58 <- mean(G1CongNep$X_58 , na.rm = T)
G1CongNepX59 <- mean(G1CongNep$X_59 , na.rm = T)
G1CongNepX60 <- mean(G1CongNep$X_60 , na.rm = T)
G1CongNepX61 <- mean(G1CongNep$X_61 , na.rm = T)
G1CongNepX62 <- mean(G1CongNep$X_62 , na.rm = T)
G1CongNepX63 <- mean(G1CongNep$X_63 , na.rm = T)
G1CongNepX64 <- mean(G1CongNep$X_64 , na.rm = T)
G1CongNepX65 <- mean(G1CongNep$X_65 , na.rm = T)
G1CongNepX66 <- mean(G1CongNep$X_66 , na.rm = T)
G1CongNepX67 <- mean(G1CongNep$X_67 , na.rm = T)
G1CongNepX68 <- mean(G1CongNep$X_68 , na.rm = T)
G1CongNepX69 <- mean(G1CongNep$X_69 , na.rm = T)
G1CongNepX70 <- mean(G1CongNep$X_70 , na.rm = T)
G1CongNepX71 <- mean(G1CongNep$X_71 , na.rm = T)
G1CongNepX72 <- mean(G1CongNep$X_72 , na.rm = T)
G1CongNepX73 <- mean(G1CongNep$X_73 , na.rm = T)
G1CongNepX74 <- mean(G1CongNep$X_74 , na.rm = T)
G1CongNepX75 <- mean(G1CongNep$X_75 , na.rm = T)
G1CongNepX76 <- mean(G1CongNep$X_76 , na.rm = T)
G1CongNepX77 <- mean(G1CongNep$X_77 , na.rm = T)
G1CongNepX78 <- mean(G1CongNep$X_78 , na.rm = T)
G1CongNepX79 <- mean(G1CongNep$X_79 , na.rm = T)
G1CongNepX80 <- mean(G1CongNep$X_80 , na.rm = T)
G1CongNepX81 <- mean(G1CongNep$X_81 , na.rm = T)
G1CongNepX82 <- mean(G1CongNep$X_82 , na.rm = T)
G1CongNepX83 <- mean(G1CongNep$X_83 , na.rm = T)
G1CongNepX84 <- mean(G1CongNep$X_84 , na.rm = T)
G1CongNepX85 <- mean(G1CongNep$X_85 , na.rm = T)
G1CongNepX86 <- mean(G1CongNep$X_86 , na.rm = T)
G1CongNepX87 <- mean(G1CongNep$X_87 , na.rm = T)
G1CongNepX88 <- mean(G1CongNep$X_88 , na.rm = T)
G1CongNepX89 <- mean(G1CongNep$X_89 , na.rm = T)
G1CongNepX90 <- mean(G1CongNep$X_90 , na.rm = T)
G1CongNepX91 <- mean(G1CongNep$X_91 , na.rm = T)
G1CongNepX92 <- mean(G1CongNep$X_92 , na.rm = T)
G1CongNepX93 <- mean(G1CongNep$X_93 , na.rm = T)
G1CongNepX94 <- mean(G1CongNep$X_94 , na.rm = T)
G1CongNepX95 <- mean(G1CongNep$X_95 , na.rm = T)
G1CongNepX96 <- mean(G1CongNep$X_96 , na.rm = T)
G1CongNepX97 <- mean(G1CongNep$X_97 , na.rm = T)
G1CongNepX98 <- mean(G1CongNep$X_98 , na.rm = T)
G1CongNepX99 <- mean(G1CongNep$X_99 , na.rm = T)
G1CongNepX100 <- mean(G1CongNep$X_100 , na.rm = T)
G1CongNepX101 <- mean(G1CongNep$X_101 , na.rm = T)
CongNepDF <- data.frame(ThTimeSteps = c(G1CongNepX1,G1CongNepX2,G1CongNepX3,G1CongNepX4,G1CongNepX5,G1CongNepX6,G1CongNepX7,G1CongNepX8,G1CongNepX9,G1CongNepX10,G1CongNepX11,G1CongNepX12,G1CongNepX13,G1CongNepX14,G1CongNepX15,G1CongNepX16,G1CongNepX17,G1CongNepX18,G1CongNepX19,G1CongNepX20, G1CongNepX21,G1CongNepX22,G1CongNepX23,G1CongNepX24,G1CongNepX25,G1CongNepX26,G1CongNepX27,G1CongNepX28,G1CongNepX29,G1CongNepX30,G1CongNepX31,G1CongNepX32,G1CongNepX33,G1CongNepX34,G1CongNepX35,G1CongNepX36,G1CongNepX37,G1CongNepX38,G1CongNepX39,G1CongNepX40,G1CongNepX41,G1CongNepX42,G1CongNepX43,G1CongNepX44,G1CongNepX45,G1CongNepX46,G1CongNepX47,G1CongNepX48,G1CongNepX49,G1CongNepX50,G1CongNepX51,G1CongNepX52,G1CongNepX53,G1CongNepX54,G1CongNepX55,G1CongNepX56,G1CongNepX57,G1CongNepX58,G1CongNepX59,G1CongNepX60,G1CongNepX61,G1CongNepX62,G1CongNepX63,G1CongNepX64,G1CongNepX65,G1CongNepX66,G1CongNepX67,G1CongNepX68,G1CongNepX69,G1CongNepX70,G1CongNepX71,G1CongNepX72,G1CongNepX73,G1CongNepX74,G1CongNepX75,G1CongNepX76,G1CongNepX77,G1CongNepX78,G1CongNepX79,G1CongNepX80,G1CongNepX81,G1CongNepX82,G1CongNepX83,G1CongNepX84,G1CongNepX85,G1CongNepX86,G1CongNepX87,G1CongNepX88,G1CongNepX89,G1CongNepX90,G1CongNepX91,G1CongNepX92,G1CongNepX93,G1CongNepX94,G1CongNepX95,G1CongNepX96,G1CongNepX97,G1CongNepX98,G1CongNepX99,G1CongNepX100,G1CongNepX101), yaxis = c(.0,.01,.02,.03,.04,.05,.06,.07,.08,.09,.10,.11,.12,.13,.14,.15,.16,.17,.18,.19,.20,.21,.22,.23,.24,.25,.26,.27,.28,.29,.30,.31,.32,.33,.34,.35,.36,.37,.38,.39,.40,.41,.42,.43,.44,.45,.46,.47,.48,.49,.50,.51,.52,.53,.54,.55,.56,.57,.58,.59,.60,.61,.62,.63,.64,.65,.66,.67,.68,.69,.70,.71,.72,.73,.74,.75,.76,.77,.78,.79,.80,.81,.82,.83,.84,.85,.86,.87,.88,.89,.90,.91,.92,.93,.94,.95,.96,.97,.98,.99,1))

Plot <- ggplot(data = CongNepDF, mapping = aes(x = ThTimeSteps, y = yaxis)) + geom_smooth() + xlab("101 Timesteps") + ylab("X coordinates")




#Literally all timesteps from G1IncongNep
G1IncongNepX1 <- mean(G1IncongNep$X_1 , na.rm = T)
G1IncongNepX2 <- mean(G1IncongNep$X_2 , na.rm = T)
G1IncongNepX3 <- mean(G1IncongNep$X_3 , na.rm = T)
G1IncongNepX4 <- mean(G1IncongNep$X_4 , na.rm = T)
G1IncongNepX5 <- mean(G1IncongNep$X_5 , na.rm = T)
G1IncongNepX6 <- mean(G1IncongNep$X_6 , na.rm = T)
G1IncongNepX7 <- mean(G1IncongNep$X_7 , na.rm = T)
G1IncongNepX8 <- mean(G1IncongNep$X_8 , na.rm = T)
G1IncongNepX9 <- mean(G1IncongNep$X_9 , na.rm = T)
G1IncongNepX10 <- mean(G1IncongNep$X_10 , na.rm = T)
G1IncongNepX11 <- mean(G1IncongNep$X_11 , na.rm = T)
G1IncongNepX12 <- mean(G1IncongNep$X_12, na.rm = T)
G1IncongNepX13 <- mean(G1IncongNep$X_13 , na.rm = T)
G1IncongNepX14 <- mean(G1IncongNep$X_14 , na.rm = T)
G1IncongNepX15 <- mean(G1IncongNep$X_15 , na.rm = T)
G1IncongNepX16 <- mean(G1IncongNep$X_16 , na.rm = T)
G1IncongNepX17 <- mean(G1IncongNep$X_17 , na.rm = T)
G1IncongNepX18 <- mean(G1IncongNep$X_18 , na.rm = T)
G1IncongNepX19 <- mean(G1IncongNep$X_19 , na.rm = T)
G1IncongNepX20 <- mean(G1IncongNep$X_20 , na.rm = T)
G1IncongNepX21 <- mean(G1IncongNep$X_21 , na.rm = T)
G1IncongNepX22 <- mean(G1IncongNep$X_22 , na.rm = T)
G1IncongNepX23 <- mean(G1IncongNep$X_23 , na.rm = T)
G1IncongNepX24 <- mean(G1IncongNep$X_24 , na.rm = T)
G1IncongNepX25 <- mean(G1IncongNep$X_25 , na.rm = T)
G1IncongNepX26 <- mean(G1IncongNep$X_26 , na.rm = T)
G1IncongNepX27 <- mean(G1IncongNep$X_27 , na.rm = T)
G1IncongNepX28 <- mean(G1IncongNep$X_28 , na.rm = T)
G1IncongNepX29 <- mean(G1IncongNep$X_29 , na.rm = T)
G1IncongNepX30 <- mean(G1IncongNep$X_30 , na.rm = T)
G1IncongNepX31 <- mean(G1IncongNep$X_31 , na.rm = T)
G1IncongNepX32 <- mean(G1IncongNep$X_32 , na.rm = T)
G1IncongNepX33 <- mean(G1IncongNep$X_33 , na.rm = T)
G1IncongNepX34<-  mean(G1IncongNep$X_34 , na.rm = T)
G1IncongNepX35 <- mean(G1IncongNep$X_35 , na.rm = T)
G1IncongNepX36 <- mean(G1IncongNep$X_36 , na.rm = T)
G1IncongNepX37 <- mean(G1IncongNep$X_37 , na.rm = T)
G1IncongNepX38 <- mean(G1IncongNep$X_38 , na.rm = T)
G1IncongNepX39 <- mean(G1IncongNep$X_39 , na.rm = T)
G1IncongNepX40 <- mean(G1IncongNep$X_40 , na.rm = T)
G1IncongNepX41 <- mean(G1IncongNep$X_41 , na.rm = T)
G1IncongNepX42 <- mean(G1IncongNep$X_42 , na.rm = T)
G1IncongNepX43 <- mean(G1IncongNep$X_43 , na.rm = T)
G1IncongNepX44 <- mean(G1IncongNep$X_44 , na.rm = T)
G1IncongNepX45 <- mean(G1IncongNep$X_45 , na.rm = T)
G1IncongNepX46 <- mean(G1IncongNep$X_46 , na.rm = T)
G1IncongNepX47 <- mean(G1IncongNep$X_47 , na.rm = T)
G1IncongNepX48 <- mean(G1IncongNep$X_48 , na.rm = T)
G1IncongNepX49 <- mean(G1IncongNep$X_49 , na.rm = T)
G1IncongNepX50 <- mean(G1IncongNep$X_50 , na.rm = T)
G1IncongNepX51 <- mean(G1IncongNep$X_51 , na.rm = T)
G1IncongNepX52 <- mean(G1IncongNep$X_52 , na.rm = T)
G1IncongNepX53 <- mean(G1IncongNep$X_53 , na.rm = T)
G1IncongNepX54 <- mean(G1IncongNep$X_54 , na.rm = T)
G1IncongNepX55 <- mean(G1IncongNep$X_55 , na.rm = T)
G1IncongNepX56 <- mean(G1IncongNep$X_56 , na.rm = T)
G1IncongNepX57 <- mean(G1IncongNep$X_57 , na.rm = T)
G1IncongNepX58 <- mean(G1IncongNep$X_58 , na.rm = T)
G1IncongNepX59 <- mean(G1IncongNep$X_59 , na.rm = T)
G1IncongNepX60 <- mean(G1IncongNep$X_60 , na.rm = T)
G1IncongNepX61 <- mean(G1IncongNep$X_61 , na.rm = T)
G1IncongNepX62 <- mean(G1IncongNep$X_62 , na.rm = T)
G1IncongNepX63 <- mean(G1IncongNep$X_63 , na.rm = T)
G1IncongNepX64 <- mean(G1IncongNep$X_64 , na.rm = T)
G1IncongNepX65 <- mean(G1IncongNep$X_65 , na.rm = T)
G1IncongNepX66 <- mean(G1IncongNep$X_66 , na.rm = T)
G1IncongNepX67 <- mean(G1IncongNep$X_67 , na.rm = T)
G1IncongNepX68 <- mean(G1IncongNep$X_68 , na.rm = T)
G1IncongNepX69 <- mean(G1IncongNep$X_69 , na.rm = T)
G1IncongNepX70 <- mean(G1IncongNep$X_70 , na.rm = T)
G1IncongNepX71 <- mean(G1IncongNep$X_71 , na.rm = T)
G1IncongNepX72 <- mean(G1IncongNep$X_72 , na.rm = T)
G1IncongNepX73 <- mean(G1IncongNep$X_73 , na.rm = T)
G1IncongNepX74 <- mean(G1IncongNep$X_74 , na.rm = T)
G1IncongNepX75 <- mean(G1IncongNep$X_75 , na.rm = T)
G1IncongNepX76 <- mean(G1IncongNep$X_76 , na.rm = T)
G1IncongNepX77 <- mean(G1IncongNep$X_77 , na.rm = T)
G1IncongNepX78 <- mean(G1IncongNep$X_78 , na.rm = T)
G1IncongNepX79 <- mean(G1IncongNep$X_79 , na.rm = T)
G1IncongNepX80 <- mean(G1IncongNep$X_80 , na.rm = T)
G1IncongNepX81 <- mean(G1IncongNep$X_81 , na.rm = T)
G1IncongNepX82 <- mean(G1IncongNep$X_82 , na.rm = T)
G1IncongNepX83 <- mean(G1IncongNep$X_83 , na.rm = T)
G1IncongNepX84 <- mean(G1IncongNep$X_84 , na.rm = T)
G1IncongNepX85 <- mean(G1IncongNep$X_85 , na.rm = T)
G1IncongNepX86 <- mean(G1IncongNep$X_86 , na.rm = T)
G1IncongNepX87 <- mean(G1IncongNep$X_87 , na.rm = T)
G1IncongNepX88 <- mean(G1IncongNep$X_88 , na.rm = T)
G1IncongNepX89 <- mean(G1IncongNep$X_89 , na.rm = T)
G1IncongNepX90 <- mean(G1IncongNep$X_90 , na.rm = T)
G1IncongNepX91 <- mean(G1IncongNep$X_91 , na.rm = T)
G1IncongNepX92 <- mean(G1IncongNep$X_92 , na.rm = T)
G1IncongNepX93 <- mean(G1IncongNep$X_93 , na.rm = T)
G1IncongNepX94 <- mean(G1IncongNep$X_94 , na.rm = T)
G1IncongNepX95 <- mean(G1IncongNep$X_95 , na.rm = T)
G1IncongNepX96 <- mean(G1IncongNep$X_96 , na.rm = T)
G1IncongNepX97 <- mean(G1IncongNep$X_97 , na.rm = T)
G1IncongNepX98 <- mean(G1IncongNep$X_98 , na.rm = T)
G1IncongNepX99 <- mean(G1IncongNep$X_99 , na.rm = T)
G1IncongNepX100 <- mean(G1IncongNep$X_100 , na.rm = T)
G1IncongNepX101 <- mean(G1IncongNep$X_101 , na.rm = T)
IncongNepDF <- data.frame(ThTimeStepsIncong = c(G1IncongNepX1,G1IncongNepX2,G1IncongNepX3,G1IncongNepX4,G1IncongNepX5,G1IncongNepX6,G1IncongNepX7,G1IncongNepX8,G1IncongNepX9,G1IncongNepX10,G1IncongNepX11,G1IncongNepX12,G1IncongNepX13,G1IncongNepX14,G1IncongNepX15,G1IncongNepX16,G1IncongNepX17,G1IncongNepX18,G1IncongNepX19,G1IncongNepX20, G1IncongNepX21,G1IncongNepX22,G1IncongNepX23,G1IncongNepX24,G1IncongNepX25,G1IncongNepX26,G1IncongNepX27,G1IncongNepX28,G1IncongNepX29,G1IncongNepX30,G1IncongNepX31,G1IncongNepX32,G1IncongNepX33,G1IncongNepX34,G1IncongNepX35,G1IncongNepX36,G1IncongNepX37,G1IncongNepX38,G1IncongNepX39,G1IncongNepX40,G1IncongNepX41,G1IncongNepX42,G1IncongNepX43,G1IncongNepX44,G1IncongNepX45,G1IncongNepX46,G1IncongNepX47,G1IncongNepX48,G1IncongNepX49,G1IncongNepX50,G1IncongNepX51,G1IncongNepX52,G1IncongNepX53,G1IncongNepX54,G1IncongNepX55,G1IncongNepX56,G1IncongNepX57,G1IncongNepX58,G1IncongNepX59,G1IncongNepX60,G1IncongNepX61,G1IncongNepX62,G1IncongNepX63,G1IncongNepX64,G1IncongNepX65,G1IncongNepX66,G1IncongNepX67,G1IncongNepX68,G1IncongNepX69,G1IncongNepX70,G1IncongNepX71,G1IncongNepX72,G1IncongNepX73,G1IncongNepX74,G1IncongNepX75,G1IncongNepX76,G1IncongNepX77,G1IncongNepX78,G1IncongNepX79,G1IncongNepX80,G1IncongNepX81,G1IncongNepX82,G1IncongNepX83,G1IncongNepX84,G1IncongNepX85,G1IncongNepX86,G1IncongNepX87,G1IncongNepX88,G1IncongNepX89,G1IncongNepX90,G1IncongNepX91,G1IncongNepX92,G1IncongNepX93,G1IncongNepX94,G1IncongNepX95,G1IncongNepX96,G1IncongNepX97,G1IncongNepX98,G1IncongNepX99,G1IncongNepX100,G1IncongNepX101), yaxis = c(.0,.01,.02,.03,.04,.05,.06,.07,.08,.09,.10,.11,.12,.13,.14,.15,.16,.17,.18,.19,.20,.21,.22,.23,.24,.25,.26,.27,.28,.29,.30,.31,.32,.33,.34,.35,.36,.37,.38,.39,.40,.41,.42,.43,.44,.45,.46,.47,.48,.49,.50,.51,.52,.53,.54,.55,.56,.57,.58,.59,.60,.61,.62,.63,.64,.65,.66,.67,.68,.69,.70,.71,.72,.73,.74,.75,.76,.77,.78,.79,.80,.81,.82,.83,.84,.85,.86,.87,.88,.89,.90,.91,.92,.93,.94,.95,.96,.97,.98,.99,1))
Plot2 <- ggplot(data = IncongNepDF, mapping = aes(x = ThTimeStepsIncong, y = yaxis, col = "red")) + geom_smooth() + xlab("101 Timesteps") + ylab("X coordinates") 

enter image description here

enter image description here

此代码生成these two plots。我希望他们在同一个轴上。我尝试使用geom_line()函数,但是没有用。另外,我尝试在这里搜索其他地方。 我应该先合并这两个数据框还是要有另一种方法?

谢谢。

1 个答案:

答案 0 :(得分:0)

您的两个Excel文件包含101列;每列包含一些您要计算平均值的数据。对? 解决此问题的更有效方法是:使用tidyr pivot_longer将数据框重整为长格式。这将创建一个类别列,其中将包含您的101列名称,以及一个值列,其中将包含实际数据。通过一些基本分组,我们可以有效地到达需要的位置。最后,我们创建一个ID列(此处为1到101),可用作x或y轴。

这是一个简单的例子,说明了这个想法。该示例仅限于10列,但可以轻松扩展到许多列。 pivot_longer函数可帮助您调整形状并避免键入100个列名。

library(dplyr)
library(tidyr)
library(tibble)
library(ggplot2)

data = tibble(
  col1 = runif(min = 0, max = 10, n = 1000),
  col2 = runif(min = 0, max = 10, n = 1000),
  col3 = runif(min = 0, max = 10, n = 1000),
  col4 = runif(min = 0, max = 10, n = 1000),
  col5 = runif(min = 0, max = 10, n = 1000),
  col6 = runif(min = 0, max = 10, n = 1000),
  col7 = runif(min = 0, max = 10, n = 1000),
  col8 = runif(min = 0, max = 10, n = 1000),
  col9 = runif(min = 0, max = 10, n = 1000),
  col10 = runif(min = 0, max = 10, n = 1000)
)

data <- data %>%
  pivot_longer(cols = contains("col"),
               names_to = "groups",
               values_to = "values") %>%
  group_by(groups) %>%
  summarize(mean = mean(values)) %>%
  mutate(ID = row_number())

ggplot(data = data, aes(x = mean, y = ID)) +
  geom_smooth()

ggplot(data = data, aes(x = mean, y = ID)) +
  geom_line()

您现在可以使用geom_linegeom_smooth。希望这会有所帮助。