0
votes

I'm trying the plot a simple scatter plot: X axis is for student graduation term and Y axis is their GPA.

Below is how I read the data (Graduated08 is the file name):

Graduated08 <- read.csv (file="200804_Graduated.csv",
+                          header = TRUE,
+                          na.strings = "NA")

Below is the first a couple of rows of this data set:

Grad_Term          GPA
201302             3.560809
201403             3.013043
201202             4.000000
201302             3.344286
201204             3.596993
201401             3.393704

Above, 201302 stands for Spring term of 2013, 201202 stands for Spring term of 2012. Basically the naming convention is like this: the first four digits represent the year and the last two digits represent a particular term in that year (01-Winter term, 02-Spring term, 03-Summer term, 04-Fall term).

I used the following R codes to get a simple scatter plot

**> plot (x= Graduated08$Grad_Term,
+       y= Graduated08$GPA
+ )**

However, I got a plot that messed up with the X axis. Y axis looks fine, but X axis (Grad_Term) is messed up. Instead of having 201201, 201202, 201203 and 201204 separated out, the plot seems to group all these terms into one variable which is labeled as "201200". Same thing happened for other years (only see 201300, 201400 etc). I want to have all the four terms in a year be plotted out separately.

2
what is the output from str(Graduated08)? - alexwhan

2 Answers

0
votes

I believe you want the Grad_Term column to be the character class. However, R is treating this column as something other than character. Use the following code to force Grad_Term to be character:

Graduated08 <- read.csv(file="200804_Graduated.csv",
                        header=TRUE,
                        colClasses=c("character", "numeric"),
                        na.strings="NA")

# now make your plot and each quarter should be a separate data point
-1
votes

I agree with Tim in principle. However, I tried this on my end and plot() will automatically convert Grad_Term to numeric.

The easiest way to get around this is to turn off the automatic plotting initially, then add in custom x-axis values

d<-data.frame(rbind(
  cbind(201302,3.560809),
  cbind(201403,3.013043),
  cbind(201202, 4.000000),
  cbind(201302,3.344286),
  cbind(201204, 3.596993),
  cbind(201401,3.393704)))
names(d)<-c('Grad_Term','GPA')

attach(d)

# this is wrong
plot(Grad_Term,GPA)

# try this:
# turn off axis with xaxt= paramter
plot(Grad_Term,GPA,xaxt='n')

# put in your own, custom x axis
axis(1,at=Grad_Term)

enter image description here