0

I have a dataframe as follows:

Figure

I want to apply ggpaired for paired comparison. My R code is as follows:

Data$State<-factor(Data$State, levels = c("PDSS", "MSDD","HCP"))
Data$Condition<-factor(Data$Condition, levels = c("SM", "DM"))
ggpaired(Data, x =  "Condition", y = "Value",color = "Condition",line.color = "grey", line.size = 0.4, palette = "jco",facet.by = "State", short.panel.labs = FALSE)

I am getting an error as follows: geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?

My output file looks like this.Output

How should I have all the paired lines? Any help is highly appreciated!

Thanks, Vrutang

Vrutang Shah
  • 45
  • 1
  • 7
  • possible duplicate https://stackoverflow.com/questions/27082601/ggplot2-line-chart-gives-geom-path-each-group-consist-of-only-one-observation?rq=1 – J.Moon Mar 08 '19 at 21:41
  • I tried doing that also. ggpaired(Data, aes(x = Condition, y = Value,group=1),color = "Condition",line.color = "grey", line.size = 0.4, palette = "jco",facet.by = "State", short.panel.labs = FALSE) It is showing error: "Error in .check_data(data, x, y, combine = combine | merge != "none") : x and y are missing. In this case data should be a numeric vector." – Vrutang Shah Mar 08 '19 at 21:45
  • Also tried to put as.numeric: ggpaired(Data, aes(x = as.numeric(Condition), y = Value,group=1),color = "Condition",line.color = "grey", line.size = 0.4, palette = "jco",facet.by = "State", short.panel.labs = FALSE) but still giving the same error: "Error in .check_data(data, x, y, combine = combine | merge != "none") : x and y are missing. In this case data should be a numeric vector." – Vrutang Shah Mar 08 '19 at 21:49
  • None of the ideas are working! – Vrutang Shah Mar 09 '19 at 04:55

1 Answers1

1

You were trying to link pairs of data points and it requires some identification to link by for each pair. Your data set is missing an identification variable. A variable ID is added into the data set:

> str(Data)
'data.frame':   44 obs. of  4 variables:
 $ State    : Factor w/ 3 levels "PDSS","MSDD",..: 3 3 3 3 3 3 3 2 2 2 ...
 $ Condition: Factor w/ 2 levels "SM","DM": 1 1 1 1 1 1 1 1 1 1 ...
 $ Value    : num  0.91 1.24 1.02 1.29 1.38 1.51 1.18 0.88 1.18 0.96 ...
 $ ID       : int  1 2 3 4 5 6 7 8 9 10 ...

Then, all you need to do is to add an id = option within the ggpaired statement, as follows:

ggpaired(Data, x =  "Condition", y = "Value",color = "Condition",
     line.color = "grey", line.size = 0.4, palette = "jco", id = "ID", 
     facet.by = "State", short.panel.labs = FALSE)

The result:

enter image description here

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
T Lin
  • 36
  • 2