r - ggplot添加线散布

标签 r ggplot2

我有“观察到的数据”,它们是经典的 [X, Y, Z] 点。 X、Y、Z 保存在名为 X、Y、Z 的向量中。(通常为 10-30 个值。)

然后我有“模型数据的结果”,它是一组 [X, Y] 点创建一条平滑线(= 特制回归线)。数据保存在 Model_X 和 Model_Y 向量中。 (大约 1000 个点可以创建额外的平滑线。)

如何散点图“观察到的数据”,然后在上面绘制“结果”线?

散点图“观察数据”的代码效果很好:

library("ggplot2")

Data_X = c(2,3,4,5,6,8,9,10,11,14)
Data_Y = c(3.25,3.66666666666667,1.83333333333333,2.4,2.5,2.375,2.16666666666667,2.8,2.18181818181818,1.85714285714286)
Data_Z = c(2,1,3,3,1,1,2,1,1,1)

Model1_X = c(1,1.13131313131313,1.26262626262626,1.39393939393939,1.52525252525253,1.65656565656566,1.78787878787879,1.91919191919192,2.05050505050505,2.18181818181818,2.31313131313131,2.44444444444444,2.57575757575758,2.70707070707071,2.83838383838384,2.96969696969697,3.1010101010101,3.23232323232323,3.36363636363636,3.4949494949495,3.62626262626263,3.75757575757576,3.88888888888889,4.02020202020202,4.15151515151515,4.28282828282828,4.41414141414141,4.54545454545455,4.67676767676768,4.80808080808081,4.93939393939394,5.07070707070707,5.2020202020202,5.33333333333333,5.46464646464647,5.5959595959596,5.72727272727273,5.85858585858586,5.98989898989899,6.12121212121212,6.25252525252525,6.38383838383838,6.51515151515152,6.64646464646465,6.77777777777778,6.90909090909091,7.04040404040404,7.17171717171717,7.3030303030303,7.43434343434344,7.56565656565657,7.6969696969697,7.82828282828283,7.95959595959596,8.09090909090909,8.22222222222222,8.35353535353535,8.48484848484848,8.61616161616162,8.74747474747475,8.87878787878788,9.01010101010101,9.14141414141414,9.27272727272727,9.4040404040404,9.53535353535354,9.66666666666667,9.7979797979798,9.92929292929293,10.0606060606061,10.1919191919192,10.3232323232323,10.4545454545455,10.5858585858586,10.7171717171717,10.8484848484849,10.979797979798,11.1111111111111,11.2424242424242,11.3737373737374,11.5050505050505,11.6363636363636,11.7676767676768,11.8989898989899,12.030303030303,12.1616161616162,12.2929292929293,12.4242424242424,12.5555555555556,12.6868686868687,12.8181818181818,12.949494949495,13.0808080808081,13.2121212121212,13.3434343434343,13.4747474747475,13.6060606060606,13.7373737373737,13.8686868686869,14)
Model1_Y = c(3.25,3.18555420097834,3.12926919689739,3.07941021181297,3.03473378038082,2.99431957725982,2.95746824042015,2.9236364484034,2.89239410255925,2.86339518366821,2.83635737247483,2.81104746070721,2.78727069071306,2.76486282326236,2.74368413932923,2.72361483829083,2.70455146117348,2.68640407763096,2.6690940496996,2.65255223653955,2.63671754016454,2.62153571758434,2.60695840309297,2.59294229779074,2.57944849328738,2.5664419038903,2.55389078712974,2.5417663366947,2.53004233509601,2.51869485588397,2.50770200720773,2.49704371004394,2.48670150564246,2.47665838770824,2.46689865561819,2.45740778560022,2.44817231731109,2.43917975366564,2.43041847210995,2.4218776458117,2.41354717347233,2.40541761665863,2.39748014371141,2.38972647942389,2.382148859795,2.37473999125844,2.36749301386891,2.36040146799541,2.35345926413012,2.34666065547108,2.34000021297974,2.33347280265142,2.32707356476813,2.32079789493082,2.31464142669197,2.30860001562977,2.30266972472342,2.29684681090454,2.29112771267366,2.28550903868246,2.27998755719325,2.27456018633652,2.26922398509527,2.26397614495246,2.25881398214421,2.25373493046685,2.24873653459143,2.24381644384331,2.23897240640893,2.23420226393503,2.22950394648906,2.22487546785239,2.22031492112025,2.21582047458498,2.21139036788103,2.2070229083721,2.20271646776243,2.19846947891591,2.19428043286793,2.1901478760162,2.18607040747793,2.18204667660161,2.17807538062293,2.17415526245479,2.17028510860243,2.16646374719532,2.16269004612806,2.15896291130317,2.15528128496922,2.15164414414803,2.14805049914555,2.14449939214096,2.14098989584917,2.13752111225228,2.13409217139578,2.13070223024546,2.12735047160163,2.12403610306702,2.12075835606543,2.11751648490801)

observedData <- data.frame("X" = as.factor(Data_X), "Y" = as.factor(Data_Y), "Z" = as.factor(Data_Z))

p <- ggplot(observedData, aes(Data_X, Data_Y))
p + geom_point(aes(colour = Data_Z)) + scale_colour_gradient(low = "green", high="red")
p + geom_line(aes(Model1_X, Model1_Y))  #<-- this replaces original scatterplot with line plot, but does not merge with it

如何根据向量 Model_X 和 Model_Y 添加线?我尝试了很多不同的方法,但无法移动,总是只工作其中一个情节。

最佳答案

library(ggplot2)

#Create a scatterplot using mtcars data with ggplot object p1 as the base layer
p1 <- ggplot(mtcars, aes(x = hp, y = mpg))

#Specify the color of points in the next layer
p1 <- p1 + geom_point(size = 2, aes(color=factor(am)))

#connect points with line
p2 <- p1 + geom_line()                           

#Adding a regression line
p3 <- p2 + geom_smooth(method = "lm", se = TRUE)  

p4 <- p3 + labs(x="Horsepower", y = "Miles per Gallon", title= "Linear Regression")

p4

enter image description here

关于r - ggplot添加线散布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27664000/

相关文章:

r - 按 ID 列折叠所有列

r - geom_smooth() 中 "method"参数值有什么区别

r - 如何在 R 中创建带有辅助分组 x 轴的条形图?

R/ggplot2 : how to match legend and plot colors in overlapping area plots?

r - 为什么 stat_summary 会根据变量绘制多条/单条线?

r - 与 R 的音频比较

r - 如何在 R 中的列的值序列中找到最大值和最小值?

r - 填写图例、ggplot 的符号

r - 使用 R 中的 ggplot2 绘制具有平均段的分类散点图

r - ggraph - 根据频率增加节点大小