使用 ggplot2 将多个 y 值绘制为单独的线的正确方法
我经常遇到一个问题,我的数据框包含一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ggplot 总是更喜欢长格式的数据框,因此
融化
它:还有许多其他选项可以进行此转换。您可以参阅有关将数据从宽数据转换为长数据的 R-FAQ 以获取概述。
ggplot always prefers long format dataframe, so
melt
it: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.
随着 reshape2 被弃用,我使用
tidyverse
包中的 pivot_longer 更新了 @kohske 答案。透视在此处进行了解释,并涉及指定要重塑的数据,第二个参数描述了哪些列需要重塑重塑(使用 - 排除该列)。第三个是 names_to 给出将从存储在列名称中的数据创建的变量的名称。最后values_to给出了将从存储在单元格值中的数据创建的变量的名称,即计数。它们还有更复杂的示例,例如列名称中的数字,例如 wk1 wk2 等。
图表为:
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.
Chart is: