如何在R中绘制数据框的所有列
数据框有 n 列,我想得到 n 个图,每列一个图。
我是新手,对 R 不太熟悉,无论如何我找到了两个解决方案。
第一个可以工作,但它不打印列名称(我需要它们!):
data <- read.csv("sample.csv",header=T,sep=",")
for ( c in data ) plot( c, type="l" )
第二个工作更好,因为它打印列名称:
data <- read.csv("sample.csv",header=T,sep=",")
for ( i in seq(1,length( data ),1) ) plot(data[,i],ylab=names(data[i]),type="l")
有没有更好的(从 R 语言的角度来看)解决方案?
The data frame has n columns and I would like to get n plots, one plot for each column.
I'm a newbie and I am not fluent in R, anyway I found two solutions.
The first one works but it does not print the column name (and I need them!):
data <- read.csv("sample.csv",header=T,sep=",")
for ( c in data ) plot( c, type="l" )
The second one works better because it prints the column name:
data <- read.csv("sample.csv",header=T,sep=",")
for ( i in seq(1,length( data ),1) ) plot(data[,i],ylab=names(data[i]),type="l")
Is there any better (from the R language point of view) solutions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
ggplot2 包需要一点点学习,但结果看起来非常好,您可以获得漂亮的图例,以及许多其他不错的功能,所有这些都无需编写太多代码。
The
ggplot2
package takes a little bit of learning, but the results look really nice, you get nice legends, plus many other nice features, all without having to write much code.有一种非常简单的方法可以使用单独的面板或同一面板绘制数据框中的所有列:
产生结果(其中 X1 - X4 是列名称):
查看 ?plot.ts 了解所有选项。
如果您不想更多地控制绘图函数并且不使用循环,您也可以执行以下操作:
There is very simple way to plot all columns from a data frame using separate panels or the same panel:
Which yields (where X1 - X4 are column names):
Have look at ?plot.ts for all the options.
If you wan't more control over your plotting function and not use a loop, you could also do something like:
您可以跳过重重困难,将您的解决方案转换为
lapply
、sapply
或apply
调用。 (我看到@jonw展示了一种方法来做到这一点。)除此之外,您已经拥有的是完全可以接受的代码。如果这些都是时间序列或类似的时间序列,那么以下可能是一个合适的替代方案,它将每个序列绘制在单个绘图区域的其自己的面板中。我们使用zoo包,因为它确实可以很好地处理这样的有序数据。
这给出:
You can jump through hoops and convert your solution to a
lapply
,sapply
orapply
call. (I see @jonw shows one way to do this.) Other than that what you have already is perfectly acceptable code.If these are all a time series or similar then the following might be a suitable alternative, which plots each series in it's own panel on a single plotting region. We use the
zoo
package as it handles ordered data like this very well indeed.Which gives:
我很惊讶没有人提到
matplot
。如果您不需要在单独的轴上绘制每条线,这非常方便。只需一个命令:
使用
?matplot
查看所有选项。要添加图例,您可以设置调色板,然后添加它:
I'm surprised that no one mentioned
matplot
. It's pretty convenient in case you don't need to plot each line in separate axes.Just one command:
Use
?matplot
to see all the options.To add the legend, you can set color palette and then add it:
使用上面的一些技巧(特别感谢 @daroczig 的
names(df)[i]
形式),该函数会打印数值变量的直方图和因子变量的条形图。探索数据框架的良好开端:最良好的祝愿,Mat。
Using some of the tips above (especially thanks @daroczig for the
names(df)[i]
form) this function prints a histogram for numeric variables and a bar chart for factor variables. A good start to exploring a data frame:Best wishes, Mat.
不幸的是,ggplot2 没有提供一种在不将数据转换为长格式的情况下(轻松地)执行此操作的方法。您可以尝试对抗它,但数据转换会更容易。这里是所有方法,包括来自 reshape2 的
melt
、来自 tidyr 的gather
和来自 tidyr 的pivot_longer
:将 data.frame 从宽格式重塑为长格式这是一个使用
pivot_longer
:如您所见,
pivot_longer
放置选定的列names_to 指定的任何内容(默认“name”),并将长值放入values_to
指定的任何内容(默认“value”)。如果我同意默认名称,我可以使用df %>%ivot_longer(c("a", "b"))
。现在您可以正常绘图:
Unfortunately, ggplot2 does not offer a way to do this (easily) without transforming your data into long format. You can try to fight it but it will just be easier to do the data transformation. Here all the methods, including
melt
from reshape2,gather
from tidyr, andpivot_longer
from tidyr: Reshaping data.frame from wide to long formatHere's a simple example using
pivot_longer
:As you can see,
pivot_longer
puts the selected column names in whatever is specified bynames_to
(default "name"), and puts the long values into whatever is specified byvalues_to
(default "value"). If I'm ok with the default names, I can use usedf %>% pivot_longer(c("a", "b"))
.Now you can plot as normal:
对于
格子
:With
lattice
:您可以使用
main
选项指定标题(以及通过xlab
和ylab
指定轴的标题)。例如:如果您想绘制(并保存)数据帧的每个变量,您应该使用
png
、pdf
或您需要的任何其他图形驱动程序,然后在该问题之后一个 dev.off() 命令。例如:或者使用
par()
的mfrow
参数将所有绘图绘制到同一图像。例如:使用par(mfrow=c(2,2)
将接下来的 4 个图包含在同一“图像”中。You could specify the title (and also the title of the axes via
xlab
andylab
) with themain
option. E.g.:And if you want to plot (and save) each variable of a dataframe, you should use
png
,pdf
or any other graphics driver you need, and after that issue adev.off()
command. E.g.:Or draw all plots to the same image with the
mfrow
paramater ofpar()
. E.g.: usepar(mfrow=c(2,2)
to include the next 4 plots in the same "image".我这台计算机上没有 R,但这里有一个破解方法。您可以使用
par
在一个窗口中显示多个绘图,或者像这样在显示下一页之前提示单击。I don't have R on this computer, but here is a crack at it. You can use
par
to display multiple plots in a window, or like this to prompt for a click before displaying the next page.如果
.csv
文件中的列名称不是有效的 R 名称:In case the column names in the
.csv
file file are not valid R name:此链接对同样的问题帮助了我很多:
https://rpubs.com/euclid/343644
This link helped me a lot for the same problem:
https://rpubs.com/euclid/343644