如何将 ggplot2 图的标签包装器放入函数中?
How to put label wrapper for ggplot2 plots inside function?
我正在尝试编写一个函数来生成 ggplot 图。 Heroka helped me out with getting my function to work. But now I have a question on how I can build an auto label wrapper inside this function. I saw a similar and very useful question 在此,但相同的代码在函数内部不起作用。
我正在重现我当前用于生成 ggplot 的函数:
myfunction = function (data, Variable1) {
ggplot(data=data, aes_string(sprintf("factor(%s)",Variable1), "Variable2"))+
geom_boxplot(fill="grey", colour="black")+
labs(title = sprintf("%s and Variable2", Variable1)) +
labs (x = Variable1, y = "Variable2")
}
当我尝试执行以下操作时(请注意,我试图将标签包装在 ggplot 的调用中:
myfunction = function (data, Variable1) {
ggplot(data=data, aes_string(sprintf("factor(%s)",Variable1), "Variable2"))+
geom_boxplot(fill="grey", colour="black")+
labs(title = sprintf("%s and Variable2", Variable1)) +
labs (x = Variable1, y = "Variable2")+
# adding the line to wrap the labels
scale_x_discrete(labels = function(x) str_wrap(x, width = 10))
}
它不起作用(情节仍然生成,但好像我没有插入那行)
我很困惑:
我想我需要将该附加行中的 x 更改为我的实际变量?在这种情况下是 Variable1
,但是当我这样做时它仍然不起作用
由于我们仍在 ggplot 调用中,这又是环境问题吗? (例如我们需要在函数中使用 aes_string
而不是 aes ,因为我们处于本地环境中)。但是,在这个函数中添加了一个额外的函数,我不确定我们在哪个环境中以及如何实现标签换行。
我会说它看起来一样,因为没有什么可以换行,0
和 1
太短无法换行。我把因子变长了,所以 str_wrap
函数可以使用,在这里你可以看到它工作正常:
set.seed(123)
library(stringr)
library(ggplot2)
valstr <- c("This is value 0","This is value 1")
dat <- data.frame(Variable2=rnorm(100),
Variable1=valstr,
Variable3=sample(valstr,100,T))
myfunction_1 = function (data, Variable1) {
ggplot(data=data, aes_string(sprintf("factor(%s)",Variable1), "Variable2"))+
geom_boxplot(fill="grey", colour="black")+
labs(title = sprintf("%s and Variable2", Variable1)) +
labs (x = Variable1, y = "Variable2")
}
myfunction_2 = function (data, Variable1) {
ggplot(data=data, aes_string(sprintf("factor(%s)",Variable1), "Variable2"))+
geom_boxplot(fill="grey", colour="black")+
labs(title = sprintf("%s and Variable2", Variable1)) +
labs (x = Variable1, y = "Variable2")+
# adding the line to wrap the labels
scale_x_discrete(labels = function(x) str_wrap(x, width = 10))
}
p1 <- myfunction_1(dat,"Variable1")
p2 <- myfunction_2(dat,"Variable3")
grid.arrange(p1, p2, ncol=2)
产量:
我正在尝试编写一个函数来生成 ggplot 图。 Heroka helped me out with getting my function to work. But now I have a question on how I can build an auto label wrapper inside this function. I saw a similar and very useful question 在此,但相同的代码在函数内部不起作用。
我正在重现我当前用于生成 ggplot 的函数:
myfunction = function (data, Variable1) {
ggplot(data=data, aes_string(sprintf("factor(%s)",Variable1), "Variable2"))+
geom_boxplot(fill="grey", colour="black")+
labs(title = sprintf("%s and Variable2", Variable1)) +
labs (x = Variable1, y = "Variable2")
}
当我尝试执行以下操作时(请注意,我试图将标签包装在 ggplot 的调用中:
myfunction = function (data, Variable1) {
ggplot(data=data, aes_string(sprintf("factor(%s)",Variable1), "Variable2"))+
geom_boxplot(fill="grey", colour="black")+
labs(title = sprintf("%s and Variable2", Variable1)) +
labs (x = Variable1, y = "Variable2")+
# adding the line to wrap the labels
scale_x_discrete(labels = function(x) str_wrap(x, width = 10))
}
它不起作用(情节仍然生成,但好像我没有插入那行) 我很困惑:
我想我需要将该附加行中的 x 更改为我的实际变量?在这种情况下是
Variable1
,但是当我这样做时它仍然不起作用由于我们仍在 ggplot 调用中,这又是环境问题吗? (例如我们需要在函数中使用
aes_string
而不是 aes ,因为我们处于本地环境中)。但是,在这个函数中添加了一个额外的函数,我不确定我们在哪个环境中以及如何实现标签换行。
我会说它看起来一样,因为没有什么可以换行,0
和 1
太短无法换行。我把因子变长了,所以 str_wrap
函数可以使用,在这里你可以看到它工作正常:
set.seed(123)
library(stringr)
library(ggplot2)
valstr <- c("This is value 0","This is value 1")
dat <- data.frame(Variable2=rnorm(100),
Variable1=valstr,
Variable3=sample(valstr,100,T))
myfunction_1 = function (data, Variable1) {
ggplot(data=data, aes_string(sprintf("factor(%s)",Variable1), "Variable2"))+
geom_boxplot(fill="grey", colour="black")+
labs(title = sprintf("%s and Variable2", Variable1)) +
labs (x = Variable1, y = "Variable2")
}
myfunction_2 = function (data, Variable1) {
ggplot(data=data, aes_string(sprintf("factor(%s)",Variable1), "Variable2"))+
geom_boxplot(fill="grey", colour="black")+
labs(title = sprintf("%s and Variable2", Variable1)) +
labs (x = Variable1, y = "Variable2")+
# adding the line to wrap the labels
scale_x_discrete(labels = function(x) str_wrap(x, width = 10))
}
p1 <- myfunction_1(dat,"Variable1")
p2 <- myfunction_2(dat,"Variable3")
grid.arrange(p1, p2, ncol=2)
产量: