I try to run ggplot2
but it does not work in the following function foo()
. Why?
foo <- function(aaa=TRUE) {
df<-data.frame(x=c(10,100,1000,10,100,1000),
y=c(1100,220000,33000000,1300,240000,36000000),
group=c("1","1","1","2","2","2")
)
if (aaa==TRUE) {
ggplot2::ggplot(df, aes(x = x, y = y, shape = group)) +
ggplot2::geom_line(position = position_dodge(0.2)) + # Dodge lines by 0.2
ggplot2::geom_point(position = position_dodge(0.2), size = 4)+ # Dodge points by 0.2
ggplot2::scale_y_log10()+
ggplot2::scale_x_log10()
}
if (aaa==FALSE) {
ggplot2::ggplot(df, aes(x = x, y = y, shape = group)) +
ggplot2::geom_line(position = position_dodge(0.2)) + # Dodge lines by 0.2
ggplot2::geom_point(position = position_dodge(0.2), size = 4)+ # Dodge points by 0.2
ggplot2::scale_y_log10()
# ggplot2::scale_x_log10()
}
}
I cannot understand why foo(T)
does not work but foo(F)
work.
Note that foo(T) runs the following codes, and it works outside the function foo
.
df<-data.frame(x=c(10,100,1000,10,100,1000),
y=c(1100,220000,33000000,1300,240000,36000000),
group=c("1","1","1","2","2","2")
)
ggplot2::ggplot(df, aes(x = x, y = y, shape = group)) +
ggplot2::geom_line(position = position_dodge(0.2)) + # Dodge lines by 0.2
ggplot2::geom_point(position = position_dodge(0.2), size = 4)+ # Dodge points by 0.2
ggplot2::scale_y_log10()+
ggplot2::scale_x_log10()
Edit for the answer of @Ronak Shah
if we use if
statement successively, then it lost the object as the following code.
df<-data.frame(x=c(10,100,1000,10,100,1000),
y=c(1100,220000,33000000,1300,240000,36000000),
group=c("1","1","1","2","2","2")
)
aaa<-TRUE
if (aaa==TRUE) {
ggplot2::ggplot(df, aes(x = x, y = y, shape = group)) +
ggplot2::geom_line(position = position_dodge(0.2)) + # Dodge lines by 0.2
ggplot2::geom_point(position = position_dodge(0.2), size = 4)+ # Dodge points by 0.2
ggplot2::scale_y_log10()+
ggplot2::scale_x_log10()
}
.Last.value
if (aaa==FALSE) {
ggplot2::ggplot(df, aes(x = x, y = y, shape = group)) +
ggplot2::geom_line(position = position_dodge(0.2)) + # Dodge lines by 0.2
ggplot2::geom_point(position = position_dodge(0.2), size = 4)+ # Dodge points by 0.2
ggplot2::scale_y_log10()
# ggplot2::scale_x_log10()
}
.Last.value
Edit for the answer of @Jules Stuifbergen
Using return()
# General print of log scale
foo <- function(aaa=TRUE) {
df<-data.frame(x=c(10,100,1000,10,100,1000),
y=c(1100,220000,33000000,1300,240000,36000000),
group=c("1","1","1","2","2","2")
)
if (aaa==TRUE) {
return( ggplot2::ggplot(df, aes(x = x, y = y, shape = group)) +
ggplot2::geom_line(position = position_dodge(0.2)) + # Dodge lines by 0.2
ggplot2::geom_point(position = position_dodge(0.2), size = 4)+ # Dodge points by 0.2
ggplot2::scale_y_log10()+
ggplot2::scale_x_log10()
)
}
if (aaa==FALSE) {
ggplot2::ggplot(df, aes(x = x, y = y, shape = group)) +
ggplot2::geom_line(position = position_dodge(0.2)) + # Dodge lines by 0.2
ggplot2::geom_point(position = position_dodge(0.2), size = 4)+ # Dodge points by 0.2
ggplot2::scale_y_log10()
# ggplot2::scale_x_log10()
}
}