datastorm-open / rAmCharts

API for Amcharts
48 stars 17 forks source link

Example of stacked area / line chart? #86

Closed thomasthomasth closed 5 years ago

thomasthomasth commented 5 years ago

Hi,

Could you provide me with a minimal example of a stacked area chart? I can create non-stacked line charts but I do want stacked ones because of the nature of the data that I work with. Also it is not clear to me whether area charts can be created at all. But I think so..

Here is a non-stacked line-chart:

library(shiny)
library(rAmCharts)
library(pipeR)
pipeline(
  amXYChart(dataProvider =  data.frame(x = 1:10, y = round(runif(10), 2), y2 =round(runif(10), 2)), hideYScrollbar = T, stacked = T),
  addGraph(balloonText = 'x:<b>[[x]]</b> y:<b>[[y]]</b>',
           bullet = 'circle', lineAlpha=1, xField = 'x',yField = 'y', maxBulletSize = 100),
  addGraph(balloonText = 'x:<b>[[x]]</b> y:<b>[[y2]]</b>',
           bullet = 'circle', lineAlpha=1, xField = 'x',yField = 'y2', maxBulletSize = 100,stacked = T),
  setChartCursor(),
  # updateOnReleaseOnly : wait for user releases mouse button.
  setChartScrollbar(updateOnReleaseOnly = TRUE))

Thanks,

Tamas

TitouanRobert commented 5 years ago

Hi,

You can add Y and Y2 in a 'Y3' column and keep Y2 in label :

library(shiny)
library(rAmCharts)
library(pipeR)

DT <- data.frame(x = 1:10, y = round(runif(10), 2), y2 =round(runif(10), 2))
DT$y3 <- DT$y+DT$y2
pipeline(
  amXYChart(dataProvider = DT, hideYScrollbar = T, stacked = T),
  addGraph(balloonText = 'x:<b>[[x]]</b> y:<b>[[y]]</b>',
           bullet = 'circle', lineAlpha=1, xField = 'x',yField = 'y', maxBulletSize = 100),
  addGraph(balloonText = 'x:<b>[[x]]</b> y:<b>[[y2]]</b>',
           bullet = 'circle', lineAlpha=1, xField = 'x',yField = 'y3', maxBulletSize = 100,stacked = T),
  setChartCursor(),
  setChartScrollbar(updateOnReleaseOnly = TRUE))

Titouan

thomasthomasth commented 5 years ago

Hi,

You can add Y and Y2 in a 'Y3' column and keep Y2 in label :

library(shiny)
library(rAmCharts)
library(pipeR)

DT <- data.frame(x = 1:10, y = round(runif(10), 2), y2 =round(runif(10), 2))
DT$y3 <- DT$y+DT$y2
pipeline(
  amXYChart(dataProvider = DT, hideYScrollbar = T, stacked = T),
  addGraph(balloonText = 'x:<b>[[x]]</b> y:<b>[[y]]</b>',
           bullet = 'circle', lineAlpha=1, xField = 'x',yField = 'y', maxBulletSize = 100),
  addGraph(balloonText = 'x:<b>[[x]]</b> y:<b>[[y2]]</b>',
           bullet = 'circle', lineAlpha=1, xField = 'x',yField = 'y3', maxBulletSize = 100,stacked = T),
  setChartCursor(),
  setChartScrollbar(updateOnReleaseOnly = TRUE))

Titouan

Hi,

Yeah, this is a nice workaround but this is a built-in option in amChart.js. It would not be efficient to perform this over and over again in R for each set of series - even though it gives us the opportunity to define custom tooltips. I frequently work with data that consists of several series (10-12, say). But number of such series changes from data to data - and adding these series one by one would be difficult to handle.

Here is an example that I tried to regenerate within R, but I could not manage this so far. It clearly says that there is a stacked option - which is exactly what I am looking for.

https://www.amcharts.com/demos/stacked-area/

Actually, I would like to accomplish this for time series, but I have found to stack option for amTimeSeries charts. Any help would be appreciated.

Thanks in advance,

Tamas

TitouanRobert commented 5 years ago

Hi,

You can use amSerialChart insted of amXYChart.

Exemple :

 dp <- data.frame(year = 1994:2012, 
                   cars = rnorm(length(1994:2012), mean = 10), 
                   motorcycles = rnorm(length(1994:2012), mean = 15), 
                   bicycles = rnorm(length(1994:2012), mean = 20)
  )
  dp <- round(dp)

  url_car <- 'http://www.amcharts.com/lib/3/images/car.png'
  url_motorcycle <- 'http://www.amcharts.com/lib/3/images/motorcycle.png'
  url_bicycle <- 'http://www.amcharts.com/lib/3/images/bicycle.png'
  pref <- '<img src = '
  suf <- paste('style = "vertical-align:bottom;',
               'margin-right: 10px; width:28px; height:21px;">',
               '<span style = "font-size:14px; color:#000000;">',
               '<b>[[value]]</b></span>')
  ##Plot
  pipeR::pipeline(
    amSerialChart(marginRight = 30, plotAreaBorderAlpha = 0, categoryField = 'year',
                  startDuration = 0, dataProvider = dp, theme = 'light'),
    setLegend(equalWidths = FALSE, periodValueText = 'total: [[value.sum]]',
              position = 'top', valueAlign = 'left', valueWidth = 100),
    addValueAxis(stackType = 'regular', gridAlpha = 0.07, position = 'left',
                 title = 'Traffic incidents'),
    addGraph(balloonText = paste(pref, url_car, suf), fillAlphas = 0.6,
             hidden = TRUE, lineAlpha = 0.4, title = 'Cars', valueField = 'cars'),
    addGraph(balloonText = paste(pref, url_motorcycle, suf), fillAlphas = 0.6,
             lineAlpha = 0.4, title = 'Motorcycles', valueField = 'motorcycles'),
    addGraph(balloonText = paste(pref, url_bicycle, suf), fillAlphas = 0.6,
             lineAlpha = 0.4, title = 'Bicycles', valueField = 'bicycles'),
    setCategoryAxis(startOnAxis = TRUE, axisColor = '#DADADA', gridAlpha = 0.07),
    addGuide(category = '2001', toCategory = '2003',
             lineColor = '#CC0000', lineAlpha = 1, fillAlpha = 0.2,
             fillColor = '#CC0000', dashLength = 2, inside = TRUE,
             labelRotation = 90, label = 'fines for speeding increased'),
    addGuide(category = '2007', lineColor = '#CC0000', lineAlpha = 1,
             dashLength = 2, inside = TRUE, labelRotation = 90,
             label = 'motorcycle fee introduced'),
    setChartScrollbar(oppositeAxis = FALSE, dragIcon = 'dragIconRectBigBlack'),
    setChartCursor(cursorAlpha = 0)
  )

If you want more exemples in R you can use : rAmCharts::runExamples()

Best,

Titouan

thomasthomasth commented 5 years ago

Perfect! This is exactly what I was looking for!

Best wishes,

Tamas