-2

I am willing to do some line charts in R with these data below. For now, I get an error with my code. Any help will be welcome.

data <- structure(list(names = c("AB", "BBB", "CCC"), day1 = c(2L, 4L, 
14L), day2 = c(2.2, 10, 25), day3 = c(1.2, 12, 12), day4 = c(1.7, 
4, 12), day5 = c(2L, 10L, 14L)), .Names = c("names", "day1", 
"day2", "day3", "day4", "day5"), row.names = c(NA, -3L), class = "data.frame")

data
#names day1 day2 day3 day4 day5
#AB     2    2.2 1.2   1.7  2
#BBB    4    10   12   4    10
#CCC    14    25   12  12    14


ggplot(data, aes(day1:day5, names, color = names)) + geom_line()
amonk
  • 1,769
  • 2
  • 18
  • 27

2 Answers2

2

Try this:

data <- structure(list(names = c("AB", "BBB", "CCC"), day1 = c(2L, 4L, 
14L), day2 = c(2.2, 10, 25), day3 = c(1.2, 12, 12), day4 = c(1.7, 
4, 12), day5 = c(2L, 10L, 14L)), .Names = c("names", "day1", 
"day2", "day3", "day4", "day5"), row.names = c(NA, -3L), class = "data.frame")

dat_m <- melt(data, id.var = "names")
ggplot(df_m, aes(variable, value, colour = names, group = names)) + geom_line()

This results in this plot: line plot

jkrainer
  • 413
  • 5
  • 10
0
library("ggplot2")
library("reshape2")

data <- data.frame(
  "names"=c("AB","BBB","CCC"),
  "day1"=c(2,4,14),
  "day2"=c(2.2,10,25),
  "day3"=c(1.2,12,12),
  "day4"=c(1.7,4,12),
  "day5"=c(2,10,14)
)

test_data_long <- melt(data, id="names")  # convert to long format
ggplot(data=test_data_long,aes(variable,value, colour=names)) + 
       geom_line(aes(group = names))

enter image description here

rgunning
  • 568
  • 2
  • 16
  • Try to avoid answering question that are duplicates of other questions. Also, no need for the extra `group` specification if `colour` is a discrete. – Axeman May 24 '17 at 14:40
  • thanks, I am grateful. it works well as I want. @rgunning – user8029791 May 24 '17 at 14:57