使用 ggplot2 将多个 y 值绘制为单独的线的正确方法

发布于 2024-12-06 11:58:46 字数 468 浏览 0 评论 0原文

我经常遇到一个问题,我的数据框包含一个 x 变量、一个或多个方面变量以及多个不同的其他变量。有时我想同时将不同的 y 变量绘制为单独的线。但它始终只是我想要的一个子集。我尝试使用 Melt 来获取“变量”作为一列并使用它,如果我想要原始数据集中的每一列,它就可以工作。通常我不会。

现在我一直在做一些非常迂回的事情。假设使用 mtcars 我想绘制 disp、hp 和 wt 与 mpg 的关系图:

ggplot(mtcars, aes(x=mpg)) + 
  geom_line(aes(y=disp, color="disp")) + 
  geom_line(aes(y=hp, color="hp")) + 
  geom_line(aes(y=wt, color="wt"))

这感觉真的很多余。如果我首先融化 mtcars,那么所有变量都会融化,然后我将最终绘制我不想要的其他变量。

有人有这样做的好方法吗?

I often run into an issue where I have a data frame that has a single x variable, one or more facet variables, and multiple different other variables. Sometimes I would like to simultaneously plot different y variables as separate lines. But it is always only a subset I want. I've tried using melt to get "variable" as a column and use that, and it works if I want every single column that was in the original dataset. Usually I don't.

Right now I've been doing things really roundabout it feels like. Suppose with mtcars I want to plot disp, hp, and wt against mpg:

ggplot(mtcars, aes(x=mpg)) + 
  geom_line(aes(y=disp, color="disp")) + 
  geom_line(aes(y=hp, color="hp")) + 
  geom_line(aes(y=wt, color="wt"))

This feels really redundant. If I first melt mtcars, then all variables will get melted, and then I will wind up plotting other variables that I don't want to.

Does anyone have a good way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

给妤﹃绝世温柔 2024-12-13 11:58:46

ggplot 总是更喜欢格式的数据框,因此融化它:

library(reshape2)
mtcars.long <- melt(mtcars, id = "mpg", measure = c("disp", "hp", "wt"))
ggplot(mtcars.long, aes(mpg, value, colour = variable)) + geom_line()

还有许多其他选项可以进行此转换。您可以参阅有关将数据从宽数据转换为长数据的 R-FAQ 以获取概述。

ggplot always prefers long format dataframe, so melt it:

library(reshape2)
mtcars.long <- melt(mtcars, id = "mpg", measure = c("disp", "hp", "wt"))
ggplot(mtcars.long, aes(mpg, value, colour = variable)) + geom_line()

There are many other options for doing this transformation. You can see the R-FAQ on converting data from wide to long for an overview.

就像说晚安 2024-12-13 11:58:46

随着 reshape2 被弃用,我使用 tidyverse 包中的 pivot_longer 更新了 @kohske 答案。

透视在此处进行了解释,并涉及指定要重塑的数据,第二个参数描述了哪些列需要重塑重塑(使用 - 排除该列)。第三个是 names_to 给出将从存储在列名称中的数据创建的变量的名称。最后values_to给出了将从存储在单元格值中的数据创建的变量的名称,即计数。它们还有更复杂的示例,例如列名称中的数字,例如 wk1 wk2 等。

# new suggestion
library(tidyverse)

# I subset to just the variables wanted so e.g. gear and cab are not included
mtcars.long <- mtcars %>% 
  select("mpg","disp", "hp", "wt") %>% 
  pivot_longer(-mpg, names_to = "variable", values_to = "value")

head(mtcars.long)
# # A tibble: 6 x 3
# mpg variable  value
# <dbl> <chr>     <dbl>
#   1    21 disp     160   
# 2    21 hp       110   
# 3    21 wt         2.62
# 4    21 disp     160   
# 5    21 hp       110   
# 6    21 wt         2.88


ggplot(mtcars.long, aes(mpg, value, colour = variable)) + geom_line()

图表为:

mtcarstestchart

With reshape2 being deprecated, I updated @kohske answer using pivot_longer from tidyverse package.

Pivoting is explained here and involves specifying the data to reshape, second argument describes which columns need to be reshape (use - to exclude that column). Third is names_to gives the name of the variable that will be created from the data stored in the column names. Finally values_to gives the name of the variable that will be created from the data stored in the cell value, i.e. count. They also have more complex examples like numbers in column names e.g. wk1 wk2 etc.

# new suggestion
library(tidyverse)

# I subset to just the variables wanted so e.g. gear and cab are not included
mtcars.long <- mtcars %>% 
  select("mpg","disp", "hp", "wt") %>% 
  pivot_longer(-mpg, names_to = "variable", values_to = "value")

head(mtcars.long)
# # A tibble: 6 x 3
# mpg variable  value
# <dbl> <chr>     <dbl>
#   1    21 disp     160   
# 2    21 hp       110   
# 3    21 wt         2.62
# 4    21 disp     160   
# 5    21 hp       110   
# 6    21 wt         2.88


ggplot(mtcars.long, aes(mpg, value, colour = variable)) + geom_line()

Chart is:

mtcarstestchart

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文