if else在ggplot中添加一个额外的图层

时间:2014-04-07 14:28:54

标签: r if-statement ggplot2

说我想在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的对象不等于表达式&#34;没有值&#34;。

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)

4 个答案:

答案 0 :(得分:29)

这是使用ggplot2 2.1.0完成的。我认为你可以完成OP所希望的,只需切换括号,使它们包含整个if语句。

以下是根据SwtichT还是F来添加水平线的示例。首先,条件为TRUE

library(ggplot2)

df<-data.frame(x=1:10,y=11:20)
Switch=T

ggplot(df,aes(x,y))+
{if(Switch)geom_hline(yintercept=15)}+
  geom_point()

enter image description here

现在,同样的事情,但条件是FALSE

df<-data.frame(x=1:10,y=11:20)
Switch=F

ggplot(df,aes(x,y))+
{if(Switch)geom_hline(yintercept=15)}+
  geom_point()

enter image description here

答案 1 :(得分:26)

您看到的是语法错误。我能想到的最强大的方法是:

tmp.data<-c(1,2,3) 
if(tmp.data[1]!="no value") {
   p = p + geom_point()
}
p + geom_line()

所以你在序列中组合对象p,只在if语句产生geom_point()时添加TRUE

答案 2 :(得分:4)

ggplot2 book之后,您可以创建一个返回列表的函数。任何NULL组件都将被忽略。

a =[31,41,59,26,41,58]
input = input("Enter number : ")
for i in range(1,len(a),1) :
    if input == a[i] :
       print(i)

library(ggplot2)
library(ggplot2movies)

# 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))
}))

# create function to add points conditionally
# If the list contains any NULL elements, they’re ignored. 
my_plot <- function(point = FALSE){
    list(
        geom_line(),
        if (point) 
            geom_point()
    )

}

p <- ggplot(mry, aes(x=year, y=number, group=rating))
p + my_plot()

reprex package(v0.3.0)于2020-02-25创建

答案 3 :(得分:1)

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))
}))

tmp.data<-c(1,2,3) # in this case the condition is fulfilled

p <- ggplot(mry, aes(x=year, y=number, group=rating))

# this won't "loop through" the data points but it's what you asked for
if (tmp.data[1]!="no value") {
  p <- p + geom_point() + geom_line()
} else {
  p <- p + geom_line()
}
p

g1

但也许这更像你真正想要的?

mry$rating <- factor(mry$rating)
p <- ggplot(mry, aes(x=year, y=number, group=rating))
p <- p + geom_line()
p <- p + geom_point(data=mry[!(mry$rating %in% tmp.data),], 
                    aes(x=year, y=number, group=rating, color=rating), size=2)
p <- p + scale_color_brewer()
p

g2