[英]Looping through dataframe names and plotting in R
I have a dataframe of 12 variables and I'd like to plot exactly one variable against others using ggplot's geom_point()
.我有一个包含 12 个变量的 dataframe ,我想使用 ggplot 的
geom_point()
将 plot 与其他变量完全对比。 Wouldn't want to do it manually so i need to loop through the variables making plots.不想手动做,所以我需要循环变量制作图。 For example, I have a df like this (simplified to 4 variables for readability):
例如,我有一个这样的 df(为了便于阅读,简化为 4 个变量):
> head(df)
letters value1 value2 value3
A 1 0 10
B 3 1 9
C 6 0 8
D 76 0 7
E 13 1 6
F 58 1 5
And I'd like to produce two plots where value1
is plotted over value2
and value3
.我想制作两个图,其中
value1
绘制在value2
和value3
上。 I've tried this:我试过这个:
plts <- vector()
for (i in names(df)) {
p <- ggplot(df, aes(x=value1, y=i, fill=letters)) + geom_point())
plts <- append(plts, p)
}
but it treats the values 2 & 3 different than the value 1 and produces something like this (eg, value1 over value3): Plot of value1 over value3但它处理值 2 和 3 与值 1 不同,并产生类似这样的结果(例如 value1 over value3): Plot of value1 over value3
What should be done to improve this and achieve the goal of having the plots like this:应该做些什么来改善这一点并实现拥有这样的情节的目标:
ggplot(df, aes(x=value1, y=value3, fill=letters)) + geom_point()
I think using aes_string()
instead of aes
will give you what you want.我认为使用
aes_string()
而不是aes
会给你你想要的。 Your problem is caused by the tidyverse's use of non-standard evaluation ( NSE ).您的问题是由 tidyverse 使用非标准评估( NSE )引起的。
lapply(
names(df),
function(y) {
df %>% ggplot() + geom_point(aes_string(x="value1", y=y, colour="letters"))
}
)
giving, for example给予,例如
You can customise the first argument to lapply
to select the variables you need.您可以自定义第一个参数以将所需的变量应用于
lapply
。
That said, I think it would be easier and more robust to reformat your data frame to a more helpful layout and then create your plots...也就是说,我认为将您的数据框重新格式化为更有用的布局然后创建您的绘图会更容易和更强大......
For example,例如,
df %>%
pivot_longer(
cols=c("value2", "value3"),
names_to="Variable",
values_to="y"
) %>%
ggplot() +
geom_point(aes(x=value1, y=y, colour=letters)) +
facet_grid(rows=vars(Variable))
Giving给予
By the way, using colour=letters
is probably more informative than fill=letters
when using geom_point
.顺便说一句,在使用
geom_point
时,使用colour=letters
可能比fill=letters
提供更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.