I have the following data and I want to create a line chart conditional on group1
and group2
variable.
dat <- structure(list(group1 = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L),
.Label = c("a", "b"),
class = "factor"),
group2 = c(1L, 2L, 3L, 4L, 1L, 2L, 3L),
val1 = c(10L, 3L, 2L, 7L, 10L, 3L, 2L),
val2 = c(8L, 4L, 8L, 5L, 8L, 4L, 8L),
val3 = c(7L, 5L, 6L, 9L, 7L, 5L, 6L)),
.Names = c("group1", "group2", "val1", "val2", "val3"),
class = "data.frame", row.names = c(NA, -7L))
Ideally, I want to have two graphs based on group1
(two for levels a
and b
) and within each group
1 variable I need line charts based on the row variables(basically connecting three points for val1
, val2
, and val3
) with different colors for different group2
variables.
Do you mean something like this:
The above code restructures the data into a format more easily used with ggplot.
dat
was defined using thedput()
code I added to your Q. The plot I think you want can be produced viawhich gives
I've basically had to make up some
x
data otherwise how do you know where in the x- and y-coordinate space the points should be located?