最佳答案
假设我想在 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 () 返回文章页面
Stat _ Identity:
Position _ Identity: (width = NULL,height = NULL)