瀑布图和图表(和R)

时间:2013-08-28 15:58:22

标签: r data-visualization

首先,看起来有两种“瀑布式”数据可视化:
瀑布图(主要用于金融)如下:
http://en.wikipedia.org/wiki/Waterfall_chart
和瀑布情节(主要用于科学)如下:
http://scigra.ph/post/21332335998/scigraph-another-graphic-design-blog

我试图用类似R的东西制作第二种类型(瀑布PLOT)但是当我尝试谷歌搜索时 - 大多数是第一种(瀑布CHART)出来。关于如何在R中制作类似图的任何建议(假设我有x,y,z)?

非常感谢你!

1 个答案:

答案 0 :(得分:1)

我不知道你所使用的情节的现成功能,但在R中烹饪你自己的剧本并不复杂。这是一个例子。

# Simulate the data (from normal distribution)
d<-rnorm(1000)
# Calculate the density of the data
xd<-density(d)$x
yd<-density(d)$y
# Specify how many curves to plot
no.of.curves<-51

# Open a new plot window
x11(6, 8)
# Set background to black
par(bg=1)
# The the initial plot
plot(x=xd, y=yd+(no.of.curves-1)/10, ylim=c(0,no.of.curves/10+max(yd)), col="grey50", type="l", lwd=2)
# Color the curve with black
polygon(xd, yd+(no.of.curves-1)/10-0.02, col="black", border=NA)
# Add more urves to the plot
for(i in 1:no.of.curves) {
   lines(x=xd, y=yd+(no.of.curves-i)/10, ylim=c(0,no.of.curves/10+max(yd)), col="grey50", type="l", lwd=2)
   polygon(xd, yd+(no.of.curves-i)/10-0.02, col="black", border=NA)
}

这应该创造一些概念相似但不完全相同的情节:

enter image description here 如果这是您正在寻找的,那么上面的脚本可以变成一个函数,可以为任何数据集生成图。您能否提供一些您想要绘制的示例数据集?

对于注释中的数据,以下代码将生成填充区域而不是线条,并且颜色已反转:

d<-structure(list(x = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L), 
    y = c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 
    4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), z = c(5.47, 
    3.36, 2.99, 3.04, 3.73, 3.25, 3.04, 2.19, 1.6, 2.63, 3.49, 
    2.48, 2.7, 1.6, 2.7, 3.33, 1.94, 2.39, 2.89, 2.94, 4.35, 
    3.21, 3.4, 3.36, 4.74)), .Names = c("x", "y", "z"), class = "data.frame", row.names = c(NA, 
-25L))

yvals<-rev(unique(d$y))
plot(x=0, y=0, ylim=c(min(d$y), max(d$y)+max(d$z)), xlim=c(min(d$x), max(d$x)), type="n", axes=F, xlab="", ylab="") 
 for(i in 1:length(yvals)) {
   a<-d[d$y==yvals[i],]
   polygon(x=a$x, y=a$z+i, border="grey75", col="black")
}

对于这些数据,没有恒定的基线,而多边形(彩色区域)看起来有点奇怪。