R - ggplot2等高线图

时间:2018-05-08 21:11:19

标签: r ggplot2 contour

我正在尝试复制来自Andrew Ng的机器学习课程中关于Coursera in R的代码(因为课程是在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))

但是我得到了错误:美学必须是长度1或与数据(118)相同:颜色,x,y,形状

感谢。

编辑(添加从错误代码创建的绘图):

enter image description here

2 个答案:

答案 0 :(得分:5)

您需要将坐标转换为长格式。以下是使用火山数据集的示例:

data(volcano)

在基地R:

contour(volcano)

enter image description here

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

如果你想直接在基础R图中标记它,你可以使用库directlabels

首先将颜色/填充映射到变量:

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

enter image description here

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

上一个情节

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,仅举几例。

enter image description here

EDIT2:要更改轴的比例,有几个选项,一个是为矩阵指定适当的rownamescolnames

我将为0轴分配0到2的序列,为0轴分配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))

enter image description here

答案 1 :(得分:2)

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

enter image description here