Devin Hunt
Devin Hunt

Reputation: 25

ifelse statements in R

I am inside a user generated function in R and want some lines of code to be conditioned on the parameters. For example, Position is an input for the function. I want to display or not display an axis if this variable is 2 or 4. This is what I have but it doesn't seem to work. Is something like this possible in R? (see line 2 of the code)

Themes <- theme(axis.title = element_text(size=fsizes),
                  if(Position == 2 | Position == 4, axis.text.x=element_blank() , axis.text.x = element_text(size=fsizes, color = "black")), 
                  axis.text.y = element_text(size=fsizes, color = "black"), 
                  title=element_text(size=fsizes), 
                  legend.text=element_text(size=fsizes-1.5),
                  legend.key.width = unit(.1, "cm"), legend.key.height = unit(.2, "cm"),
                  legend.position = "top",
                  legend.direction = "horizontal",
                  legend.margin = unit(0, 'cm'),
                  axis.ticks = element_line(size = .2),
                  panel.grid.major = element_line(size=.2),
                  panel.grid.minor = element_line(size=.1),
                  plot.margin = unit(c(.1,.1,.1,0), 'cm')
                  )

Upvotes: 1

Views: 876

Answers (2)

jlhoward
jlhoward

Reputation: 59425

Another way to do this is to replace your second line of code:

if(Position == 2 | Position == 4, 
   axis.text.x=element_blank() , 
   axis.text.x = element_text(size=fsizes, color = "black")), 

with this:

axis.text.x= ifelse(Position==2 | Position==4, 
                    element_blank(), 
                    element_text(size=fsizes, color = "black")),

Upvotes: 1

marbel
marbel

Reputation: 7714

I believe that `if` is what you are after here. Write ?`if` for help, but the idea is:

if(condition) expression

or as in this case:

if(condition) {
  expression 1
} else {
expression 2
}


ifelse(test, cond1, cond2) 

i believe is not used much to test simple conditions in functions. of course take a look at ?ifelse anyway.

require(ggplot2)


theme_fun <- function(Position = NULL, fsizes = NULL){
  theme = theme(axis.title = element_text(size=fsizes), 
                 axis.text.y = element_text(size=fsizes, color = "black"), 
                 title=element_text(size=fsizes), 
                 legend.text=element_text(size=fsizes-1.5),
                 legend.key.width = unit(.1, "cm"), legend.key.height = unit(.2, "cm"),
                 legend.position = "top",
                 legend.direction = "horizontal",
                 legend.margin = unit(0, 'cm'),
                 axis.ticks = element_line(size = .2),
                 panel.grid.major = element_line(size=.2),
                 panel.grid.minor = element_line(size=.1),
                 plot.margin = unit(c(.1,.1,.1,0), 'cm')
  )

  if(Position == 2 | Position == 4){
    theme = theme + axis.text.x=element_blank()
  } else {
    theme = theme + axis.text.x = element_text(size=fsizes, color = "black")
  }
}

EDIT to clarify the comments

x = "hello"

# This if else construct
if(x == "hello"){
  print("como va che?")
} else {
  "hello"
}

# Is equivalent to this
`if`(x == "hello", "como va che?", "hello")

The point of the comment is to clarify that function is another way to access a function. For example, if you would like to read the help.

?`if`

Here is another example:

`+`(1, 100)

Upvotes: 2

Related Questions