3

I am trying to replicate the code from Andrew Ng's Machine Learning course on Coursera in R (as the course is in Octave).

Basically I have to plot a non linear decision boundary (at p = 0.5) for a polynomial regularized logistic regression.

I can easily replicate the plot with the base library:

contour(u, v, z, levels = 0)
points(x = data$Test1, y = data$Test2)

where:

u <- v <- seq(-1, 1.5, length.out = 100)

and z is a matrix 100x100 with the values of z for every point of the grid. Dimension of data is 118x3.

I cannot do it in ggplot2. Does somebody know how to replicate the same in ggplot2? I tried with:

z = as.vector(t(z))
ggplot(data, aes(x = Test1, y = Test2) + geom_contour(aes(x = u, y = 
v, z = z))

But I get the error: Aesthetics must be either length 1 or the same as the data (118): colour, x, y, shape

Thanks.

EDIT (Adding plot created from code of missuse):

enter image description here

1

2 Answers 2

11

What you need is to convert the coordinates into long format. Here is an example using volcano data set:

data(volcano)

in base R:

contour(volcano)

enter image description here

with ggplot2:

library(tidyverse)
as.data.frame(volcano) %>% #convert the matrix to data frame
  rownames_to_column() %>% #get row coordinates
  gather(key, value, -rowname) %>% #convert to long format
  mutate(key = as.numeric(gsub("V", "", key)), #convert the column names to numbers
         rowname = as.numeric(rowname)) %>%
  ggplot() +
  geom_contour(aes(x = rowname, y = key, z = value))

enter image description here

if you would like to label it directly as in base R plot you can use library directlabels:

First map the color/fill to a variable:

as.data.frame(volcano) %>%
  rownames_to_column() %>%
  gather(key, value, -rowname) %>%
  mutate(key = as.numeric(gsub("V", "", key)),
         rowname = as.numeric(rowname)) %>%
  ggplot() +
  geom_contour(aes(x = rowname,
                   y = key,
                   z = value,
                   colour = ..level..)) -> some_plot

and then

library(directlabels)

direct.label(some_plot, list("far.from.others.borders", "calc.boxes", "enlarge.box", 
                     box.color = NA, fill = "transparent", "draw.rects"))

enter image description here

to add markers at specific coordinates you just need to add another layer with appropriate data:

the previous plot

as.data.frame(volcano) %>% 
  rownames_to_column() %>% 
  gather(key, value, -rowname) %>% 
  mutate(key = as.numeric(gsub("V", "", key)), 
         rowname = as.numeric(rowname)) %>%
  ggplot() +
  geom_contour(aes(x = rowname, y = key, z = value)) -> plot_cont

add layer with points for instance:

plot_cont +
  geom_point(data = data.frame(x = c(35, 47, 61),
                               y = c(22, 37, 15)),
             aes(x = x, y = y), color = "red")

you can add any type of layer this way: geom_line, geom_text to name a few.

enter image description here

EDIT2: to change the scale of the axis there are several options, one is to assign appropriate rownames and colnames to the matrix:

I will assign a sequence from 0 - 2 for the x axis and 0 - 5 to the y axis:

rownames(volcano) <- seq(from = 0,
                         to = 2,
                         length.out = nrow(volcano)) #or some vector like u
colnames(volcano) <- seq(from = 0,
                         to = 5,
                         length.out = ncol(volcano)) #or soem vector like v

as.data.frame(volcano) %>% 
  rownames_to_column() %>% 
  gather(key, value, -rowname) %>% 
  mutate(key = as.numeric(key), 
         rowname = as.numeric(rowname)) %>%
  ggplot() +
  geom_contour(aes(x = rowname, y = key, z = value))

enter image description here

3
  • Hi, your solution works, it draws the expected contour. There is just a problems: the scale of the x and y axes is not from -1 to 1.5 (u and v), but goes until 100.
    – Melvin86
    May 10, 2018 at 19:38
  • @Melvin86 added edits for both the deleted comment and the one addressing the scale of the x and y axes.
    – missuse
    May 10, 2018 at 19:49
  • Thank you missuse, this works perfectly. Adding the plot to my question for reference. Ciao!
    – Melvin86
    May 11, 2018 at 7:28
3

ggplot2 works most efficiently with data in long format. Here's an example with fake data:

library(tidyverse)  

u <- v <- seq(-1, 1.5, length.out = 100)

# Generate fake data
z = outer(u, v, function(a, b) sin(2*a^3)*cos(5*b^2))
rownames(z) = u
colnames(z) = v

# Convert data to long format and plot
as.data.frame(z) %>% 
  rownames_to_column(var="row") %>% 
  gather(col, value, -row) %>% 
  mutate(row=as.numeric(row), 
         col=as.numeric(col)) %>% 
ggplot(aes(col, row, z=value)) +
  geom_contour(bins=20) +
  theme_classic()

enter image description here

1
  • Hi eipi10, thank you for your answer, I already created the plot with missuse's code. I did not test but I think your code could work as well for my case as the codes are pretty similar.
    – Melvin86
    May 11, 2018 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.