如何获取数据框中所有行的线图?

时间:2014-04-16 10:37:06

标签: r plot ggplot2 bar-chart

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

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

1 个答案:

答案 0 :(得分:3)

由于你没有提到你想要什么样的情节,这里有两个例子(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")

enter image description here

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

enter image description here