Interaction graph using ggplot2

Asked

Viewed 220 times

0

all right? I am trying to make a graph of interaction between the subjects (Variable S) being these subjects colored in 5 colors (which are the groups (variable G), but is giving the following error:

Error in select(. , Weight, Time, S) unused Arguments (Weight, Time, S)

setwd("C:\\Users\\breni\\Google Drive\\Acadêmica\\Mestrado\\Splines")
dados = read.table("dadosnovo1.csv", header = T, sep=";", dec=",")
dados$G  <- factor(dados$G)
dados$S  <- factor(dados$S)
dados$S1 <- factor(dados$S1)
str(dados)
attach(dados)

library(tidyverse)

interaction <- dados %>%
  select(Peso, Tempo, S) %>%
  group_by(S, Tempo) %>%
  summarise(Average = mean(Peso))

x11()

ggplot(interaction, aes(x=Tempo, y=Average, colour=G, group=S)) + 
  ggtitle("Evolução do peso no tempo para cada individuo em grupos") +   
  geom_line()
  • 1

    I cannot reproduce this error. The (obvious) error is that G is not a variable of interaction and therefore cannot be colour = G. Just switch to select(Peso, Tempo, G, S) and group_by(G, S, Tempo) and everything goes well.

  • 1

    interaction is a base R function, maybe you want to switch to, for example, interact.

2 answers

3


As I said in comment to the question, the mistake is that G is not a dataframe variable interaction and therefore cannot be colour = G. Just switch to select(Peso, Tempo, S, G) and group_by(S, G, Tempo) and everything goes well.

Note also that I changed the name of interaction, a function of the base R, to interact. The code used and the chart are as follows. The device x11() was disabled to be able to export the plotted chart on that device, something Rstudio does not allow.

library(tidyverse)

dados <- read.csv("dadosnovo.csv")

interact <- dados %>%
  select(Peso, Tempo, S, G) %>%
  group_by(S, G, Tempo) %>%
  summarise(Average = mean(Peso))

#x11()

ggplot(interact, aes(x=Tempo, y=Average, colour=G, group=S)) + 
  ggtitle("Evolução do peso no tempo para cada individuo em grupos") +   
  geom_line()

inserir a descrição da imagem aqui

  • Thanks Rui!! !!

0

By error message I believe that the R is not recognizing the function.

This may be because some packages have similar functions. One way to explicitly declare a function is to put the library name followed by two dots twice.

Try to use dplyr::select(Peso, Tempo, S) and tell me if the error persists.

  • thank you Lucca!

  • @Brenogabriel if the answer is correct you can mark it as right so that other users can also find it. Hug!

Browser other questions tagged

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