创建散点图,其中符号高度/宽度由2个变量控制

时间:2013-06-24 14:40:15

标签: r ggplot2

我是R的新手,我正在尝试在ggplot2中创建散点图,这会影响散点图,其中每个符号的宽度和高度由两个变量控制。我已经想出了一个使用geom_errorbar和geo_errorbarh(下面的代码)的工作,它正在产生我正在寻找的东西,但我希望能够删除错误x / y十字线中间并只留下一个框每个数据点,我可以着色(我只需要它们是一种颜色)。我所谈论的事物的一个例子是:http://imbie.org/about-the-project/ice-sheet-mass-balance-measurements/

我的数据的一个子集是(我希望我正确使用了dput()):

structure(list(Year = c(2000L, 1995L, 1986L, 1977L, 1977L, 1973L, 
1973L, 1969L, 1965L, 1961L), RSL = c(0.02, 0.02, 0, -0.01, -0.07, 
0, -0.11, -0.1, -0.16, -0.07), Age_error = c(2L, 1L, 5L, 4L, 
3L, 4L, 3L, 2L, 2L, 3L), RSL_error = c(0.03, 0.04, 0.03, 0.03, 
0.05, 0.03, 0.05, 0.05, 0.05, 0.03), plus_error = c(0.05, 0.06, 
0.03, 0.02, -0.02, 0.03, -0.06, -0.05, -0.11, -0.04), minus_error = c(-0.01, 
-0.02, -0.03, -0.04, -0.12, -0.03, -0.16, -0.15, -0.21, -0.1), 
plus_age = c(2002L, 1996L, 1991L, 1981L, 1980L, 1977L, 1976L, 
1971L, 1967L, 1964L), minus_age = c(1998L, 1994L, 1981L, 
1973L, 1974L, 1969L, 1970L, 1967L, 1963L, 1958L), total_RSLerror = c(0.06, 
0.08, 0.06, 0.06, 0.1, 0.06, 0.1, 0.1, 0.1, 0.06), total_Ageerror = c(4L, 
2L, 10L, 8L, 6L, 8L, 6L, 4L, 4L, 6L)), .Names = c("Year", 
"RSL", "Age_error", "RSL_error", "plus_error", "minus_error", 
"plus_age", "minus_age", "total_RSLerror", "total_Ageerror"), class = "data.frame", 
row.names = c  ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"))

我目前获得的代码是:

    ggplot(data, aes(x=Year, y=RSL)) + geom_errorbar(aes(ymin=minus_error, ymax=plus_error, width=total_Ageerror)) + geom_errorbarh(aes(xmax=plus_age, xmin=minus_age, height=total_RSLerror)) + theme_bw()

非常感谢你的期待。如果我需要发送任何其他内容或者我错误地输入了这个内容,请告诉我。

1 个答案:

答案 0 :(得分:4)

您可以使用geom_rect()并将plus_minus_变量用作maxmin坐标(将您的数据命名为{{1})来实现})。

df

enter image description here