r - ggplot2 positive/negative plot not rendering cleanly -
i'm trying replicate top bottom plot example here it's not rendering correctly (the purple series has +ve , -ve values , green negative) leaving messy artifacts. i'm struggling create toy example replicates issue i'm hoping can despite lack of data
my code is:
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))
the structure of data is:
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")
i figured out issue, data points can positive or negative. appears stacking doesn't work if don't have value each variable each datetime. instead of doing this
negatives <- result[result$value < 0,] positives <- result[result$value >= 0,]
i this
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')
and chart renders correctly
dave
Comments
Post a Comment