2016-01-21 108 views
3

我试图复制像例here的顶部底部情节,但它不能正确呈现(紫色系列有+ ve和-ve值和绿色是负面的)留下凌乱的文物。我也在努力创造出复制的问题,所以我希望有人能帮助我,尽管缺乏数据ggplot2积极/消极阴谋不渲染干净

我的代码是一个玩具例子:

negatives <- result[result$value < 0,] 
positives <- result[result$value >= 0,] 

ggplot() + 
    geom_area(data = negatives, aes(x = datetime, y = value, fill=variable)) + 
    geom_area(data = positives, aes(x = datetime, y = value, fill=variable)) 

我的数据的结构是:

dput(droplevels(head(result))) 

structure(
list(
    datetime = structure(c(1421751900, 1421751900, 1421752200, 1421752200, 1421752500, 1421752500), class = c("POSIXct", "POSIXt"), tzone = ""), 
    variable = structure(c(1L, 2L, 1L, 2L, 1L, 2L), .Label = c("V-S-MNSP1", "VIC1-NSW1"), class = "factor"), 
    value = c(9, 80.106180098, 9, 77.719632578, 9, 84.158868934) 
), 
.Names = c("datetime", "variable", "value"), 
row.names = c(NA, 6L), 
class = "data.frame") 

top/bottom plot

+1

你能不能让这个重现?也许模拟一些示例数据? – Gregor

+0

嗨@Gregor,不抱歉,这是我的问题 - 它只发生在我的真实数据集。迄今为止,我用玩具数据重现的所有尝试都失败了...... – user2981639

+0

我不确定在这种情况下,您希望任何人如何帮助您...您还可以分享您可以找到的最小数据样本说明了这个问题。 – Gregor

回答

0

我想通了这个问题,我的数据点可以是正的或负。如果我没有每个日期时间的每个变量的值,那么看起来堆叠不起作用。因此,与其这样

negatives <- result[result$value < 0,] 
positives <- result[result$value >= 0,] 

我做这个

result$positive <- ifelse(result$value >= 0,result$value,0) 
result$negative <- ifelse(result$value < 0,result$value,0) 

ggplot(data = result) + 
    geom_area(aes(x = datetime, y = positive, fill=variable), position = 'stack') + 
    geom_area(aes(x = datetime, y = negative, fill=variable), position = 'stack') 

然后我的图表呈现正确

戴夫