Source file

Please make the following plot using the data table d given below. I have provided examples to help you.

Questions to ask yourself to get started:

  1. Is the data table glyph ready? If not, do I need to need to convert table formats (i.e. narrow to wide) or join another table?

  2. What are the glyphs?

  3. What are the aesthetics?

  4. What are the scales (i.e. mappings)

  5. Are there fixed attributes (i.e. col = “red”)?

  6. Are there labels and themes?

set.seed(123)
d <- diamonds[sample(nrow(diamonds), 1000), ]
head(d)
carat cut color clarity depth table price x y z
1.03 Premium G VS2 62.3 59 6214 6.38 6.42 3.99
0.50 Fair D SI1 65.7 56 1323 5.01 4.97 3.28
1.74 Very Good H SI2 62.1 59 10086 7.65 7.78 4.79
0.51 Ideal D VS2 61.2 55 1882 5.18 5.16 3.16
0.70 Ideal H SI1 62.5 56 2294 5.64 5.69 3.54
0.71 Ideal H IF 62.0 54 3190 5.71 5.75 3.55

a) geom_smooth

loess smoothed curve

set.seed(955)
# Make some noisily increasing data
dat <- data.frame(cond = rep(c("A", "B"), each=10),
                  xvar = 1:20 + rnorm(20,sd=3),
                  yvar = 1:20 + rnorm(20,sd=3))

ggplot(dat, aes(x=xvar, y=yvar)) +
    geom_point(shape=1) +    # Use hollow circles
    geom_smooth()            # Add a loess smoothed fit curve with confidence region

# > geom_smooth: method="auto" and size of largest group is less than 1000, so using loess.
# Use 'method = x' to change the smoothing method.

regression line

set.seed(955)
# Make some noisily increasing data
dat <- data.frame(cond = rep(c("A", "B"), each=10),
                  xvar = 1:20 + rnorm(20,sd=3),
                  yvar = 1:20 + rnorm(20,sd=3))

ggplot(dat, aes(x=xvar, y=yvar)) +
    geom_point(shape=1) +    # Use hollow circles
    geom_smooth(method=lm, fill="red")   # Add linear regression line

b) facet_grid and facet_wrap

library(reshape2)

sp <- ggplot(tips, aes(x=total_bill, y=tip/total_bill)) + geom_point(shape=1)

# Divide by levels of "sex", in the horizontal direction
sp + facet_grid(. ~ sex)

hp <- ggplot(tips, aes(x=total_bill)) + geom_histogram(binwidth=2,colour="white")

# Histogram of total_bill, divided by sex and smoker
hp + facet_grid(sex ~ smoker)

ggplot(mpg, aes(displ, hwy))+
    geom_point()+
    facet_wrap(~manufacturer)

c) geom_text

set.seed(123)
ds <- data.frame(x = rnorm(10),
                 y = rnorm(10),
                 group = LETTERS[1:2])
ggplot(ds, aes(x, y)) +
  geom_point(aes(color = group), size = 7) +
  geom_text(aes(label = group), size = 4) 

d) labs and theme

plot and axis title

set.seed(123)

df <- diamonds[sample(1:nrow(diamonds), size = 1000),]


ggplot(df, aes(carat, price)) +
  geom_point() + 
  labs(title = "Diamonds", x = "x-axis -> Carat", y = "y-axis -> Price") +
  theme(plot.title = element_text(size = 50, colour = "#668cff"),  
        axis.title.x = element_text(size = 20, colour = "#6699ff"),  
        axis.title.y = element_text(size = 20, colour = "#ff8080"))  

axis tick size, color and blank

set.seed(123)

df <- diamonds[sample(1:nrow(diamonds), size = 1000),]

ggplot(df, aes(carat, price)) + 
  geom_point() + 
  theme(axis.ticks = element_line(size = 10))

ggplot(df, aes(carat, price, color = color, alpha = cut)) + 
  geom_point() + 
  theme(axis.text.x = element_text(colour = "#ff6666", size = 20), 
        axis.text.y = element_text(colour = "#668cff", size = 20))

ggplot(df, aes(carat, price, color = cut)) + 
  geom_point() + 
  theme(axis.text = element_blank())