R-ggplot2等高线图 [英] R - ggplot2 contour plot
问题描述
我正在尝试从R的Coursera上的Andrew Ng的机器学习课程中复制代码(因为该课程在八度中)。
I am trying to replicate the code from Andrew Ng's Machine Learning course on Coursera in R (as the course is in Octave).
基本上,我必须绘制一个多项式正则逻辑回归的非线性决策边界(在p = 0.5时)。
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)
其中:
u <- v <- seq(-1, 1.5, length.out = 100)
z是矩阵100x100,其中每个网格点的z值都为z。
数据尺寸为118x3。
and z is a matrix 100x100 with the values of z for every point of the grid. Dimension of data is 118x3.
我无法在ggplot2中做到这一点。有人知道如何在ggplot2中复制吗?我尝试过:
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))
但是我得到一个错误:美学必须为长度1或与数据(118)相同:颜色,x,y,形状
But I get the error: Aesthetics must be either length 1 or the same as the data (118): colour, x, y, shape
谢谢。
编辑(添加根据误用代码创建的图):
EDIT (Adding plot created from code of missuse):
推荐答案
您需要的是将坐标转换为长格式,以下是使用火山数据的示例设置:
What you need is to convert the coordinates into long format. Here is an example using volcano data set:
data(volcano)
$ b在基础R中的
$ b
:
in base 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
:
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
然后
library(directlabels)
direct.label(some_plot, list("far.from.others.borders", "calc.boxes", "enlarge.box",
box.color = NA, fill = "transparent", "draw.rects"))
要在特定坐标处添加标记,您只需添加具有适当数据的另一层即可:
to add markers at specific coordinates you just need to add another layer with appropriate data:
上一个情节
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")
您可以通过以下方式添加任何类型的图层: geom_line
, geom_text
仅举几例。
you can add any type of layer this way: geom_line
, geom_text
to name a few.
EDIT2:要更改轴的比例,有几个选项,一种方法是将适当的行名
和姓氏
分配给矩阵:
to change the scale of the axis there are several options, one is to assign appropriate rownames
and colnames
to the matrix:
我将为x轴分配一个从0-2的序列,为y轴分配一个从0-5的序列:
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))
这篇关于R-ggplot2等高线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!