我想在此范围内拟合一组X-Y坐标。 Y轴= 0至420,X轴= -130至130。 数据: a (它是从表中提取的,我使用data.table包来提取X和Y列)。所以“a”的数据如下:
x y
+ 20 34
+ 9 19
+ 15 91
+ 18 36
+ 18 34
+ 4 29
+ 47 24
+ 1 19
+ 14 103
+ -5 75
等
ggplot(a,aes(x,y)) +
geom_point() +
geom_segment(aes(x = 130, y = 0, xend = 130, yend =420))+
geom_segment(aes(x = -130, y = 0, xend = -130, yend =420))+
geom_segment(aes(x = 130, y = 0, xend = 0, yend =0))+
geom_segment(aes(x = -130, y = 0, xend = 0, yend =0))+
geom_segment(aes(x = -130, y = 420, xend = 0, yend =420))+
geom_segment(aes(x = 130, y = 420, xend = 0, yend =420))
以下是生成的图片:
有人可以帮我吗?
答案 0 :(得分:1)
您必须先确保数据类是数字。例如,如果数据是字符类,则会导致显示绘图错误。
a$x <- as.numeric(a$x)
a$y <- as.numeric(a$y)
require(ggplot2)
ggplot(a,aes(x,y)) +
geom_point() +
geom_segment(aes(x = 130, y = 0, xend = 130, yend =420))+
geom_segment(aes(x = -130, y = 0, xend = -130, yend =420))+
geom_segment(aes(x = 130, y = 0, xend = 0, yend =0))+
geom_segment(aes(x = -130, y = 0, xend = 0, yend =0))+
geom_segment(aes(x = -130, y = 420, xend = 0, yend =420))+
geom_segment(aes(x = 130, y = 420, xend = 0, yend =420))
输出:
示例数据:
require(data.table)
a <- fread("x y
20 34
9 19
15 91
18 36
18 34
4 29
47 24
1 19
14 103
-5 75")
答案 1 :(得分:1)
可能是这样的:
a <- read.table(text = "
x y
20 34
9 19
15 91
18 36
18 34
4 29
47 24
1 19
14 103
-5 75", header = T, sep = "")
library(ggplot2)
ggplot(a,aes(x,y)) +
geom_point() + coord_cartesian(xlim = c(-130,130), ylim = c(0,420)) +
geom_rect(xmin = -130, xmax = 130, ymin = 0, ymax = 420, fill = NA, color = "black")