# Sample scripts for tutorial: data types
# call library
library(ggplot2)
library(reshape2)
# read data
data0 <- read.table(header = T, text='
id japanese mathematics
11 50 60
22 70 40
33 40 80
44 90 30
')
# wide to long for ggplot2
data0.long <- melt(data0, id.vars="id", variable.name="subject", value.name="score")
# show structure
str(data0.long)
## 'data.frame': 8 obs. of 3 variables:
## $ id : int 11 22 33 44 11 22 33 44
## $ subject: Factor w/ 2 levels "japanese","mathematics": 1 1 1 1 2 2 2 2
## $ score : int 50 70 40 90 60 40 80 30
# convert id column from integer to factor
data0.long$id <- as.factor(data0.long$id)
# show structure
str(data0.long)
## 'data.frame': 8 obs. of 3 variables:
## $ id : Factor w/ 4 levels "11","22","33",..: 1 2 3 4 1 2 3 4
## $ subject: Factor w/ 2 levels "japanese","mathematics": 1 1 1 1 2 2 2 2
## $ score : int 50 70 40 90 60 40 80 30
# plot
p1 = ggplot(aes(y = score, x = subject, colour = id), data = data0.long) +
geom_point(size=10)
p1
