Section 30 Numerics & Factor: geom_point
30.1 Scatter Plot
30.2 Numeric, Numeric, Factor
data(mtcars)
# geom_point
g <- ggplot(data=mtcars, mapping=aes(x=wt, y=mpg))
g <- g + geom_point(mapping=aes(colour=factor(cyl)))
g
# size
g <- ggplot(data=mtcars, mapping=aes(x=wt, y=mpg))
g <- g + geom_point(mapping=aes(colour=factor(cyl)), size=5)
g
# colour and shape
g <- ggplot(data=mtcars, mapping=aes(x=wt, y=mpg))
g <- g + geom_point(mapping=aes(colour=factor(cyl), shape=factor(cyl)), size=5)
g
# labs, theme_bw
g <- ggplot(data=mtcars, mapping=aes(x=wt, y=mpg))
g <- g + geom_point(mapping=aes(colour=factor(cyl), shape=factor(cyl)), size=5)
g <- g + labs(title='Scatter plot',
x='Weight (1000 lbs)',
y='Miles/gallon')
g + theme_bw()
30.3 Numeric, Numeric, Numeric
data(mtcars)
g <- ggplot(data=mtcars, mapping=aes(x=wt, y=mpg))
g <- g + geom_point(mapping=aes(size=qsec))
g
g <- ggplot(data=mtcars, mapping=aes(x=wt, y=mpg))
g <- g + geom_point(mapping=aes(colour=factor(cyl), size=qsec))
g
g <- ggplot(data=mtcars, mapping=aes(x=wt, y=mpg))
g <- g + geom_point(mapping=aes(colour=cyl))
g <- g + scale_colour_gradient(low = 'blue')
g
30.4 Numeric, Numeric, Factor with Model
data(mtcars)
g <- ggplot(data=mtcars, mapping=aes(x=wt, y=mpg))
g <- g + geom_point(mapping=aes(colour=factor(cyl), shape=factor(cyl)), size=5)
g <- g + labs(title='Scatter plot',
x='Weight (1000 lbs)',
y='Miles/gallon')
g + theme_bw()
g <- ggplot(data=mtcars, mapping=aes(x=wt, y=mpg))
g <- g + geom_point(mapping=aes(colour=factor(cyl), shape=factor(cyl)), size=5)
g <- g + geom_smooth(method=lm)
g <- g + labs(title='Scatter plot',
x='Weight (1000 lbs)',
y='Miles/gallon')
g + theme_bw()
g <- ggplot(data=mtcars, mapping=aes(x=wt, y=mpg, colour=factor(cyl)))
g <- g + geom_point(mapping=aes(shape=factor(cyl)), size=5)
g <- g + geom_smooth(method=lm, se=TRUE)
g <- g + labs(title='Scatter plot',
x='Weight (1000 lbs)',
y='Miles/gallon')
g + theme_bw()
g <- ggplot(data=mtcars, mapping=aes(x=wt, y=mpg, colour=factor(cyl)))
g <- g + geom_point(mapping=aes(shape=factor(cyl)), size=5)
g <- g + geom_smooth(method=loess, se=FALSE)
g <- g + labs(title='Scatter plot',
x='Weight (1000 lbs)',
y='Miles/gallon')
g + theme_bw()
30.5 Exercise
- Generate the following data.
set.seed(12345)
x <- rnorm(n = 10, mean=10, sd=2)
y1 <- 2*x + rnorm(n = 10, mean=0, sd=1)
y2 <- 4 + 2*x + rnorm(n = 10, mean=0, sd=1)
Gr <- rep(c('G1','G2'), each = 10)
DF <- data.frame(X=x, Y=c(y1,y2), ID=LETTERS[1:20], Gr=Gr)
- Create the following scatter plot along with the fitted line.