ggplot2每行数据帧一行 [英] ggplot2 one line per each row dataframe

查看:175
本文介绍了ggplot2每行数据帧一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下数据的CSV文件:

I have a CSV file that contains the following data:

user,Measurement1,Measurement2,Measurement3,group
1,0.1,0.7,0.2,3
2,0.3,0.3,0.4,2
3,0.3,0.3,0.4,2

我需要为每个用户绘制一行。 x轴可以只是1,2,3(每个测量一个点,标记为在之前,之后,之后,但稍后可以制定标签)。颜色将由组列
设置我发现一个情节类似于我需要的,但我不想使用用户代码作为x值。

I need to plot one line per each user. The x axis can be just 1,2,3 (one point per each measurement labeled as before,at,after, but I can work out the labeling later). The color will be set by the group columns I found a plot similar to the one I need but I don't want to use the user code as the x value.

推荐答案

ggplot 以long格式喜欢数据:即每个维度的列,每个观察的行。您的数据目前是宽。使用 reshape 包从一个到另一个。

ggplot likes data in the 'long' format: i.e., a column for every dimension, and a row for every observation. Your data is currently 'wide'. Use the reshape package to go from one to the other.

我明白了,你想要Measurement1,Measurement2 ,而Measurement3为x轴? (所以在你的数据中,用户1的行将从0.1变为0.7到0.2?如果是这样,你想要这样:

Do I understand correctly that you want Measurement1, Measurement2, and Measurement3 to be on the x-axis? (So that, in your data, user 1's line would go from 0.1 to 0.7 to 0.2? If so, you want something like this:

require(reshape)

#Recreate your data frame
user <- gl(3, 1)
Meas1 <- c(0.7, 0.3, 0.3)
Meas2 <- c(0.7, 0.3, 0.3)
Meas3 <- c(0.2, 0.4, 0.4)
group <- c(3, 2, 2)
df <- data.frame(user=user, Meas1=Meas1, Meas2=Meas2, Meas3=Meas3, group=group)

#'melt' the data frame into long format
dfm <- melt(df, id.vars=c("user", "group"))

ggplot(dfm, aes(x=as.numeric(variable), y=value, colour=user)) + geom_line()

这篇关于ggplot2每行数据帧一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆