0

This is my data . I want to create multiple line charts for the different variables . The long form of the data is being created using "melt" from "reshape2" package .

The current code I am using is :

ggplot(data = agg_melt_p, aes(x=Cat, y=value)) + geom_line(aes(colour=variable))

This gives me the following error: geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?

Data:

Cat <- c(1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4)

variable <- c("IL_1_Flag_p", "IL_1_Flag_p", "IL_1_Flag_p", "IL_1_Flag_p", "IL_2_Flag_p", "IL_2_Flag_p", "IL_2_Flag_p","IL_2_Flag_p", "IL_3_Flag_p", "IL_3_Flag_p", "IL_3_Flag_p", "IL_3_Flag_p", "IL_4_Flag_p", "IL_4_Flag_p", "IL_4_Flag_p", "IL_4_Flag_p", "IL_5_Flag_p", "IL_5_Flag_p", "IL_5_Flag_p", "IL_5_Flag_p")

value <- c(21,17,16,210,20,17,15,189,20,17,15,188,19,17,15,188,20,17,15,194)

agg_melt_p <- data.frame(cat, variable, value)
2
  • 3
    try group = variable in the geom line aes? Commented Jan 18, 2018 at 9:09
  • 1
    Your code works, given the data snapshot you provided Commented Jan 18, 2018 at 9:12

1 Answer 1

1

both your code, using colour=variable, and Scientist_jake's suggestion to use group = variable, works for me. As follows,

agg_melt_p <- data.frame(cat = c(1, 2, 3, 4, 1, 2, 3, 4, 
                  1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4),
              variable = c("IL_1_Flag_p", "IL_1_Flag_p", "IL_1_Flag_p", "IL_1_Flag_p",
                 "IL_2_Flag_p", "IL_2_Flag_p", "IL_2_Flag_p","IL_2_Flag_p", "IL_3_Flag_p",
                 "IL_3_Flag_p", "IL_3_Flag_p", "IL_3_Flag_p", "IL_4_Flag_p", "IL_4_Flag_p",
                 "IL_4_Flag_p", "IL_4_Flag_p", "IL_5_Flag_p", "IL_5_Flag_p", "IL_5_Flag_p",
                 "IL_5_Flag_p"),
              value = c(21, 17, 16, 210, 20, 17, 15, 189, 20, 17,
                       15, 188, 19, 17, 15, 188, 20, 17, 15, 194))

# install.packages(c("ggplot2"), dependencies = TRUE)
library(ggplot2)

ggplot(data = agg_melt_p, aes(x = cat, y = value)) + 
       geom_line(aes(colour = variable))

colour = variable

ggplot(data = agg_melt_p, aes(x = cat, y=value)) +  
      geom_line(aes(group = variable))

group = variable

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

2 Comments

This works for me ! Thanks ! Also can you tell how to scale the values I am using ? I am appending this line chart with a 100% Stacked bar chart with the same X axis as Category .
Glad it works for you. Please ask any new questions an a new question.

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.