[英]Labelling x- and y-axis by the corresponding variable's name in R
this sounds like a very trivial question at first, but no one managed to help me thus far, hence I'm reaching out to you all.起初这听起来像是一个非常微不足道的问题,但到目前为止没有人能够帮助我,因此我正在与大家联系。
I'd like to do the following:我想做以下事情:
I'm writing a simple function that allows me to plot two variables against each other, with a third variable coloring the observation points (depending on the corresponding value of the color variable).我正在编写一个简单的 function ,它允许我对 plot 两个变量进行对比,第三个变量为观察点着色(取决于颜色变量的相应值)。 The code looks like that:
代码如下所示:
scatterplot <- function(data_used, x.variable, y.variable, color.variable) {
ggplot(data_used, aes(x=x.variable, y = y.variable)) +
geom_point(aes_string(color = color.variable))
}
scatterplot(data_used = example_data, x.variable = example_data$education,
y.variable = example_data$wages,
color.variable = example_data$sex)
What I would like R to do now is to label the x- and y-axis (respectively) by the corresponding variable's name that I decide to be plotted.我现在想要 R 做的是 label 由我决定绘制的相应变量名称(分别)的 x 轴和 y 轴。 In this example here, x-axis would be 'education', y-axis would be 'wages'.
在此示例中,x 轴将是“教育”,y 轴将是“工资”。
I tried to simply put + labs (x = x.variable, y = y.variable)
and it doesn't work (when doing that, R labels the axes by the variable values.), By default.我试图简单地将
+ labs (x = x.variable, y = y.variable)
,但它不起作用(这样做时,R 通过变量值标记轴。),默认情况下。 R just names the axes "x.variable" and "y.variable". R 只是将轴命名为“x.variable”和“y.variable”。
Can someone help me achieve what I'm trying to do?有人可以帮助我实现我想要做的事情吗?
Best regards,此致,
xifrix西弗里克斯
I'm not sure the quasi-quotation stuff is 100% necessary in hindsight, but this is the pattern I use for similar needs:事后看来,我不确定准引用的东西是 100% 必要的,但这是我用于类似需求的模式:
my_scatterplot <- function(data, x, y){
.x = rlang::enquo(x)
.y = rlang::enquo(y)
data %>%
ggplot(aes(x = x, y = y))+
geom_point()+
labs(x = .x,
y = .y)
}
Let me know if it doesn't work for you, it should though.让我知道它是否不适合你,但它应该。 edit: Should add after DaveArmstrong's answer, the function would be called without quotes for the x / y variable eg
编辑:应该在 DaveArmstrong 的回答之后添加,function 将在不带引号的情况下调用 x / y 变量,例如
diamonds %>% my_scatterplot(price, table)
jpenzer's answer is a good one. jpenzer 的回答很好。 Here it is without the quasi-quotation stuff.
这里没有准引用的东西。
scatterplot <- function(data_used, x.variable, y.variable, color.variable) {
ggplot(data_used, aes_string(x=x.variable, y = y.variable)) +
geom_point(aes_string(color = color.variable)) +
labs(x=x.variable, y=y.variable, colour=color.variable)
}
mtcars %>%
mutate(am = as.factor(am)) %>%
scatterplot(., x.variable = "hp",
y.variable = "mpg",
color.variable = "am")
To pass a column name in the function you could use double curly braces {{...}}
around the desired column name in the function body:要在 function 中传递列名,您可以在 function 正文中的所需列名周围使用双花括号
{{...}}
:
library(dplyr)
library(ggplot2)
scatterplot <- function(data_used, x.variable, y.variable, color.variable) {
ggplot(data_used, aes_string({{x.variable}}, {{y.variable}})) +
geom_point(aes_string(color = {{color.variable}})) +
labs(x=x.variable, y=y.variable, colour=color.variable)
}
scatterplot(mtcars %>% mutate(am = as.factor(am)), x.variable = "mpg",
y.variable = "hp",
color.variable = "am")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.