如何在每行顶部创建名称的线图?

时间:2017-08-03 04:12:33

标签: r

我正在尝试用线条绘制一个图(每行作为一条线)。它们的颜色应该不同。棘手的部分是如何将名称添加到每一行?

一个例子,数据在

之下
df<- structure(list(name = structure(c(1L, 2L, 3L, 4L, 7L, 8L, 6L, 
12L, 10L, 5L, 9L, 13L, 11L, 14L), .Label = c("A", "AAD", "ABC", 
"ACF", "ACN", "ADDCR", "ADR", "AFFT", "ARFT", "ASED", "ASEDZ", 
"AXED", "AZEDR", "AZERD"), class = "factor"), X1 = c(1487.2, 
48705.7, 59.4, 1676, 998.6, 35936, 1573.8, 1553.8, 53.9, 56.8, 
2432.2, 431.8, 820.1, 10866.9), X2 = c(3251, 5217.2, 64.2, 45.4, 
65.8, 2937.6, 564, 659.6, NA, 68.9, 654.9, 130, 720.7, 1457.1
), X3 = c(6364.3, 2752.5, 147.9, 6647.6, 21444.6, 1512.2, 5615, 
2190, 506.3, 2150.3, 273.2, 1922.9, 10762, 12900.8), X4 = c(4942.3, 
3885.8, 248.8, 7643.5, 12741.5, 2141.8, 5407.1, 2486.4, 542.9, 
5058.1, 846.7, 5322, 6887.4, 7000.6), X5 = c(2301.2, 2173.5, 
313.1, 14112.1, 8298.2, 1615.7, 7516.3, 3094.1, 777.8, 12007.9, 
901.4, 9291.9, 4670.1, 3629.5), X6 = c(2779.1, 5564, 211.7, 9172.2, 
9581.5, 2136.2, 7630.7, 3986.6, 440.9, 6898.1, 2280.4, 6311.1, 
4080.4, 4843.9)), .Names = c("name", "X1", "X2", "X3", "X4", 
"X5", "X6"), class = "data.frame", row.names = c(NA, -14L))

1 个答案:

答案 0 :(得分:1)

library(reshape2)
library(ggplot2)
library(directlabels)

n <- df$name
df <- as.data.frame(t(df[,-1]))
colnames(df) <- n
rownames(df) = NULL
df$x = row.names(df)

mlt = melt(df)

p <- ggplot(data = mlt, aes(x = x, y = value,
                            group = variable, color = variable)) +
  geom_line()

direct.label(p, list(last.points, hjust = -0.25))