从数据框中绘制多条平滑线 [英] Plotting multiple smooth lines from a dataframe
问题描述
我对R相对较新.我正在尝试绘制从csv文件加载的数据帧.数据由6列组成,如下所示:
I am relatively new to R. I am trying to plot a dataframe loaded from a csv file. The data consists of 6 columns like this:
xval,col1,col2,col3,col4,col5
第一列(xval)由一系列单调递增的正整数(例如10、40、60等)组成,其他列1至5由浮点数组成.
The first column (xval) consist of a sequence of monotonically increasing positive integers (e.g. 10, 40, 60 etc), the other columns columns 1 to 5, consist of floating point numbers.
我要在R中创建一个图,如下所示:
I want to create a plot in R as follows:
- 在x轴上绘制xval项
- 绘制剩余的列(col1 ... col5)行
- 使用col2创建一个图例图例,... col5重命名
要绘制的数据(col1,... col5)是快照"值,因此尽管我想将它们绘制为线,但我还是希望这些线平滑(即插值).
The data to be plotted (col1, ... col5) are 'snapshot' values so although I want to plot them as lines, I want the lines to be smoothed (i.e. interpolated).
我正在寻找一个片段,一旦将数据读入数据框,该片段将帮助我创建绘图.任何帮助将不胜感激.
I am looking for a snippet that help me create the plot once I have read the data into a dataframe. Any help will be appreciated.
推荐答案
看看ggplot2
#create dummy data
n <- 200
dataset <- data.frame(xval = runif(n), col1 = rnorm(n), col2 = rnorm(n, sd = 2), col3 = rnorm(n, mean = seq(0, 2, length = n)), col4 = rnorm(n, sd = seq(0, 1, length = n)), col5 = rnorm(n, mean = 1))
#convert data to long format
library(reshape)
Molten <- melt(dataset, id.vars = "xval")
#plot it
library(ggplot2)
ggplot(Molten, aes(x = xval, y = value, colour = variable)) +
geom_smooth() + geom_point()
#some tweaking
ggplot(Molten, aes(x = xval, y = value, colour = variable)) +
geom_smooth(se = FALSE) + geom_point() + theme_bw() +
scale_x_continuous("the x label") + scale_x_continuous("the y label") +
scale_colour_discrete("")
这篇关于从数据框中绘制多条平滑线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!