brittAnderson / Intro2Computing4Psychology

A guided introduction to computing tools useful for research in psychology - targeted to complete beginners
46 stars 159 forks source link

ploting in r #83

Closed TheCanasian23 closed 2 years ago

TheCanasian23 commented 2 years ago

I'm trying to do the interaction plot in r, but the graph is not coming out rite. I'm pretty sure all the data is imported right and put in the right variables.

`install.packages("readr") install.packages("ggplot2") install.packages("dplyr") library(dplyr) library (ggplot2) library(readr)

datas <- read_tsv(url("http://personality-project.org/r/datasets/heating.txt"))

x = datas %>% pull(degreedays) y = datas %>% pull(therms) affect = datas %>% pull(Location) interaction.plot(x, affect, y, fun = median, ylab = "Mean of Therms", xlab = "degreedays", col = c("red", "black", "blue"), lty = 1, lwd = 2, trace.label = "Location")`

brittAnderson commented 2 years ago

It is important to realize that there are many syntactic similarities and they have many of the same features (for example loops), the route they take to getting there can be quite different, and just because something has the same name as a python function doesn't mean it does the same thing. For example, try range(10) in R and python and compare what you get. There are a lot easier ways to get an interaction plot in R than trying to use the function with the exact name as you did in python. Try not to just copy the surface similarities, but think of the content you want.

Here is a pretty minimal example of an interaction type plot:

library (ggplot2)
datas <- read.csv(url("http://personality-project.org/r/datasets/heating.txt"), sep = "\t")
ggplot(datas,aes(x=degreedays,y=thermsdd,color=Location)) + geom_point() + stat_smooth(method = "lm")
TheCanasian23 commented 2 years ago

I'm not copying interaction plot from python, I'm using interaction plot from R that you link in your assignment page.

"R has a built in function for interaction plots that might work."

If ggplot works better or is easier to use then I'll switch to that

brittAnderson commented 2 years ago

Yes. But you have to read the fine print. That interaction.plot is for two way combination of factors, but you only have one factor: location. The other is a variable. Though you can technically force R to treat it as a factor, it won't be conceptually a factor.

For your data you will have a hard time getting a nice plot with interaction.plot, and you will get a nicer plot, more easily with ggplot, which is an R plotting package that many people like very much and fine the easiest way to get plotting in R.