0

My dataset is organized in this way:

factor <- c("group1", "group2", "group2")
X <- c("A", "B", "C")
V1 <- c(1:3)
V2 <- c(7:9)
V3 <- c(13:15)

df <- data.frame(factor, X, V1, V2, V3)
df
  factor X V1 V2 V3
1 group1 A  1  7 13
2 group2 B  2  8 14
3 group2 C  3  9 15

I would like to represent a line chart where:

  • The X axis represents X (A, B, C)
  • The Y axis represents values from V1, V2, and V3
  • I want to represent in a single chart these three lines, one per each variable (V1, V2, V3).
  • I want to color parts of each line differently according to factor variable, so that a single line (V1 for instance) is partly represented by two colors (let's say red group1 and green group2).
1
  • What prevents you to do so? Did you checked plot, lines, ggplot2 already? Commented May 14, 2018 at 15:17

1 Answer 1

2

You are likely going to want to tidy your data first -- this has to be a duplicate, but couldn't find one quickly...

library(tidyverse)

df %>%
  gather(key, value, -factor, -X) %>%
  ggplot(., aes(x = X, y = value, group = key, color = factor)) +
  geom_line(size = 2)

plot

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.