1

Suppose I have a data set of this form.

enter image description here

I would like to make one for X1 and one for X2 by connecting LCL and UCL for each model. The expected outcome should look like this figure

enter image description here

The lower point is the LCL and the upper point is the UCL of each model. Any help is appreciated. here is data using dput() function

> dput(mydata)
structure(list(x1m1 = c(0.5, 1), x1m2 = c(0.2, 1.5), x1m3 = c(0.5, 
1.25), x2m1 = c(0.24, 0.98), x2m2 = c(0.1, 1.4), x2m3 = c(1, 
2)), class = "data.frame", row.names = c("LCL", "UCL"))
2
  • 1
    Could you provide the an example of the input data with dput()? Your input data has multiple column levels (X,model,lcl,values), which is impossible to represent in a single standard dataframe (to my knowledge). Did you already parsed the data? Commented Jun 18, 2021 at 6:45
  • 1
    see the data. you should be able to use it now. Commented Jun 18, 2021 at 6:54

1 Answer 1

1

This is a solution using the tidyverse (in this case dplyr and ggplot)

library(tidyverse)

mydata$point <- rownames(mydata) # integrate Rownames in dataframe

# pivot the data longer and separate the cols
mydata <- mydata %>% pivot_longer(-c(point),
                        names_to="X",
                        values_to="value"
                        ) %>%
  separate(X,into = c("X","model"), sep=2)  %>%# separate the stringcol
  pivot_wider(names_from = point, values_from=value) #pivot wider again to separate LCL and UCL Column

# use 
ggplot(mydata,aes(x=X,color=model,ymin = LCL, ymax=UCL))+
  geom_linerange(position=position_dodge(width=0.20))
  # postion argument to dodge so that the lines are not plottet above each other

Result:

enter image description here

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

1 Comment

If I want to add one point in each line? How can I do? Here is the data set structure(list(x1m1 = c(0.5, 1, 0.6), x1m2 = c(0.2, 1.5, 0.25 ), x1m3 = c(0.5, 1.25, 1), x2m1 = c(0.24, 0.98, 0.7), x2m2 = c(0.1, 1.4, 0.8), x2m3 = c(1, 2, 0.6)), class = "data.frame", row.names = c("LCL", "UCL", "mean")) @Sandwichnick

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.