【问题标题】:How to get a line-plot of all the rows in a data frame? [closed]如何获取数据框中所有行的线图? [关闭]
【发布时间】:2014-04-16 10:37:06
【问题描述】:

这不是我的数据,但我们可以用它作为例子:

Name     1st   2nd   3rd   4th   5th  6th  7th   
Gregg     0    0.6   1     0.2   0    0.5    1  
Mike     0.4    1    0.6   0     0    0      0 
Susane    1     0    0     0     1    0.3    0 
Marcel    0     1    0.75  0.25  0    0      0 

我想获得此数据每一行的线图。对于大数据集,我怎样才能有效地做到这一点?

对于每一行,最大值始终为1

【问题讨论】:

  • 情节是什么意思?
  • 可能是matplot(t(dat[-1,]))?目前尚不清楚您拥有什么样的数据结构以及您想要实现什么。

标签: r plot ggplot2 bar-chart


【解决方案1】:

由于你没有提到你想要什么样的情节,这里有两个例子(ggplot2 包):

# reading the data
df <- read.table(text = "Name     first   second   third   fourth   fifth  sixth  seventh   
Gregg     0    0.6   1     0.2   0    0.5    1  
Mike     0.4    1    0.6   0     0    0      0 
Susane    1     0    0     0     1    0.3    0 
Marcel    0     1    0.75  0.25  0    0      0", header = TRUE)

# transforming the data to long format
library(reshape2)
df2 <- melt(df, id = "Name")

# creating a barplot
require(ggplot2)
ggplot(df2, aes(x = Name, y = value, fill = variable)) +
  geom_bar(stat = "identity", position = "dodge")

# creating a line plot
ggplot(df2, aes(x = as.numeric(variable), y = value)) +
  geom_line() +
  facet_grid(~ Name)

【讨论】:

  • 我需要一个线图。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多