gpt4 book ai didi

使用 ggplot2 在单个图中以图形方式表示多条曲线

转载 作者:行者123 更新时间:2023-12-04 09:31:34 28 4
gpt4 key购买 nike

enter image description here

我一直在尝试使用 ggplot2 在单个图中以图形方式表示多条曲线(大约 12 条)。我最初将数据收集在 Excel 工作表中,然后将它们原样传输到 R 中。每条曲线的数据量不同,每条曲线的 x 值也不同。因此,数据不能被视为矩阵或数据集。我想表示曲线而不分别提取两列中的数据以表示相应的曲线。

我尝试了很多版本的代码,例如以下用于表示前 2 条曲线的代码(没有结果):

library("ggplot2")
g <- ggplot(D, aes(x=V1))
k <- g + geom_line(aes(y=V2), colour="red")
s <- k + geom_line(aes(x=V5))
h <- s + geom_line(aes(y=V6), colour="green")

我在下文中展示了大量数据的最小版本。即使它看起来很大,即使它只有大约 8 行和 8 列。对此我深表歉意。为了一个简单的例子,我删除了许多列和行。所以,要表示的曲线一共有4条:(V1,V2)、(V5,V6)、(V11,V12)、(V15,V16),其中第一个坐标是x,第二个坐标是y 4 例。我将非常感谢你的帮助。

> dput(D)
structure(list(V1 = structure(c(85L, 86L, 87L, 88L, 89L, 90L,
1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "0", "0.005966", "0.011966",
"0.017966", "0.023966", "0.029966", "0.035966", "0.041966", "0.047966",
"0.053966", "0.059966", "0.065966", "0.071966", "0.077966", "0.083966",
"0.089966", "0.092265", "0.098408", "0.105918", "0.113602", "0.120645",
"0.130484", "0.137735", "0.148359", "0.154359", "0.165272", "0.171272",
"0.18083", "0.18683", "0.19283", "0.19883", "0.20483", "0.21083",
"0.21683", "0.22283", "0.22883", "0.23483", "0.24083", "0.252113",
"0.258113", "0.264113", "0.270113", "0.276113", "0.282113", "0.288113",
"0.294113", "0.300113", "0.306113", "0.312113", "0.318113", "0.324113",
"0.330113", "0.336113", "0.342113", "0.348113", "0.354113", "0.363916",
"0.375691", "0.381691", "0.393053", "0.399053", "0.405053", "0.411053",
"0.417053", "0.426986", "0.432986", "0.438986", "0.448759", "0.458853",
"0.464853", "0.470853", "0.481612", "0.487612", "0.497969", "0.503969",
"0.509969", "0.515969", "0.521969", "0.527969", "0.533969", "0.539969",
"0.551301", "0.557301", "0.562965", "0.568965", "0.574965", "0.580965",
"0.586965", "0.592965", "0.598965", "0.599966", "Displ.", "M11 (10-BF)"
), class = "factor"), V2 = structure(c(88L, 89L, 90L, 91L, 92L,
85L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "0", "112.369",
"149.825", "187.282", "224.738", "262.194", "299.651", "337.107",
"37.456", "374.564", "412.02", "449.476", "486.933", "524.389",
"561.845", "576.195", "605.792", "629.753", "648.093", "658.487",
"670.233", "677.776", "687.528", "692.703", "701.893", "706.104",
"712.587", "716.571", "720.277", "723.983", "727.688", "731.394",
"735.1", "738.806", "74.913", "742.512", "746.217", "749.923",
"756.33", "757.954", "759.576", "761.199", "762.82", "764.441",
"766.062", "767.654", "769.246", "770.837", "772.428", "774.018",
"775.572", "777.125", "778.678", "780.231", "781.783", "783.334",
"785.664", "788.255", "789.526", "791.883", "792.981", "793.987",
"794.895", "795.803", "796.996", "797.655", "798.313", "799.259",
"800.029", "800.407", "800.745", "801.259", "801.505", "801.915",
"802.145", "802.375", "802.604", "802.76", "802.915", "803.07",
"803.179", "803.188", "803.199", "803.322", "803.373", "803.413",
"803.438", "803.44", "803.441", "803.443", "803.444", "BaseFor."
), class = "factor"), V5 = structure(c(85L, 86L, 87L, 88L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "0", "0.005941",
"0.011941", "0.017941", "0.023941", "0.029941", "0.035941", "0.041941",
"0.047941", "0.053941", "0.059941", "0.065941", "0.071941", "0.077941",
"0.083941", "0.089941", "0.095941", "0.101941", "0.103817", "0.110449",
"0.118017", "0.125068", "0.13262", "0.143702", "0.152147", "0.15839",
"0.16439", "0.17039", "0.17639", "0.182967", "0.191488", "0.202601",
"0.208601", "0.214601", "0.223557", "0.229557", "0.235557", "0.241557",
"0.251764", "0.257764", "0.263764", "0.273723", "0.279723", "0.285723",
"0.296481", "0.302481", "0.308481", "0.314481", "0.320481", "0.329858",
"0.335858", "0.341858", "0.347858", "0.353858", "0.359858", "0.365858",
"0.371858", "0.38087", "0.38687", "0.39287", "0.404708", "0.415154",
"0.421154", "0.4287", "0.4347", "0.4407", "0.451398", "0.457398",
"0.463398", "0.469398", "0.475398", "0.487014", "0.497525", "0.509064",
"0.515064", "0.521064", "0.527064", "0.533064", "0.543151", "0.549151",
"0.555151", "0.566361", "0.57723", "0.58323", "0.58923", "0.59523",
"0.599941", "Displ.", "M13 (10-BF_M)"), class = "factor"), V6 = structure (c (84L,
85L, 86L, 87L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("",
"0", "112.442", "140.553", "168.663", "196.774", "224.885", "252.995",
"28.111", "281.106", "309.216", "337.327", "365.437", "393.548",
"421.659", "449.769", "477.598", "486.301", "515.282", "544.842",
"56.221", "567.028", "588.112", "612.031", "627.001", "636.278",
"644.516", "652.395", "660.274", "668.094", "676.388", "686.223",
"691.258", "696.203", "702.797", "706.954", "710.844", "714.734",
"721.266", "725.069", "728.873", "734.733", "738.113", "741.493",
"747.304", "750.435", "753.566", "756.618", "759.67", "763.8",
"765.277", "766.747", "768.217", "769.687", "771.156", "772.625",
"774.093", "776.263", "777.617", "778.97", "781.541", "783.744",
"784.896", "786.257", "787.267", "788.276", "789.981", "790.847",
"791.661", "792.411", "793.16", "794.53", "795.617", "796.748",
"797.29", "797.732", "798.143", "798.555", "799.151", "799.467",
"799.753", "800.244", "800.621", "800.772", "800.923", "801.074",
"801.193", "84.332", "BaseFor."), class = "factor"), V11 = structure(c (85L, 86L, 87L, 88L, 89L, 90L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("",
"0", "0.003903", "0.009903", "0.015903", "0.021903", "0.027903",
"0.033903", "0.039903", "0.045903", "0.051903", "0.057903", "0.063903",
"0.069903", "0.075903", "0.077429", "0.08433", "0.093127", "0.101114",
"0.108712", "0.11453", "0.12053", "0.124929", "0.130929", "0.136267",
"0.142267", "0.152885", "0.158885", "0.164885", "0.170885", "0.180633",
"0.190768", "0.196768", "0.202768", "0.208768", "0.214768", "0.22325",
"0.231018", "0.240961", "0.247414", "0.253414", "0.262807", "0.264757",
"0.270757", "0.276757", "0.284065", "0.29092", "0.293955", "0.296581",
"0.303881", "0.309881", "0.317746", "0.323746", "0.329746", "0.335746",
"0.341746", "0.347746", "0.353746", "0.359746", "0.365746", "0.371746",
"0.377746", "0.383746", "0.389746", "0.401176", "0.407176", "0.413936",
"0.421828", "0.427828", "0.433828", "0.439828", "0.445828", "0.451828",
"0.457828", "0.463828", "0.469828", "0.478943", "0.485564", "0.491564",
"0.497564", "0.503564", "0.509564", "0.515564", "0.521564", "0.527564",
"0.538766", "0.544766", "0.550766", "0.556766", "0.562766", "0.568766",
"0.574766", "0.580766", "0.586766", "0.592766", "0.597903", "Displ.",
"M15 (10-INF)"), class = "factor"), V12 = structure(c(64L, 63L,
62L, 61L, 60L, 59L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "0",
"1005.726", "1009.623", "1011.811", "1017.902", "1025.83", "1031.746",
"1038.527", "1039.66", "1042.112", "1056.988", "1067.679", "1071.904",
"1081.668", "1084.051", "1096.224", "1097.858", "1106.559", "1118.378",
"1125.618", "1135", "1140.472", "1141.291", "1148.964", "1156.559",
"1166.651", "1176.709", "1186.523", "1198.38", "1202.793", "1217.696",
"1226.19", "1234.685", "1240.749", "1242.795", "1256.85", "1268.252",
"1269.925", "1272.089", "1275.215", "1275.357", "1276.389", "166.25",
"254.359", "343.708", "433.057", "522.87", "612.683", "702.496",
"79.716", "792.309", "858.234", "859.779", "861.582", "863.381",
"865.178", "866.972", "868.763", "870.552", "872.337", "874.12",
"875.901", "878.915", "880.338", "881.758", "882.122", "883.176",
"884.591", "886.003", "887.412", "888.819", "889.813", "890.896",
"891.464", "893.109", "895.73", "899.729", "903.725", "907.718",
"911.709", "915.696", "921.024", "926.016", "932.761", "944.564",
"949.074", "950.715", "956.855", "962.992", "969.127", "975.258",
"981.357", "987.454", "993.547", "999.638", "BaseFor."), class = "factor"),
V15 = structure(c(85L, 86L, 87L, 88L, 89L, 90L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("", "0", "0.000278", "0.005722",
"0.011722", "0.017722", "0.023722", "0.029722", "0.035722",
"0.041722", "0.047722", "0.053722", "0.059722", "0.065722",
"0.071722", "0.077722", "0.083722", "0.089722", "0.095722",
"0.101722", "0.107722", "0.113722", "0.117013", "0.123013",
"0.129013", "0.138671", "0.14632", "0.156907", "0.163297",
"0.165095", "0.171095", "0.181276", "0.185661", "0.191661",
"0.197661", "0.20741", "0.219165", "0.227842", "0.233842",
"0.239842", "0.245842", "0.251842", "0.257842", "0.265518",
"0.277034", "0.287175", "0.293925", "0.298905", "0.304905",
"0.310905", "0.316905", "0.319905", "0.327", "0.337938",
"0.345053", "0.353392", "0.359392", "0.365392", "0.373443",
"0.381492", "0.390686", "0.398531", "0.406132", "0.412132",
"0.418132", "0.424132", "0.430132", "0.436132", "0.442132",
"0.450659", "0.456659", "0.462659", "0.468659", "0.477793",
"0.483793", "0.489793", "0.495793", "0.501793", "0.507793",
"0.513793", "0.519793", "0.525793", "0.531793", "0.537793",
"0.543793", "0.549793", "0.555793", "0.561793", "0.567793",
"0.573793", "0.579793", "0.585793", "0.591793", "0.593722",
"Displ.", "M17 (10-INF_M)"), class = "factor"), V16 = structure(c(66L,
65L, 64L, 63L, 62L, 61L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("",
"0", "1001.042", "1007.585", "1013.736", "1018.478", "1022.144",
"1030.544", "1043.215", "1054.922", "1055.09", "1073.135",
"1088.127", "1092.718", "1101.899", "1107.55", "1112.331",
"1122.695", "1127.945", "1135.753", "1145.092", "1147.475",
"1161.206", "1173.141", "1183.647", "1189.412", "1194.152",
"1204.658", "1212.448", "1214.9", "1218.199", "1224.255",
"1229.838", "1235.349", "1245.109", "1247.205", "1248.478",
"1251.639", "1251.741", "133.508", "182.716", "232.96", "283.203",
"333.447", "383.69", "39.235", "433.934", "484.177", "534.421",
"584.777", "635.134", "685.49", "735.847", "785.196", "81.948",
"831.454", "849.509", "850.124", "852.032", "854.335", "856.635",
"858.931", "861.223", "863.514", "866.744", "870.24", "873.733",
"875.898", "877.221", "881.135", "885.044", "888.948", "893.188",
"895.326", "900.137", "905.603", "911.296", "916.983", "918.404",
"926.137", "932.075", "938.009", "943.939", "951.689", "956.848",
"958.382", "962.005", "967.053", "972.099", "977.142", "980.212",
"981.722", "986.714", "993.275", "BaseFor."), class = "factor")), .Names = c("V1",
"V2", "V5", "V6", "V11", "V12", "V15", "V16"), row.names = c(3L,
4L, 5L, 6L, 7L, 8L, 12L, 13L, 14L, 15L, 16L, 17L), class = "data.frame")

最佳答案

考虑到您的需要,您应该像这样在 csv 文件中安排数据

library(magrittr)
library(ggplot2)

D <- structure(list(X = c(0.562965, 0.568965, 0.574965, 0.580965,
0.586965, 0.592965, 0.58323, 0.58923, 0.59523, 0.599941, 0.527564,
0.538766, 0.544766, 0.550766, 0.556766, 0.562766, 0.543793, 0.549793,
0.555793, 0.561793, 0.567793, 0.573793), Y = c(803.438, 803.44,
803.441, 803.443, 803.444, 803.322, 800.772, 800.923, 801.074,
801.193, 878.915, 875.901, 874.12, 872.337, 870.552, 868.763,
870.24, 866.744, 863.514, 861.223, 858.931, 856.635), Group = c("V1_V2",
"V1_V2", "V1_V2", "V1_V2", "V1_V2", "V1_V2", "V5_V6", "V5_V6",
"V5_V6", "V5_V6", "V11_V12", "V11_V12", "V11_V12", "V11_V12",
"V11_V12", "V11_V12", "V15_V16", "V15_V16", "V15_V16", "V15_V16",
"V15_V16", "V15_V16")), .Names = c("X", "Y", "Group"), row.names = c(NA,
-22L), class = c("tbl_df", "tbl", "data.frame"), spec = structure(list(
cols = structure(list(X = structure(list(), class = c("collector_double",
"collector")), Y = structure(list(), class = c("collector_double",
"collector")), Group = structure(list(), class = c("collector_character",
"collector"))), .Names = c("X", "Y", "Group")), default = structure(list(),
class = c("collector_guess",
"collector"))), .Names = c("cols", "default"), class = "col_spec"))
head(D)

#> # A tibble: 6 x 3
#> X Y Group
#> <dbl> <dbl> <chr>
#> 1 0.563 803. V1_V2
#> 2 0.569 803. V1_V2
#> 3 0.575 803. V1_V2
#> 4 0.581 803. V1_V2
#> 5 0.587 803. V1_V2
#> 6 0.593 803. V1_V2

ggplot(D, aes(x = X, y = Y, color = Group, group = Group)) +
geom_line()

# or
D %>%
ggplot(., aes(x = X, y = Y, color = Group, group = Group)) +
geom_line()

编辑:根据OP的原始数据自动创建数据框D
归功于此 answer

D1 <- structure(list(V1 = c(0.562965, 0.568965, 0.574965, 0.580965, 
0.586965, 0.592965), V2 = c(803.438, 803.44, 803.441, 803.443,
803.444, 803.322), V5 = c(0.58323, 0.58923, 0.59523, 0.599941,
NA, NA), V6 = c(800.772, 800.923, 801.074, 801.193, NA, NA),
V11 = c(0.527564, 0.538766, 0.544766, 0.550766, 0.556766,
0.562766), V12 = c(878.915, 875.901, 874.12, 872.337, 870.552,
868.763), V15 = c(0.543793, 0.549793, 0.555793, 0.561793,
0.567793, 0.573793), V16 = c(870.24, 866.744, 863.514, 861.223,
858.931, 856.635)), .Names = c("V1", "V2", "V5", "V6", "V11",
"V12", "V15", "V16"), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"), spec = structure(list(cols = structure(list(
V1 = structure(list(), class = c("collector_double", "collector"
)), V2 = structure(list(), class = c("collector_double",
"collector")), V5 = structure(list(), class = c("collector_double",
"collector")), V6 = structure(list(), class = c("collector_double",
"collector")), V11 = structure(list(), class = c("collector_double",
"collector")), V12 = structure(list(), class = c("collector_double",
"collector")), V15 = structure(list(), class = c("collector_double",
"collector")), V16 = structure(list(), class = c("collector_double",
"collector"))), .Names = c("V1", "V2", "V5", "V6", "V11",
"V12", "V15", "V16")), default = structure(list(), class = c("collector_guess",
"collector"))), .Names = c("cols", "default"), class = "col_spec"))

# make group names which are the combination of every 2 column names
groupName <- paste0(names(D1)[c(TRUE, FALSE)], names(D1)[c(FALSE, TRUE)])
groupName
#> [1] "V1V2" "V5V6" "V11V12" "V15V16"

# next we split the data into a list of groups of 2 columns,
# then change the names of the list with setNames and
# rbind the list elements to a single data.table using rbindlist
# and specifying the idcol as 'Group'
library(data.table)
lst <- split.default(D1, cumsum(rep(c(TRUE, FALSE), ncol(D1)/2)))
D <- rbindlist(setNames(lst, groupName), idcol = "Group")

D %>%
ggplot(., aes(x = V1, y = V2, color = Group, group = Group)) +
xlab("X") + ylab("Y") +
geom_line()

其他提示:使用 readr 包中的 read_csv 将数据读入 R,因为它具有 stringsAsFactors = FALSE默认情况下,比 base R read.csv 快得多。了解更多信息 herehere .

reprex package 创建于 2018-03-25 (v0.2.0).

关于使用 ggplot2 在单个图中以图形方式表示多条曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49471490/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com