A specific coding question

The data I used is in the attachment, so the problem I have is that I want to use days as x axis and time preference as y axis and make line plot or linear regression for them.

I want to divide the data into 8 groups, that is, two strains (1,0) * four types of stress condition combinations ( in which the number of different stress columns are stress.trian, stress rec1, stress rec2 ---000,101,110, 010 respectively). Since there are many time preference values for a single today, I also need to aggregate them to get the mean values in order to draw the line or do the linear regression. The ideal case is to get a graph like the one in attachment ( but that is the one without linear regression).

I tried and write the code like this:

data1<-read.csv("Experimental_conditions.csv")

data8<-read.csv("time_preferences.csv")

install.packages("reshape2")
library(reshape2)
data10 <- melt(data8, id.vars = "Mouse.no.", variable.name = "days", value.name = "time preference")

data9<-cbind(data1, data10)
pointcolor<-rep("purple", nrow(data9))
pointcolor[data9$Stress.train== "1"& data9$Stress.rec1=="1"& data9$Stress.rec2=="0"]<-"blue"
pointcolor[data9$Stress.train =="0"& data9$Stress.rec1 =="1"& data9$Stress.rec2 =="0"]<-"green"
pointcolor[data9$Stress.train =="0"& data9$Stress.rec1 =="0"& data9$Stress.rec2 =="0"]<-"black"
plot(aggregate(data9$`time preference`, by=list(data9$days), mean), col = pointcolor, bty = "n", pch = 20, cex = 0.7, cex.axis = 0.8, las = 1,ylim=c(0,2),xlab="Days",ylab="time preference" )

legend("topright", legend = c("no stress", "ts+re2","ts+rs1","rs1"), fill = c("black", "purple","blue","green"), cex = 0.7)

So I first reshaped the data to the suitable format and merge it with another one, and I tried to give different color to different cases with fours types of stress combinations. But there is only black color shows in my final plot. I think maybe aggregate can't be used in that way (included in plot function)?

I tried ggplot and scatter.smooth also but all failed. How should I do? The most tricky things for me is how make those 8 lines distinguishable in the graph (ex. different colors for standing 4 different stress combinations and 2 line types for strains) and also how to put each day's time preference values distribution and there means in the graph when I draw the line also, just like the graph shown in attachment.

You mention attachments but there are none...
I assume we can consider the code leading up to the creation of data9 to be working for you as you would wish, and you require more specific assistance in plotting data9 in the way you describe, therefore consider providing a representative sample of data9 in a forum friendly form, i.e. as described in the guide.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.