简体   繁体   English

以高斯/法线曲线绘制xy图,并带有误差(理想情况下为R)

[英]Plotting x-y plot with errors as Gaussian/normal curves (ideally in R)

I have xy data, which both have +/- errors (that are equal on each side). 我有xy数据,它们都有+/-错误(每边都相等)。 The type of data it is has normal distribution, on in both the xy direction. 它在xy方向上都具有正态分布的数据类型。 At the moment we plot it either as typical xy crosses, or using geom_rect(); 目前,我们要么将其绘制为典型的xy十字形,要么使用geom_rect();进行绘制。 but both of which have issues in demonstrating what the data represents. 但两者在演示数据代表的内容时都有问题。 I am looking for a solution that would allow the each of the xy data points to be represented as some sort of normal/Gaussian distribution (instead of just as +) as per my rough sketch below. 我正在寻找一种解决方案,按照下面的粗略草图,每个xy数据点都可以表示为某种正态/高斯分布(而不是+)。

两种误差均具有正态分布的x-y图

Below is an example data frame. 以下是示例数据帧。

structure(list(Age = c(2003L, 1999L, 1995L, 1993L, 1993L, 1990L, 1988L, 1987L, 1985L, 1984L, 1983L, 1975L, 1974L, 1972L, 1963L, 1960L, 1959L, 1957L, 1953L, 1951L, 1951L, 1946L, 1940L, 1936L, 1930L, 1927L, 1919L, 1914L, 1906L, 1885L, 1864L, 1842L, 1830L, 1810L, 1803L, 1783L, 1762L, 1741L, 1720L, 1699L, 1678L, 1657L ), Age_error = c(1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 4L, 2L, 2L, 2L, 3L, 5L, 3L, 3L, 4L, 6L, 4L, 8L, 5L, 7L, 5L, 10L, 14L, 17L, 23L, 21L, 20L, 53L, 67L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L), Value = c(0, 0.07, 0, 0.09, 0.02, 0.06, -0.02, 0.154, 0.05, 0.02, -0.03, -0.024, -0.01, -0.06, -0.15, -0.04, 0.065, -0.1, -0.09, -0.02, -0.024, -0.11, -0.081, -0.13, -0.12, -0.07, -0.16, -0.122, -0.057, -0.18, -0.095, -0.105, -0.23, -0.19, -0.178, -0.267, -0.26, -0.158, -0.079, -0.218, -0.148, -0.193), Value_error = c(0.17, 0.143, 0.18, 0.18, 0.17, 0.19, 0.18, 0.163, 0.19, 0.18, 0.18, 0.142, 0.17, 0.18, 0.17, 0.17, 0.152, 0.17, 0.17, 0.17, 0.151, 0.17, 0.154, 0.17, 0.18, 0.26, 结构(列表(年龄= c(2003L,1999L,1995L,1993L,1993L,1990L,1988L,1987L,1985L,1984L,1983L,1975L,1974L,1972L,1963L,1960L,1959L,1957L,1953L,1951L,1951L, 1946L,1940L,1936L,1930L,1927L,1919L,1914L,1906L,1885L,1864L,1842L,1830L,1810L,1803L,1783L,1762L,1741L,1720L,1699L,1678L,1657L),Age_error = c(1L,2L ,1L,1L,1L,1L,1L,2L,1L,1L,1L,4L,2L,2L,2L,3L,5L,3L,3L,4L,6L,4L,8L,5L,7L,5L,10L ,14L,17L,23L,21L,20L,53L,67L,30L,30L,30L,30L,30L,30L,30L,30L),值= c(0,0.07,0,0.09,0.02,0.06,-0.02 ,0.154、0.05、0.02,-0.03,-0.024,-0.01,-0.06,-0.15,-0.04、0.065,-0.1,-0.09,-0.02,-0.024,-0.11,-0.081,-0.13,-0.12 ,-0.07,-0.16,-0.122,-0.057,-0.18,-0.095,-0.105,-0.23,-0.19,-0.178,-0.267,-0.26,-0.158,-0.079,-0.218,-0.148,- 0.193),Value_error = c(0.17,0.143,0.18,0.18,0.17,0.19,0.18,0.163,0.19,0.18,0.18,0.142,0.17,0.18,0.17,0.17,0.152,0.17,0.17,0.17,0.151,0.17 ,0.154、0.17、0.18、0.26, 0.17, 0.144, 0.145, 0.18, 0.153, 0.153, 0.17, 0.18, 0.144, 0.155, 0.138, 0.141, 0.157, 0.14, 0.147, 0.137)), .Names = c("Age", "Age_error", "Value", "Value_error"), class = "data.frame", row.names = c(NA, -42L)) 0.17,0.144,0.145,0.18,0.153,0.153,0.17,0.18,0.144,0.155,0.138,0.141,0.157,0.14,0.147,0.137)),.names = c(“ Age”,“ Age_error”,“ Value” ,“ Value_error”),class =“ data.frame”,row.names = c(NA,-42L))

This is the sort of code I am using to just get a typical xy error plot for this data frame. 我正在使用这种代码来获取该数据帧的典型xy错误图。

ggplot() + geom_linerange(data=mydata, aes(y=Value, x=Age, xmin=Age-Age_error, xmax=Age+Age_error, ymin=Value-Value_error, ymax=Value+Value_error)) + geom_errorbarh(data=mydata, aes(y=Value, x=Age, xmin=Age-Age_error, xmax=Age+Age_error, ymin=Value-Value_error, ymax=Value+Value_error)) 

I haven't found a function yet to do xy normal distribution type plots and there might not be one, but thought someone might have some ideas! 我还没有找到一种可以做xy正态分布图的函数,可能没有,但是以为有人可能有一些想法! Many thanks in advance. 提前谢谢了。

Do you want a contour plot of Age versus Value as a 2d kernel density? 您是否希望将年龄与价值的等高线图作为2d内核密度?

require(MASS)
dens <- with(dat, MASS::kde2d(Age, Value))
str(dens)
#-------------
List of 3
 $ x: num [1:25] 1657 1671 1686 1700 1715 ...
 $ y: num [1:25] -0.267 -0.249 -0.232 -0.214 -0.197 ...
 $ z: num [1:25, 1:25] 0.00152 0.00187 0.00226 0.00267 0.00312 ...
#--------------
# kde2d is designed for contour display: x-vector, y-vector, z-Matrix
 contour(dens)

Added the data points so the connection between the contour plot and the data was more visible: 添加了数据点,以便等高线图和数据之间的联系更加明显:

 points(dat$Age, dat$Value, cex=0.3, col="red")

在此处输入图片说明

If you need each Age,Value pair to have a +ve and -ve error, then I think you may be looking for smoothScatter function. 如果您需要每个年龄,价值对都有+ ve和-ve错误,那么我认为您可能正在寻找smoothScatter函数。 This function plots densities of each point using a color scheme that fades as you get farther from the point. 此功能使用一种颜色方案绘制每个点的密度,该颜色方案随着您远离该点而逐渐消失。

smoothScatter(mydata$Age, mydata$Value)

Results in 结果是

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM