首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >瀑布图和图表(和R)

瀑布图和图表(和R)
EN

Stack Overflow用户
提问于 2013-08-28 23:58:22
回答 1查看 1.7K关注 0票数 0

首先,看起来有两种“瀑布”数据可视化:

瀑布图(主要用于金融)如下所示:

http://en.wikipedia.org/wiki/Waterfall_chart

和瀑布图(主要用于科学)如下所示:

http://scigra.ph/post/21332335998/scigraph-another-graphic-design-blog

我正在尝试创建第二种类型(瀑布图),比如R,但当我尝试用谷歌搜索它时--主要是第一种类型(瀑布图)。关于如何在R中绘制类似的图(假设我有x,y,z),有什么建议吗?

非常感谢您的提前!

EN

回答 1

Stack Overflow用户

发布于 2013-08-29 00:55:28

我不知道有没有现成的函数来处理你想要的情节,但是用R编写你自己的脚本并不是那么复杂。下面是一个例子。

代码语言:javascript
复制
# 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)
}

这应该会创建一些概念上相似的东西,但不是完全相同的情节:

如果这就是您要找的,可以将上面的脚本转换为一个函数,该函数可以为任何数据集生成绘图。您能提供一些您想要绘制的示例数据集吗?

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

代码语言:javascript
复制
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")
}

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18494251

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档