0

I've been trying to get a graph for several days now with no success, help would be really appreciated.

I've tried to replicate saveral examples, especially this one from here

haireye <- subset(as.data.frame(HairEyeColor), Sex == "Male")
n1 <- nPlot(Freq ~ Eye, group = 'Hair', data = haireye, type = 'multiBarChart')
n1$chart(stacked = TRUE, forceY = 120)

the result I'd like to get is basically this one

this is my code:

graficaOccorrenzeContrattiPagamenti = function(tabellaDati) {
require(plyr)
tabellaDatiMelt = melt(tabellaDati, id = c('Regione','Pagamento'))
risultato = tabellaDatiMelt[order(tabellaDatiMelt$value,decreasing = TRUE, na.last=NA),]
grafico <- nPlot( value ~ Regione, group = 'Pagamento', data = risultato, type = 'multiBarChart')
grafico$xAxis(rotateLabels = -45)
grafico$set(reduceXTicks = FALSE)
grafico$chart(stacked = TRUE)
return(grafico)
}

and these are the values in "risultato" passed to nplot:

                 Regione  Pagamento variable value
12                VENETO Bollettino   Valore  1299
31                VENETO   Bonifico   Valore    13
51                VENETO        RID   Valore   969
60                VENETO      Vuoto   Valore    25
21         VALLE D'AOSTA Bollettino   Valore     1
69         VALLE D'AOSTA      Vuoto   Valore     0
70         VALLE D'AOSTA   Bonifico   Valore     0
71         VALLE D'AOSTA        RID   Valore     0
1                 UMBRIA Bollettino   Valore   774
22                UMBRIA   Bonifico   Valore     6
34                UMBRIA        RID   Valore   161
72                UMBRIA      Vuoto   Valore     0
20   TRENTINO ALTO ADIGE Bollettino   Valore    47
53   TRENTINO ALTO ADIGE        RID   Valore    22
73   TRENTINO ALTO ADIGE      Vuoto   Valore     0
74   TRENTINO ALTO ADIGE   Bonifico   Valore     0
2                TOSCANA Bollettino   Valore  1475
28               TOSCANA   Bonifico   Valore     5
35               TOSCANA        RID   Valore   399
59               TOSCANA      Vuoto   Valore     9
7                SICILIA Bollettino   Valore 18089
29               SICILIA   Bonifico   Valore    11
38               SICILIA        RID   Valore   462
57               SICILIA      Vuoto   Valore    40
16              SARDEGNA Bollettino   Valore    23
49              SARDEGNA        RID   Valore     2
75              SARDEGNA      Vuoto   Valore     0
76              SARDEGNA   Bonifico   Valore     0
3                 PUGLIA Bollettino   Valore  6258
27                PUGLIA   Bonifico   Valore     3
40                PUGLIA        RID   Valore   382
66                PUGLIA      Vuoto   Valore     2
14              PIEMONTE Bollettino   Valore  1005
33              PIEMONTE   Bonifico   Valore     1
43              PIEMONTE        RID   Valore   480
63              PIEMONTE      Vuoto   Valore     4
18                  NULL Bollettino   Valore   250
48                  NULL        RID   Valore    15
77                  NULL      Vuoto   Valore     0
78                  NULL   Bonifico   Valore     0
8                 MOLISE Bollettino   Valore  3283
45                MOLISE        RID   Valore   136
55                MOLISE      Vuoto   Valore     3
79                MOLISE   Bonifico   Valore     0
4                 MARCHE Bollettino   Valore   195
36                MARCHE        RID   Valore    74
80                MARCHE      Vuoto   Valore     0
81                MARCHE   Bonifico   Valore     0
11             LOMBARDIA Bollettino   Valore  2395
24             LOMBARDIA   Bonifico   Valore    33
42             LOMBARDIA        RID   Valore   692
64             LOMBARDIA      Vuoto   Valore     5
15               LIGURIA Bollettino   Valore    88
47               LIGURIA        RID   Valore    29
62               LIGURIA      Vuoto   Valore     2
82               LIGURIA   Bonifico   Valore     0
5                  LAZIO Bollettino   Valore 12344
23                 LAZIO   Bonifico   Valore    37
37                 LAZIO        RID   Valore   468
56                 LAZIO      Vuoto   Valore    10
19 FRIULI VENEZIA GIULIA Bollettino   Valore   937
52 FRIULI VENEZIA GIULIA        RID   Valore   682
61 FRIULI VENEZIA GIULIA      Vuoto   Valore    12
83 FRIULI VENEZIA GIULIA   Bonifico   Valore     0
17        EMILIA ROMAGNA Bollettino   Valore   442
32        EMILIA ROMAGNA   Bonifico   Valore     1
50        EMILIA ROMAGNA        RID   Valore   123
68        EMILIA ROMAGNA      Vuoto   Valore     2
6               CAMPANIA Bollettino   Valore  6099
25              CAMPANIA   Bonifico   Valore    66
39              CAMPANIA        RID   Valore   204
65              CAMPANIA      Vuoto   Valore    36
13              CALABRIA Bollettino   Valore 13117
30              CALABRIA   Bonifico   Valore     6
41              CALABRIA        RID   Valore   501
54              CALABRIA      Vuoto   Valore    18
10            BASILICATA Bollettino   Valore   989
26            BASILICATA   Bonifico   Valore     4
44            BASILICATA        RID   Valore    44
67            BASILICATA      Vuoto   Valore     3
9                ABRUZZO Bollettino   Valore  1958
46               ABRUZZO        RID   Valore    45
58               ABRUZZO      Vuoto   Valore     3
84               ABRUZZO   Bonifico   Valore     0 

I also tried to remove "variable" column in case it might cause some prbolem but to no avail. No success with hPlot, dPlot, uPlot, mPlot either. If I wrote some clear nonsense please be as clear as possible since I'm very new to R language and still struggling to understand how the whole thing works.

2 Answers2

2

Here is one way to get the plot you are interested in using nPlot. The basic idea is to transform the data before plotting it so that in addition to the Freq column, you also have percentages.

library(plyr); library(rCharts)
haireye <- subset(as.data.frame(HairEyeColor), Sex == "Male")
haireye <- ddply(haireye, .(Eye), transform, Perc = Freq/sum(Freq)*100)
n1 <- nPlot(Freq ~ Eye, group = 'Hair', data = haireye, type = 'multiBarChart')
n1$chart(stacked = TRUE, forceY = 120)
n1

Here is an interactive version of the code that you can play with online.

Ramnath
  • 54,439
  • 16
  • 125
  • 152
1

Hello with the dimple library you can do like this :

d1 = dPlot(y = "Regione", x = "value",data = risultato, groups = "Pagamento",type = "bar") 
d1$yAxis(type = "addCategoryAxis", orderRule = "Regione")
d1$xAxis(type = "addPctAxis") 
d1$legend(
  x = 0,
  y = 0,
  width = 500,
  height = 75,
  horizontalAlign = "right"
)
d1

You can find many examples with rCharts here

Victorp
  • 13,636
  • 2
  • 51
  • 55
  • thank you very much! even more for the link where I can see the R code instead of the javascript I continue to find everywhere. – user3872474 Jul 24 '14 at 12:36
  • 1
    more links for you if you go the dimple route http://timelyportfolio.github.io/rCharts_dimple/gallery/ http://timelyportfolio.github.io/docs/_build/html/dimple/index.html – timelyportfolio Jul 25 '14 at 15:06