我在 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.htmlhttp://had.co.nz/ggplot2/geom_histogram.html 和这个博客 http://chartsgraphs.wordpress.com/2008/10/05/r-lattice-plot-beats-excel-stacked-area-trend-chart/ 但我不能完全让它为我工作。

我怎样才能实现这个目标?

有帮助吗?

解决方案

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)

enter image description here

其他提示

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

我能够得到我的结果:

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

该功能(不是我的,请参阅链接)是:


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);

将整数转换为因子并使用 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')
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top