-1

I have the following example data and want to create a horizon plot, showing the changes in area column over the year. Any suggestion on doing this using ggplot2?

year <- 1990:2005
area1 <- runif(16, 18,20)
area2 <- runif (16,6,6.7)
area3 <- runif(16, 7,8)
dat <- data.frame(year, area1, area2, area3)
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Geo-sp
  • 1,704
  • 3
  • 19
  • 42

2 Answers2

5

You can create a horizon plot using ggplot_horizon from the ggTimeSeries package. Your data sample is a bit sparse for a good horizon plot, so I've created some fake data. The example below is based on the example in the ggTimeSeries vignette.

# Fake data
set.seed(1)
df = data.frame(x = rep(1:1000,3), y = cumsum(rnorm(3000)) + 50, 
                group=rep(LETTERS[1:3], each=1000))

#devtools::install_github("Ather-Energy/ggTimeSeries")
#install.packages("viridis")
library(ggTimeSeries)
library(viridis)

ggplot_horizon(df, 'x', 'y', vcGroupingColumnNames='group', bandwidth=10) +
  facet_grid(group ~ .) +
  scale_fill_viridis(option="inferno") 

enter image description here

eipi10
  • 91,525
  • 24
  • 209
  • 285
  • Thank you, these should be separate form each other so that they you have 3 horizontal plots and the color scheme represents the value. – Geo-sp Jun 24 '16 at 22:11
  • When you say "the color scheme represents the value," are you saying you want the color to vary horizontally within a ribbon based on the height of the ribbon? – eipi10 Jun 24 '16 at 22:21
  • yes, I want something like this: [link] (http://www.r-bloggers.com/horizon-plots-with-ggplot-not/) – Geo-sp Jun 24 '16 at 22:26
2

Not really sure what you're looking for but start with this structure and go from there.

dat <- reshape2::melt(dat, id.var = "year")
library(ggplot2)
ggplot(dat, aes(x = year, y = value, colour = variable)) + geom_point()
mkearney
  • 1,266
  • 10
  • 18