R 是否有更好的方法将这些线段添加到ggplot?
有没有更好的方法将所有这些线段添加到ggplot中,而不是将每条线段都添加到ggplot中R 是否有更好的方法将这些线段添加到ggplot?,r,ggplot2,annotate,R,Ggplot2,Annotate,有没有更好的方法将所有这些线段添加到ggplot中,而不是将每条线段都添加到ggplot中 annotate("segment", x = -250, xend = -250, y = -50, yend = 315, colour = "gray50") + annotate("segment", x = 250, xend = 250, y = -50, yend = 315, colour = "gray50") + annotate("segment", x = -250,
annotate("segment", x = -250, xend = -250, y = -50, yend = 315, colour = "gray50") +
annotate("segment", x = 250, xend = 250, y = -50, yend = 315, colour = "gray50") +
annotate("segment", x = -250, xend = 250, y = -50, yend = -50, colour = "gray50") +
annotate("segment", x = -211, xend = -211, y = -50, yend = 110, colour = "gray50") +
annotate("segment", x = 211, xend = 211, y = -50, yend = 110, colour = "gray50") +
annotate("segment", x = -30, xend = 30, y = -10, yend = -10, colour = "gray50") +
annotate("segment", x = -80, xend = -80, y = -50, yend = 140, colour = "gray50") +
annotate("segment", x = -60, xend = -60, y = -50, yend = 140, colour = "gray50") +
annotate("segment", x = 60, xend = 60, y = -50, yend = 140, colour = "gray50") +
annotate("segment", x = 80, xend = 80, y = -50, yend = 140, colour = "gray50") +
annotate("segment", x = -80, xend = 80, y = 140, yend = 140, colour = "gray50") +
annotate("segment", x = 0, xend = 0, y = -10, yend = -5, colour = "gray50")
将绘图存储在变量中:
library(ggplot2)
data(diamonds)
chart <- ggplot(diamonds, aes(x=depth, y=price)) +
geom_point(alpha=0.1)
库(ggplot2)
数据(钻石)
图表将绘图存储在变量中:
library(ggplot2)
data(diamonds)
chart <- ggplot(diamonds, aes(x=depth, y=price)) +
geom_point(alpha=0.1)
库(ggplot2)
数据(钻石)
图表你可以给注释提供向量,例如,你的前三个点(我懒得全部输入)
或者,如果数据位于数据框中,则使用geom_段
层(根据需要扩展到所有点):
您可以为注释提供向量,例如,为您的前三个点(我懒得全部键入)
或者,如果数据位于数据框中,则使用geom_段
层(根据需要扩展到所有点):
你想要的坐标应该都在一个数据框中。是的,把你的数据放在向量中,然后做(前三行)注释(“段”,x=c(-250250,-250),xend=c(-250250250),y=c(-50,-50),yend=c(315315315,-50),color=“gray50”)
。或者,如果它在一个数据帧中,只需添加一个geom_段(data=your_段数据,aes(…)
层。你所需的坐标都应该在一个数据帧中。是的,把你的数据放在向量中,并做(前三行)注释(“段”,x=c(-250,250,-250),xend=c(-250,250),y=c(-50,-50,-50),yend=c(315,315,-,-50),color=“gray50”)
。或者,如果它在一个数据帧中,只需添加一个geom_段(data=your_段数据,aes(…)
层。annotate
和geom_段
接受向量,你为什么要循环?不知道,你的答案更好。annotate
和geom_段
接受向量,你为什么要循环?不知道,你的答案更好。
for (segment in segment_coords) {
chart <- chart + annotate(
"segment",
x = segment["x"],
xend = segment["xend"],
y = segment["y"],
yend = segment["yend"],
colour = "gray50"
)
}
chart
annotate("segment", x = c(-250, 250, -250),
xend = c(-250, 250, 250),
y = c(-50, -50, -50),
yend = c(315, 315, -50),
color = "gray50")
annotation_data = data.frame(x1 = c(-250, 250, -250),
x2 = c(-250, 250, 250),
y1 = c(-50, -50, -50),
y2 = c(315, 315, -50))
geom_segment(data = annotation_data,
mapping = aes(x = x1, xend = x2, y = y1, yend = y2),
color = "gray50)