在ggplot中绘制多边形链 [英] Drawing polygonal chains in ggplot
本文介绍了在ggplot中绘制多边形链的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在ggplot中绘制多个
解决方案
我无法在这里告诉你想要什么。
- 您是否指定了多个路径?我只看到一组
x
和y
值。 - 什么是
z
应该在做什么?
您可以通过
p0 < - ggplot(mtcars,aes(wt,mpg))绘制单个(未关闭的)路径
p <-p0 + geom_path(aes(x,y),data = data.frame(x,y))
或通过
p0 + geom_polygon(aes(x,y),data = data.frame(x,y),fill = NA,
color =black)
<如果你有多条路径,你可以将两个长向量中的所有顶点合并为 x
和 y
,构成一个 g
向量来区分路径,并使用 aes(x,y,group = g)
来获得路径/多边形分开绘制。
How to draw multiples polygonal chains in ggplot? I was writing something like:
x=c(1,3,4,5,6)
y=c(0.5,2,3,7,1)
z=c(8,2,6,7,8)
n=length(x)-1
library(ggplot2)
p <- ggplot(mtcars, aes(wt, mpg))
for (i in 1:n){
p <-p + geom_segment(aes(x = x[i], y = y[i], xend = x[i+1], yend = y[i+1]), colour = "red")
p$plot_env <- list2env(list(x=x,y=y))
}
Drawing two polygonal chains:
n=length(x)
pol1=c(x,y)
pol2=c(y,z)
g=c(rep(1,n),rep(2,n))
library(ggplot2)
p0 <- ggplot(mtcars,aes(wt,mpg))
p0 + geom_path(aes(pol1,pol2,group=g),data=data.frame(pol1,pol2))
解决方案
I can't quite tell what you want here.
- Are you specifying more than one path? I only see a single set of
x
andy
values. - What is
z
supposed to be doing?
You can draw a single (unclosed) path via
p0 <- ggplot(mtcars,aes(wt,mpg))
p <- p0 + geom_path(aes(x,y),data=data.frame(x,y))
Or a closed path via
p0 + geom_polygon(aes(x,y),data=data.frame(x,y),fill=NA,
colour="black")
If you do have more than one path, you can combine all the vertices in two long vectors x
and y
, make up a g
vector to distinguish paths, and use aes(x,y,group=g)
to get the paths/polygons drawn separately.
这篇关于在ggplot中绘制多边形链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文