R - ggplot2 等高线图

R - ggplot2 contour plot

我正在尝试在 R 中复制 Andrew Ng 在 Coursera 上的机器学习课程的代码(因为课程是 Octave)。

基本上我必须为多项式正则化逻辑回归绘制一个非线性决策边界(p = 0.5)。

我可以使用基础库轻松复制情节:

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

其中:

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

z 是一个 100x100 的矩阵,网格的每个点都有 z 的值。 数据维度为 118x3.

我无法在 ggplot2 中完成。有人知道如何在 ggplot2 中复制相同的内容吗?我试过:

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

但我得到错误:Aesthetics must be either length 1 or the same as the data (118): colour, x, y, shape

谢谢。

编辑(添加由误用代码创建的图):

ggplot2 在处理长格式数据时效率最高。这是一个假数据的例子:

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()

你需要的是将坐标转换成长格式。下面是一个使用火山数据集的例子:

data(volcano)

在基础 R 中:

contour(volcano)

使用 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))

如果您想像在基础 R 图中那样直接标记它,您可以使用库 directlabels:

首先将color/fill映射到一个变量:

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

然后

library(directlabels)

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

要在特定坐标处添加标记,您只需添加具有适当数据的另一层:

上一个剧情

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

添加带有点的图层,例如:

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

您可以通过这种方式添加任何类型的图层:geom_linegeom_text 等等。

EDIT2:要更改轴的比例有几种选择,一种是将适当的 rownamescolnames 分配给矩阵:

我将为 x 轴分配 0 - 2 的序列,为 y 轴分配 0 - 5 的序列:

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))