Graph of joint curves in ggplot2

Asked

Viewed 53 times

1

I’m trying to make a graph that contains the evolution of the variable Var1 depending on the variable Var2 together rather than separately as it is being presented in the figure below. inserir a descrição da imagem aqui

The data is here: https://drive.google.com/file/d/1eWNHSuYuKbXakJv9w9hd8rDPSvXPuxUf/view?usp=sharing

To carry out the above graph I used the following computational routine:


library(ggplot2)
library(gridExtra)
library(dplyr)

p1=ggplot(subset(datanew2, Var3 < 5), aes(Var2,Var1)) + 
  geom_smooth() + 
  theme(legend.position = "none")

p2=ggplot(subset(datanew2, Var3 < 11), aes(Var2,Var1)) + 
  geom_smooth() + 
  theme(legend.position = "none")

p3=ggplot(subset(datanew2, Var3 < 16), aes(Var2,Var1)) + 
  geom_smooth() + 
  theme(legend.position = "none")

p4=ggplot(subset(datanew2, Var3 < 20), aes(Var2,Var1)) + 
  geom_smooth() + 
  theme(legend.position = "none")

However, what I would like to accomplish is close to what is being presented below (in the computational routine below), but realize that something is going wrong, since the result shown in the graph does not match the image above where the separate curves are. To try to accomplish what I would like, I created a variable that contains the different classifications obtained through the variable Var3 and then I used the function group containing the variable created within the function aes both internal the function ggplot, but the result is not satisfactory as described above.

inserir a descrição da imagem aqui

library(ggplot2)
library(gridExtra)
library(dplyr)

datanew2 <- datanew2%>%dplyr::mutate(Factor = ifelse(Var3 < 5,
                                              "A",ifelse(Var3 < 11, "B", ifelse(Var3 < 16, "C",
                                                                                 ifelse(Var3 < 20, "D")))))
ggplot(datanew2, aes(group=Factor,Var2,Var1)) +
  geom_smooth()  +
theme(legend.position = "none")
  • 1

    The graph looks different because the axes have different scales.

1 answer

1


There is nothing wrong with the graph, if the lines are separated, the x axis can vary between 5 and 17.5, in the first graph, and between 10 and 40, in the last two. And the axes of the y’s are also very different.

In the following code, I use the cut to create the factor Group which in the question code is called Factor and is created with a series of ifelse

library(dplyr)
library(ggplot2)

datanew2 %>%
  mutate(Group = cut(Var3, breaks = c(-Inf, 5, 11, 16, 20), labels = LETTERS[1:4])) %>%
  ggplot(aes(Var2, Var1, group = Group)) +
  geom_smooth(method = 'gam', formula = y ~ s(x, bs = "cs"))

[![insert image description here][1][1]


Data reading

google_id <- "1eWNHSuYuKbXakJv9w9hd8rDPSvXPuxUf"
google_file <- sprintf("https://docs.google.com/uc?id=%s&export=download", google_id)
datanew2 <- read.csv2(google_file)

 [1]: https://i.stack.imgur.com/cucxZ.png
  • 1

    @Brenog. The last ifelse(Var3 < 20, "D") is not correct, does not have the value for the alternative no, when Var3 >= 20. Maybe by removing it, staying with ifelse(Var3 < 16, "C", "D"). As for the graph, with all the levels of the factor, this one is correct. It is quite simple.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.