R ggplot2 geom_多边形未打印任何内容
我试图在ggplot中使用geom_多边形绘制一些三角形,但是,即使我没有得到任何错误,绘图还是完全为空 我按照文档中给出的代码来获取相同类型的数据,但这并没有真正的帮助(这里的第一个示例)。我确信我在数据帧的格式化过程中遗漏了一些关键步骤,但我不明白我错在哪里R ggplot2 geom_多边形未打印任何内容,r,ggplot2,R,Ggplot2,我试图在ggplot中使用geom_多边形绘制一些三角形,但是,即使我没有得到任何错误,绘图还是完全为空 我按照文档中给出的代码来获取相同类型的数据,但这并没有真正的帮助(这里的第一个示例)。我确信我在数据帧的格式化过程中遗漏了一些关键步骤,但我不明白我错在哪里 data = read.table(text = " start end av y1 y2 y3 719000 721000 720000 1 1 2 1199000 1201000 1200000 1 1 2 3039000 3041
data = read.table(text = "
start end av y1 y2 y3
719000 721000 720000 1 1 2
1199000 1201000 1200000 1 1 2
3039000 3041000 3040000 1 1 2
3679000 3681000 3680000 1 1 2
4119000 4121000 4120000 1 1 2
4999000 5001000 5000000 1 1 2", sep = " ", stringsAsFactors = F, header = T)
ids <- factor(paste(1:nrow(data), 1:nrow(data), sep = "."))
positions <- data.frame(
id = rep(ids, times = nrow(data)/2),
x = c(data$start, data$end, data$av),
y = c(data$y1, data$y2, data$y3)
)
values <- data.frame(
id = ids,
value = 1:length(ids)
)
datapoly <- merge(values, positions, by = c("id"))
ggplot(datapoly, aes(x = x, y = y)) + geom_polygon(aes(fill = value, group = id))
data=read.table(text=”
起始端av y1 y2 y3
719000 721000 720000 1 1 2
1199000 1201000 1200000 1 1 2
3039000 3041000 3040000 1 1 2
3679000 3681000 3680000 1 1 2
4119000 4121000 4120000 1 1 2
4999000 5001000 5000000 1 1 1 2“,sep=”,stringsAsFactors=F,header=T)
ids可能需要关闭多边形:
datapoly <- merge(values, positions, by = c("id"))
datapoly2<-rbind(datapoly,datapoly[1,])
datapoly2$id<-as.character(datapoly2$id)
datapoly2[19,1]<-"6.7"
library(ggplot2)
ggplot( datapoly2, aes(x = x, y = y)) + geom_polygon()
datapoly他们在那里,他们只是非常非常瘦。x比例的跨度超过400万,但每个多边形的宽度要小得多。例如,最后一条线的宽度仅为2000,因此它仅覆盖x轴宽度的0.048%,因此您所能看到的只是看起来像一条垂直线的东西。@camille已经提到了这个问题,您可以看到您的代码通过在id
变量上刻面来工作:ggplot(datapoly,aes(x=x,y=y,group=id))+geom_polygon()+facet_grid(~id,scales=“free”)
非常感谢@camile和@codenob!!现在我觉得自己很愚蠢>