4

我在 Hadoop 集群上运行了一个 Pig 作业,该作业将一堆数据压缩成 R 可以处理的东西来进行群组分析。我有以下脚本,从倒数第二行开始,我的数据格式为:

> names(data)
[1] "VisitWeek" "ThingAge"    "MyMetric"

VisitWeek 是一个日期。ThingAge 和 MyMetric 是整数。

数据如下:

2010-02-07     49  12345

我到目前为止的脚本是:

# Load ggplot2 for charting 
library(ggplot2);

# Our file has headers - column names
data = read.table('weekly_cohorts.tsv',header=TRUE,sep="\t");

# Print the names
names(data)

# Convert to dates
data$VisitWeek = as.Date(data$VisitWeek)
data$ThingCreation = as.Date(data$ThingCreation)

# Fill in the age column
data$ThingAge = as.integer(data$VisitWeek - data$ThingCreation)

# Filter data to thing ages lt 10 weeks (70 days) + a sanity check for gt 0, and drop the creation week column
data = subset(data, data$ThingAge <= 70, c("VisitWeek","ThingAge","MyMetric"))
data = subset(data, data$ThingAge >= 0)

print(ggplot(data, aes(x=VisitWeek, y=MyMetric, fill=ThingAge)) + geom_area())

最后一行不起作用。我尝试了很多变化、条形图、直方图,但像往常一样,R 文档打败了我。

我希望它显示一个标准的 Excel 样式堆叠面积图 - 每个 ThingAge 的一个时间序列在 x 轴上堆叠在几周内,日期在 y 轴上。这种图表的一个例子在这里:http: //upload.wikimedia.org/wikipedia/commons/a/a1/Mk_Zuwanderer.png

我在这里阅读了文档:http ://had.co.nz/ggplot2/geom_area.html和http://had.co.nz/ggplot2/geom_histogram.html以及这个博客http://chartsgraphs.wordpress。 com/2008/10/05/r-lattice-plot-beats-excel-stacked-area-trend-chart/但我不能让它完全适合我。

我怎样才能做到这一点?

4

4 回答 4

5
library(ggplot2)
set.seed(134)
df <- data.frame(
    VisitWeek = rep(as.Date(seq(Sys.time(),length.out=5, by="1 day")),3),
    ThingAge = rep(1:3, each=5),
    MyMetric = sample(100, 15))

ggplot(df, aes(x=VisitWeek, y=MyMetric)) + 
    geom_area(aes(fill=factor(ThingAge)))

给我下面的图片。我怀疑您的问题在于正确指定区域图的填充映射:fill=factor(ThingAge)

在此处输入图像描述

于 2010-02-11T09:09:03.267 回答
2

ggplot(data.set, aes(x = Time, y = Value, color = Type)) + geom_area(aes(fill = Type), position = 'stack')

您需要给 geom_area 一个填充元素并将其堆叠(尽管这可能是默认值)

在这里找到http://www.mail-archive.com/r-help@r-project.org/msg84857.html

于 2010-02-11T00:23:36.837 回答
2

我能够得到我的结果:

我从https://stat.ethz.ch/pipermail/r-help/2005-August/077475.html加载了stackedPlot() 函数

功能(不是我的,见链接)是:


stackedPlot = function(data, time=NULL, col=1:length(data), ...) {

  if (is.null(time))
    time = 1:length(data[[1]]);

  plot(0,0
       , xlim = range(time)
       , ylim = c(0,max(rowSums(data)))
       , t="n" 
       , ...
       );

  for (i in length(data):1) {

    # Die Summe bis zu aktuellen Spalte
    prep.data = rowSums(data[1:i]);

    # Das Polygon muss seinen ersten und letzten Punkt auf der Nulllinie haben
    prep.y = c(0
                , prep.data
                , 0
                )

    prep.x = c(time[1]
                , time
                , time[length(time)]
                )

    polygon(prep.x, prep.y
            , col=col[i]
            , border = NA
            );
  }
}

然后我将我的数据重新调整为宽格式。然后它起作用了!


wide = reshape(data, idvar="ThingAge", timevar="VisitWeek", direction="wide");
stackedPlot(wide);
于 2010-02-11T01:46:38.153 回答
2

将整数转换为因子并使用 geom_bar 而不是 geom_area 对我有用:

df<-expand.grid(x=1:10,y=1:6)
df<-cbind(df,val=runif(60))
df$fx<-factor(df$x)
df$fy<-factor(df$y)
qplot(fy,val,fill=fx,data=df,geom='bar')
于 2010-02-11T05:38:25.713 回答