dire che voglio tracciare due layer in ggplot, uno contenente punti e un altro contenente linee se un determinato criterio è soddisfatto.if else condition in ggplot per aggiungere un ulteriore layer
Il codice senza i criteri potrebbe essere la seguente:
library("ggplot2")
# Summarise number of movie ratings by year of movie
mry <- do.call(rbind, by(movies, round(movies$rating), function(df) {
nums <- tapply(df$length, df$year, length)
data.frame(rating=round(df$rating[1]), year = as.numeric(names(nums)), number=as.vector(nums))
}))
p <- ggplot(mry, aes(x=year, y=number, group=rating))
p +
geom_point()+
geom_line()
ora la condizione per tracciare i punti e non solo le linee sarebbero, che un oggetto chiamato tmp.data non è uguale l'espressione "no valore".
tmp.data<-c(1,2,3) # in this case the condition is fulfilled
# attempt to plot the two layers including the condition in the plotting function
p+
if(tmp.data[1]!="no value"){ geom_point()+}
geom_line()
fallisce ....
Error: unexpected '}' in:
"p+
if(tmp.data[1]!="no value"){ geom_point()+}"
geom_line() geom_line:
stat_identity:
position_identity: (width = NULL, height = NULL)
Dove è stato definito 'C'? – James
il mio male. scusa. considera le modifiche sopra – joaoal
Perché non modificare semplicemente l'ordine: 'p + geom_line() + if (tmp.data [1]! =" nessun valore ") {geom_point()}' – shadow