say Я хочу построить два слоя в ggplot, один из которых содержит точки, а другой - строки, если выполняется определенный критерий.
Код без критериев может выглядеть так:
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()
теперь условие для построения точек, а не только строк будет, что объект с именем tmp.data не равен выражению "no value".
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()
не удается....
Error: unexpected '}' in:
"p+
if(tmp.data[1]!="no value"){ geom_point()+}"
geom_line() geom_line:
stat_identity:
position_identity: (width = NULL, height = NULL)