Using ggplot2 to create a Histogram - r

Still working in my project with R.
Here's the code I have so far.
# change columns to factors#
dat[, 9:23] <- lapply(dat[, 9:23], factor, levels = c(1, 2, 3, 4), labels = c("Nunca",
"1 vez por semana", "3 veces o menos a la semana", "Más de 4 veces a la semana"))
dat$Who_answers <- factor(dat$Who_answers, levels = c(1, 2, 3, 4), labels = c("Mamá",
"Papá", "Tutor", "Otro"))
dat$gender <- factor(dat$gender, levels = c(1, 2), labels = c("F", "M"))
dat$diagnosis <- factor(dat$diagnosis, levels = c(1, 2, 3, 4, 5, 6, 7), labels = c("Leucemia linfoblástica aguda",
"Leucemia mieloide aguda", "Linfoma de Hodgkin", "Linfoma no Hodgkin", "Tumor del sistema nervioso central", "Tumor sólido", "Otro"))
dat$acomp_rutsu <- factor(dat$acomp_rutsu, levels = c(1, 2, 3, 4), labels = c("Mamá", "Papá", "Abuela(o)", "Otro tutor"))
dat$Duerme_con <- factor(dat$Duerme_con, levels = c(1, 2, 3, 4), labels = c("Padres", "Hermanos", "Abuelos", "Solo"))
dat$Cuarto_propio <- factor(dat$Cuarto_propio, levels = c(1, 2), labels = c("Sí", "No"))
dat$Hospitalizado <- factor(dat$Hospitalizado, levels = c(1, 2), labels = c("Sí", "No"))
dat$Min_dormir <- factor(dat$Min_dormir, levels = c(1, 2, 3, 4), labels = c("Menos de 5 minutos",
"Entre 5-10 minutos", "Entre 10-15 minutos", "Más de 15 minutos"))
dat$Max_dormir <- factor(dat$Max_dormir, levels = c(1, 2, 3, 4), labels = c("Entre 5-10 minutos",
"Entre 10-15 minutos", "Entre 15-30 minutos", "Más de 30 minutos"))
dat$Levanta_noche <- factor(dat$Levanta_noche, levels = c(1, 2, 3, 4, 5), labels = c("Nunca",
"1 vez por noche", "2-3 veces por noche", "4-5 veces por noche", "Más de 5 veces por noche"))
dat$Tiempo_despNoche <- factor(dat$Tiempo_despNoche, levels = c(1, 2, 3, 4, 5, 6), labels = c("No se despierta en las noches",
"Menos de 30 minutos", "De 30-60 minutos", "De 1-2 horas", "De 2-4 horas", "Más de 5 horas"))
dat$Problema_dormirNoche <- factor(dat$Problema_dormirNoche, levels = c(1, 2, 3), labels = c("No se despierta en las noches", "Sí", "No"))
dat$Tan_descansado <- factor(dat$Tan_descansado, levels = c(1, 2, 3, 4), labels = c("Nada descansado",
"Poco descansado", "Algo descansado", "Muy descansado"))
dat[, 31:36] <- lapply(dat[, 31:36], factor, levels = c(1, 2, 3, 4), labels = c("Nunca",
"1 vez por semana", "3 veces o menos a la semana", "Más de 4 veces a la semana"))
dat$Horas_duermeDia <- factor(dat$Horas_duermeDia, levels = c(1, 2, 3, 4, 5), labels = c("No duerme durante el día",
"Menos de 1 hora", "De 1-2 horas", "De 2-4 horas", "5 o más horas"))
BD <- as.data.frame(dat)
str(BD)
#DESCRIPTIVE STATISTICS FOR FI#
#Frequency distribution FI
FI1_table <- table(BD$Who_answers, exclude=NULL)
FI2_table <- table(BD$age, exclude=NULL)
FI3_table <- table(BD$gender, exclude=NULL)
FI4_table <- table(BD$diagnosis, exclude=NULL)
FI1_table
FI2_table
FI3_table
FI4_table
prop.table(FI1_table)
prop.table(FI2_table)
prop.table(FI3_table)
prop.table(FI4_table)
gender_diagnosis <- table(BD$gender, BD$diagnosis) #Falta agregar más correlaciones interesantes
rownames(gender_diagnosis) <- c("Mujer", "Hombre")
gender_diagnosis
prop.table(gender_diagnosis)
age_diagnosis <- table (BD$age, BD$diagnosis)
age_diagnosis
prop.table(age_diagnosis)
#Central tendency FI
CT_FI <- import("Respuestas Prueba piloto para R 4.xlsx") %>%
as.data.frame() %>%
dplyr::select(1:4) %>%
print()
stat.desc(CT_FI, norm = TRUE)
#Descriptive statistics in graphs FI
#Q1
HistoFI1 <-ggplot(BD,aes(Who_answers)) #Crear Histograma para variable única
HistoFI1 + geom_histogram(binwidth=0.4) + labs(x="Quién responde este cuestionario", y="Frecuencia", fill="SexL")
I have trouble generating this last ggplot. R shows me:
Error in `geom_histogram()`:
! Problem while computing stat.
ℹ Error occurred in the 1st layer.
Caused by error in `setup_params()`:
! `stat_bin()` requires a continuous x aesthetic
✖ the x aesthetic is discrete.
ℹ Perhaps you want `stat="count"`?
Run `rlang::last_error()` to see where the error occurred.
> rlang::last_error()
<error/rlang_error>
Error in `geom_histogram()`:
! Problem while computing stat.
ℹ Error occurred in the 1st layer.
Caused by error in `setup_params()`:
! `stat_bin()` requires a continuous x aesthetic
✖ the x aesthetic is discrete.
ℹ Perhaps you want `stat="count"`?
---
Backtrace:
1. base (local) `<fn>`(x)
2. ggplot2:::print.ggplot(x)
4. ggplot2:::ggplot_build.ggplot(x)
5. ggplot2:::by_layer(...)
12. ggplot2 (local) f(l = layers[[i]], d = data[[i]])
13. l$compute_statistic(d, layout)
14. ggplot2 (local) compute_statistic(..., self = self)
15. self$stat$setup_params(data, self$stat_params)
16. ggplot2 (local) setup_params(..., self = self)
Also, I tried this code:
ggplot(BD,aes(Who_answers))+
geom_histogram(aes(y = ..density..), binwidth=0.4, color = "#000000", fill = "#0099F8") +
geom_density(color = "#000000", fill = "#F85700", alpha = 0.6) +
labs(x="Quién responde este cuestionario", y="Frecuencia", fill="SexL")
But R shows me this:
Error in `geom_histogram()`:
! Problem while computing stat.
ℹ Error occurred in the 1st layer.
Caused by error in `setup_params()`:
! `stat_bin()` requires a continuous x aesthetic
✖ the x aesthetic is discrete.
ℹ Perhaps you want `stat="count"`?
Run `rlang::last_error()` to see where the error occurred.
> rlang::last_error()
<error/rlang_error>
Error in `geom_histogram()`:
! Problem while computing stat.
ℹ Error occurred in the 1st layer.
Caused by error in `setup_params()`:
! `stat_bin()` requires a continuous x aesthetic
✖ the x aesthetic is discrete.
ℹ Perhaps you want `stat="count"`?
---
Backtrace:
1. base (local) `<fn>`(x)
2. ggplot2:::print.ggplot(x)
4. ggplot2:::ggplot_build.ggplot(x)
5. ggplot2:::by_layer(...)
12. ggplot2 (local) f(l = layers[[i]], d = data[[i]])
13. l$compute_statistic(d, layout)
14. ggplot2 (local) compute_statistic(..., self = self)
15. self$stat$setup_params(data, self$stat_params)
16. ggplot2 (local) setup_params(..., self = self)
I am relatively new to R and its language.
Is there any way you could help me understand the error and help me fix it?
Thank you so much in advance!

Related

Add text over the bars in text of barplot

The advance of my code is (MWE) :
# https://www.kaggle.com/kaggle/kaggle-survey-2017/data
#### Analisis primario del dataset ####
response <- read.csv(file = "multipleChoiceResponses.csv",na.strings = "")
# seleccionamos solo algunas variables :
Variables <- c("GenderSelect","Country","Age","CurrentJobTitleSelect","MLToolNextYearSelect","LanguageRecommendationSelect","FormalEducation",
"FirstTrainingSelect","EmployerIndustry")
# Mantenemos en memoria solo las variables seleecionadas :
response <- response[,Variables]
# Por un tema de cantidades solo nos quedamos con M y F
Response <- response[response$GenderSelect == "Male" | response$GenderSelect == "Female",]
# agrego una columna para los continenetes (continent) a donde pertenecen los paises (Country)
library(countrycode)
Response$continent <- countrycode(sourcevar = Response[, "Country"],
origin = "country.name",
destination = "continent")
# Convertimos a factor esta nueva variable
Response$continent <- as.factor(Response$continent)
# Eliminamos las filas con elementos NA
Response <- Response[complete.cases(Response), ]
# Enumeramos todas las filas de manera adecuada
rownames(Response) <- 1:nrow(Response)
Response <- droplevels(Response)
bp_Continent <- barplot(table(Response$continent),
main = "Distribucion de DS por continentes",
ylim = c(0,3500)
)
# Add GenderSelect proportion by continent in label argument ("BLABLABLA")
text(x = bp_Continent, y = table(Response$continent), label = "BLABLABLA", pos = 3, cex = 0.8, col = "red")
Basically, the script loads the data, chooses some of the variables, creates a new variable (continent), to finally clean the data. The next thing to do is create a barplot, placing the proportion of men and women on top of the bars
What I am looking to do is change the "BLABLABLA" to the proportion between men and women (GenderSelect variable) by continent.
My question is not at all similar to :
How to display the frequency at the top of each factor in a barplot in R
Because what interests me is the calculation of the proportion and the impression above the bars.
After reading Rui's answer,I thought of another solution .
first a function to calculate the ratio of men and women (by continent) and then sapply .
CreaEtiq <- function(conti){
NumHContin <- dim(Response[Response$GenderSelect=="Male" & Response$continent==conti,])[1]
NumMACntin <- dim(Response[Response$GenderSelect=="Female" & Response$continent==conti,])[1]
return(round(NumHContin/NumMACntin,2))
}
EtiquetaBarPlot <- sapply(levels(Response$continent),CreaEtiq)
And to finish:
bp_Continent <- barplot(table(Response$continent),
main = "Distribucion de DS por continentes",
ylim = c(0,3500)
)
text(x = bp_Continent, y= table(Response$continent),
label = paste("H/M = ", EtiquetaBarPlot) ,
pos = 3, cex = 0.8, col = "red")
obtaining the following graph
The code below uses a made up data set, created in the end.
Once the proportions computed, all it is needed is to pass them function text, argument label.
Compute the proportions.
tbl <- table(Response$continent)
xt <- xtabs( ~ GenderSelect + continent, Response)
prop <- sweep(xt, 2, tbl, `/`)
Now plot the bars. The labels are the proportions of "Male".
bp_Continent <- barplot(tbl,
main = "Distribucion de DS por continentes",
ylim = c(0, 3500)
)
text(x = bp_Continent, y = tbl,
label = round(prop[2, ], 2),
pos = 3, cex = 0.8, col = "red")
Other labels could be, for instance, these:
sprintf("F: %1.2f/M: %1.2f", prop[1,], prop[2,])
Data creation code.
set.seed(1234)
n <- 5e3
GenderSelect <- c("Male", "Female")
GenderSelect <- sample(GenderSelect, n, TRUE)
continent <- c("Africa", "Americas", "Asia", "Europa", "Oceania")
continent <- sample(continent, n, TRUE, prob = c(1, 20, 14, 16, 2))
Response <- data.frame(GenderSelect, continent)

Separate factor into columns R

I am trying to separate (tidyr) line_text into separate words so it's one word per column.
Data:
structure(list(ID = c(140L, 233L, 233L),
pdf_name = structure(c(1L,
2L, 2L), .
Label = c("GBD2016_2_1255_Venezuela_MoH_Epi_2012_9.pdf",
"GBD2016_2_1351_Venezuela_MoH_Epi_2014_44.pdf"),
class = "factor"),
keyword = c("SEGÚN GRUPOS", "SEGÚN GRUPOS", "SEGÚN GRUPOS"
), line_text = list("2000 Gráfico 2 . CASOS DE MALARIA SEGÚN GRUPOS DE EDAD Y SEXO, EPIDEMIOLÓGICA 9 Año 2012",
"GRÁFICO 2. CASOS DE MALARIA SEGÚN GRUPOS DE EDAD Y SEXO, HASTA",
"GRÁFICO 2. CASOS DE SEGÚN GRUPOS"),
.Names = c("ID", "pdf_name", "keyword",
"page_num", "line_num", "line_text", "token_text"), row.names = c(NA,
-3L), class = "data.frame")
Coded used:
numcols<- make.unique(c(rep("word",10, sep = " ")) )
df<- reportdiagn%>%
(separate(reportdiagn$line_text,
into = numcols,
sep = ("")))
I get the following error and can't work out how to fix it.
`Error in UseMethod("separate_") :
no applicable method for 'separate_' applied to an object of class "factor
The data you pasted isn't quite right. Might be good to try that again - but I have tried to reproduce your data anyway. It might not be exactly the same. I have set linetext to be a character string - but I think the code below works in either character or factor.
In select(), you don't need to reference the data frame - the %>% already does that, you just need the name of the variable without quotes. Also, your sep needs to be a space or \\b for a word boundary.
ID <- c(140, 233, 233)
pdf_name <- factor(c(1, 2, 2),
labels = c(
"GBD2016_2_1255_Venezuela_MoH_Epi_2012_9.pdf",
"GBD2016_2_1351_Venezuela_MoH_Epi_2014_44.pdf")
)
keyword <- c("SEGÚN GRUPOS", "SEGÚN GRUPOS", "SEGÚN GRUPOS")
line_text <- c("2000 Gráfico 2 . CASOS DE MALARIA SEGÚN GRUPOS DE EDAD Y SEXO, EPIDEMIOLÓGICA 9 Año 2012",
"GRÁFICO 2. CASOS DE MALARIA SEGÚN GRUPOS DE EDAD Y SEXO, HASTA",
"GRÁFICO 2. CASOS DE SEGÚN GRUPOS.")
reportdiagn <- data.frame(ID, pdf_name, keyword, line_text)
numcols<- make.unique(c(rep("word",10 )) )
df <- reportdiagn %>%
separate(line_text,
into = numcols,
sep = " ")
This produces some NA values where there's less than 10 words and truncates when there's more. I assume you're expecting that.

how to debug R scripts

I´ve made a little simulator to teach the basics of population dynamics under stochasticity. This is a simple viz for the Ricker equation. It works in linux, however, I get a criptic error while running it under other environment (macos, win).
So, I wonder what my best options are to debug this error:
<simpleError in is.list(x): object of type 'closure' is not subsettable>
here is the code. I suspect on the plotly library in R... any hint?
# Ejercicio para ensenar curvas de crecimiento poblacional
# _author_ = horacio.samaniego#gmail.com
# _date_ = August 2018
# Check whether required packages are installed
list.of.packages <- c("manipulateWidget", "plotly")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
# librerias requeridas
library(manipulateWidget)
library(plotly)
# Simulador
crecLogistico <- function(t=80,r=1,n0=10,K=60,d=0){
# Simulacion de crecimiento poblacional con tasa de crecimiento (r) variable
# que simula variabilidad (estocasticidad ambiental)
# t, numero de generaciones a simular
# r, tasa intrinsica de crecimiento
# n0, abundancia inicial
# K, capacidad de carga
# d, estocasticidad
N <- matrix(n0,ncol=t,nrow=1) # vector con tamano poblacional (abundancia) inicial
R <- matrix(NA,ncol=t,nrow=1) # vector con tasas de crecimiento
for(i in 1:t) {
if(d>0) R[i] <- rnorm(1,mean=r,sd=d) else R[i] <- r
# N[i+1] <- N[i]+N[i]*R[i]*(1-N[i]/K) # Logistica
N[i+1] <- N[i]*exp(R[i]*(1 - N[i]/K)) # Ricker
}
res <- matrix(c(1:t,N[-(t+1)],R),ncol=3)
res <- as.data.frame(res)
names(res) <- c("t","Abundancia","r")
return(res)
}
## Plot de Crecimiento Logistico
if ( require(plotly) ) {
manipulateWidget({
# Modela abundancias
res = crecLogistico(n0=n0,t=t,r=r,K=K,d=d)
# Colecta informacion para construir mapa logistico
mapa = data.frame("N_1"=res$Abundancia[-t],"N_0"=res$Abundancia[-1])
combineWidgets( # crea ventanas donde plotear (proporcion 3:1). Una grande a la izq y otra con 2 lineas a la dcha (1:1)
ncol = 2,colsize=c(3,1),
# Evolucion temporal de poblacion
p = plot_ly(res[rango[1]:rango[2],],x=~t,y=~Abundancia,type="scatter",
mode="lines+markers",line=~r,color=~r), # %>%
# agrega linea para remarcar abundancia = 0
add_segments(p, x = rango[1], xend = rango[2], y = 0, yend = 0,mode="lines") %>%
# muestra escocasticidad a cada tiempo segun color
colorbar(title="Tasa de Crecimiento") %>%
layout(title="Crecimiento Logistico",showlegend = FALSE,yaxis=list(zerolinecolor=toRGB("red"))),
combineWidgets(
ncol = 1,
# histograma de estocasticidad a lo largo de todo el tiempo
plot_ly(res,x=~r,type="histogram") %>%
layout(title="Tasa de Crecimiento"),
# mapa logistico, muestra estados estacionarios en primer orden, de t-a a t
plot_ly(mapa,x=~N_1,y=~N_0,type="scatter",mode="markers") %>%
add_segments(x=0,xend=max(res$Abundancia),y=0,yend=max(res$Abundancia)) %>%
layout(title="Mapa Logistico", xaxis = list(title = "abundancia (t)"),
yaxis = list(title="abundancia (t-1)"))
)
)
},
n0 = mwNumeric(100, min = 2, step = 1 , label = "Poblacion Inicial"),
t = mwNumeric(100, min = 2, step = 1 , label = "Generaciones (t)"),
r = mwNumeric(0.9, min = -4, step = 0.05 , label = "Tasa de Crecimiento (r)"),
K = mwNumeric(60, min = 5, step = 2, label = "Capacidad de Carga"),
d = mwNumeric(0.05, min = 0, step = 0.05 ,label = "Estocasticidad"),
rango = mwSlider(0, t, c(1, t),label="Generaciones a Visualizar")
)
}
will report... thanks a bunch!!
Ok, I've got the issue... the problem is that plotly in R (or rstudio) for linux is not too picky about the 'line' flag in plot_ly, which macos strictly enforces.
I unfortunately have no training in debugging code and would not be able diagnose this using the standard tools mentioned here (will learn though!)
recasting the line to this:
plot_ly(res[rango[1]:rango[2],],x=~t,y=~Abundancia,type="scatter", mode="lines+markers",color=~r) %>%
did all the trick.
Thanks,
I'll copy my equation explorer here just in case:
# Ejercicio para ensenar curvas de crecimiento poblacional
# _author_ = horacio.samaniego#gmail.com
# _date_ = August 2018
# Check whether required packages are installed
list.of.packages <- c("manipulateWidget", "plotly")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
# librerias requeridas
library(manipulateWidget)
library(plotly)
# Simulador
crecLogistico <- function(t=80,r=1,n0=10,K=60,d=0){
# Simulacion de crecimiento poblacional con tasa de crecimiento (r) variable
# que simula variabilidad (estocasticidad ambiental)
# t, numero de generaciones a simular
# r, tasa intrinsica de crecimiento
# n0, abundancia inicial
# K, capacidad de carga
# d, estocasticidad
N <- matrix(n0,ncol=t,nrow=1) # vector con tamano poblaciona (abundancia) inicial
R <- matrix(NA,ncol=t,nrow=1) # vector con tasas de crecimiento
for(i in 1:t) {
if(d>0) R[i] <- rnorm(1,mean=r,sd=d) else R[i] <- r # adds stochasticity
# N[i+1] <- N[i]+N[i]*R[i]*(1-N[i]/K) # Logistica
N[i+1] <- N[i]*exp(R[i]*(1 - N[i]/K)) # Ricker
}
res <- matrix(c(1:t,N[-(t+1)],R),ncol=3)
res <- as.data.frame(res)
names(res) <- c("t","Abundancia","r")
return(res)
}
## Plot de Crecimiento Logistico
if ( require(plotly) ) {
manipulateWidget({
# Modela abundancias
res = crecLogistico(n0=n0,t=t,r=r,K=K,d=d)
# Colecta informacion para construir mapa logistico
mapa = data.frame("N_1"=res$Abundancia[-t],"N_0"=res$Abundancia[-1])
combineWidgets( # crea ventanas donde plotear (proporcion 3:1). Una grande a la izq y otra con 2 lineas a la dcha (1:1)
ncol = 2,colsize=c(3,1),
# Evolucion temporal de poblacion
plot_ly(res[rango[1]:rango[2],],x=~t,y=~Abundancia,type="scatter", mode="lines+markers",color=~r) %>%
# # muestra escocasticidad a cada tiempo segun color
colorbar(title="Tasa de Crecimiento") %>%
layout(title="Crecimiento Logistico",showlegend = FALSE,yaxis=list(zerolinecolor=toRGB("red"))),
combineWidgets(
ncol = 1,
# histograma de estocasticidad a lo largo de todo el tiempo
plot_ly(res,x=~r,type="histogram") %>%
layout(title="Tasa de Crecimiento"),
# mapa logistico, muestra estados estacionarios en primer orden, de t-a a t
plot_ly(mapa,x=~N_1,y=~N_0,type="scatter",mode="markers") %>%
add_segments(x=0,xend=max(res[[2]]),y=0,yend=max(res[[2]])) %>%
layout(title="Mapa Logistico", xaxis = list(title = "abundancia (t)"),
yaxis = list(title="abundancia (t-1)"),showlegend = FALSE)
)
)
},
n0 = mwNumeric(100, min = 2, step = 1 , label = "Poblacion Inicial"),
t = mwNumeric(100, min = 2, step = 1 , label = "Generaciones (t)"),
r = mwNumeric(0.9, min = -4, step = 0.05 , label = "Tasa de Crecimiento (r)"),
K = mwNumeric(60, min = 5, step = 2, label = "Capacidad de Carga"),
d = mwNumeric(0.05, min = 0, step = 0.05 ,label = "Estocasticidad"),
rango = mwSlider(0, t, c(1, t),label="Generaciones a Visualizar")
)
}

Update the axis labels with rChart

I just discovered rChart and googleVis and i want to thank developers for their job.
My problem is simple, i want to add a variable label to my axis for nplot?
I also want to know if it's possible to add variable like sizevar and colorvar of gvisBubbleChart to nplot ?
Thank you.
library(rCharts)
VehiculeFunction <- function(data, gamme, absciss, ordinate){
# Aim: Permet de visualiser les données sous la forme de nuages de points
# croisant GMF*Coût, Ratio K * Ratio Coût ou bien GMF*Ratio en
# choisissant la gamme qu'on désire
# Input: data.frame avec notamment GAMME, PROJET, PERIMETRE, NITG, GMF.24,
# Cout.24 et libele
# Output: Graphique avec le croisement choisi ainsi que le libellé étiquetté
# sur le point qu'on voudra identifier
if(absciss == "GMF.24"){
my.data <- data[data$RANG_NITG_PROJET_K %in% c(1, 2, 3),]
} else if(absciss == "Ratio.K") {
my.data <- data[data$RANG_NITG_PROJET_C %in% c(1, 2, 3),]
}
my.data2 <- my.data[my.data$GAMME == gamme,]
X <- my.data2[[absciss]]
Y <- my.data2[[ordinate]]
SIZEVAR <- my.data2$Ratio.K
df <- data.frame(X,Y,SIZEVAR)
plot <- nPlot(x = "X", y = "Y", size = "SIZEVAR", data = df, type = "scatterChart")
plot$xAxis(axisLabel = 'X')
plot
}
VehiculeFunction(data.vehicule, gamme = "M1", "GMF.24", "Cout.24")
Usage:
gvisBubbleChart(data, idvar = "", xvar = "", yvar = "",
colorvar = "", sizevar = "",
options = list(), chartid)
E.G.
## Set color and size
bubble2 <- gvisBubbleChart(Fruits, idvar="Fruit", xvar="Sales", yvar="Expenses",
colorvar="Location", sizevar="Profit",
options=list(hAxis='{minValue:75, maxValue:125}'))
SEE http://www.inside-r.org/packages/cran/googleVis/docs/gvisBubbleChart

rChart in a function

I need help about this function, i try it with plot (R) and googleVis, it works perfectly; now i want to use rChart but when i launch the functions it returned me a simple grid with no points on it.
VehiculeFunction <- function(data, gamme, absciss, ordinate){
# Aim: Permet de visualiserles données sous la forme de nuages de points
# croisant GMF*Coût, Ratio K * Ratio Coût ou bien GMF*Ratio en
# choisissant la gamme qu'on désire
# Input: data.frame avec notamment GAMME, PROJET, PERIMETRE, NITG, GMF.24,
# Cout.24 et libele
# Output: Graphique avec le croisement choisi ainsi que le libellé étiquetté
# sur le point qu'on voudra identifier
if(absciss == "GMF.24"){
my.data <- data[data$RANG_NITG_PROJET_K %in% c(1, 2, 3),]
} else if(absciss == "Ratio.K") {
my.data <- data[data$RANG_NITG_PROJET_C %in% c(1, 2, 3),]
}
my.data2 <- my.data[my.data$GAMME == gamme,]
X <- my.data2[[absciss]]
Y <- my.data2[[ordinate]]
plot <- nPlot(Y ~ X, data = data, type = 'scatterChart')
plot
}
VehiculeFunction(data.vehicule2, gamme = "M1", "GMF.24", "Cout.24")
data.vehicule2 is a dataframe.
Thank you.
EDIT:
In the dataframe, i have 18 variables and 36 000. This code works perfectly, but my problem is the plot appearance.
library(rCharts)
VehiculeFunction <- function(data, gamme, absciss, ordinate){
# Aim: Permet de visualiser les données sous la forme de nuages de points
# croisant GMF*Coût, Ratio K * Ratio Coût ou bien GMF*Ratio en
# choisissant la gamme qu'on désire
# Input: data.frame avec notamment GAMME, PROJET, PERIMETRE, NITG, GMF.24,
# Cout.24 et libele
# Output: Graphique avec le croisement choisi ainsi que le libellé étiquetté
# sur le point qu'on voudra identifier
if(absciss == "GMF.24"){
my.data <- data[data$RANG_NITG_PROJET_K %in% c(1, 2, 3),]
} else if(absciss == "Ratio.K") {
my.data <- data[data$RANG_NITG_PROJET_C %in% c(1, 2, 3),]
}
my.data2 <- my.data[my.data$GAMME == gamme,]
X <- my.data2[[absciss]]
Y <- my.data2[[ordinate]]
SIZEVAR <- my.data2$Ratio.K
df <- data.frame(X,Y,SIZEVAR)
plot <- nPlot(x = "X", y = "Y", size = "SIZEVAR", data = df, type = "scatterChart")
plot
}
VehiculeFunction(data.vehicule, gamme = "M1", "GMF.24", "Cout.24")
Could i add a size variable because on the plot nothing appears, how could i have my variable "GMF.24" and "Cout.24" on the axis ? and the last question, is it possible to add label when i place the mouse above a point ?
Thank you in advance.

Resources