R ggplot2:如何将每个数据点的y坐标写入其旁边?
示例数据集:R ggplot2:如何将每个数据点的y坐标写入其旁边?,r,R,示例数据集: n value 100000 20 200000 30 300000 25 400000 40 500000 12 以下代码: require(ggplot2) data <- read.table("test", sep = "\t", header = TRUE,) ggplot(data, aes(n, value)) + geom_point(aes(n,value)) + geom_line(aes(n,value)) dev.off() req
n value
100000 20
200000 30
300000 25
400000 40
500000 12
以下代码:
require(ggplot2)
data <- read.table("test", sep = "\t", header = TRUE,)
ggplot(data, aes(n, value)) +
geom_point(aes(n,value)) + geom_line(aes(n,value))
dev.off()
require(ggplot2)
数据如果在调用ggplot
中指定data
参数,则以下对geom_point等的调用将继承该参数,因此无需再次指定。使用dplyr管道符号(%>%)和geom_text
结果将是:
library(dplyr)
library(ggplot2)
tribble(
~n, ~value,
100000, 20,
200000, 30,
300000, 25,
400000, 40,
500000, 12
) %>% ggplot(aes(n, value)) +
geom_point() +
geom_line() +
geom_text(aes(label=value), nudge_y=1, nudge_x=-10000)
这与调用基本相同
ggplot(data, aes(n, value)) +
geom_point() +
geom_line() +
geom_text(aes(label=value), nudge_y=1, nudge_x=-10000)
如果在调用ggplot
中指定data
参数,则以下对geom_point等的调用将继承该参数,因此无需再次指定。使用dplyr管道符号(%>%)和geom_text
结果将是:
library(dplyr)
library(ggplot2)
tribble(
~n, ~value,
100000, 20,
200000, 30,
300000, 25,
400000, 40,
500000, 12
) %>% ggplot(aes(n, value)) +
geom_point() +
geom_line() +
geom_text(aes(label=value), nudge_y=1, nudge_x=-10000)
这与调用基本相同
ggplot(data, aes(n, value)) +
geom_point() +
geom_line() +
geom_text(aes(label=value), nudge_y=1, nudge_x=-10000)
根据@Roland的建议,您可以使用geom_text
或geom_标签
或注释
将文本添加到绘图中。以下是使用geom\u text
的代码:
data <- structure(list(n = c(100000L, 200000L, 300000L, 400000L, 500000L
), value = c(20L, 30L, 25L, 40L, 12L)), .Names = c("n", "value"
), class = "data.frame", row.names = c(NA, -5L))
library(ggplot2)
ggplot(data, aes(x=n, y=value)) +
geom_point(aes(n,value)) + geom_line(aes(n,value))+
geom_text(aes(label=value), hjust=c(-1,0,0,-1,2), vjust=c(1,-.5,1.5,0,0))
结果是:
也可以使用注释,如下所示:
ggplot(data, aes(x=n, y=value)) +
geom_point(aes(n,value)) + geom_line(aes(n,value))+
annotate("text", x=data$n, y=data$value, label=data$value,
hjust=c(-1,0,0,-1,2), vjust=c(1,-.5,1.5,0,0))
根据@Roland的建议,您可以使用geom_text
或geom_标签
或注释
将文本添加到绘图中。以下是使用geom\u text
的代码:
data <- structure(list(n = c(100000L, 200000L, 300000L, 400000L, 500000L
), value = c(20L, 30L, 25L, 40L, 12L)), .Names = c("n", "value"
), class = "data.frame", row.names = c(NA, -5L))
library(ggplot2)
ggplot(data, aes(x=n, y=value)) +
geom_point(aes(n,value)) + geom_line(aes(n,value))+
geom_text(aes(label=value), hjust=c(-1,0,0,-1,2), vjust=c(1,-.5,1.5,0,0))
结果是:
也可以使用注释,如下所示:
ggplot(data, aes(x=n, y=value)) +
geom_point(aes(n,value)) + geom_line(aes(n,value))+
annotate("text", x=data$n, y=data$value, label=data$value,
hjust=c(-1,0,0,-1,2), vjust=c(1,-.5,1.5,0,0))
另一个选项是库(ggrepel)
以自动最小化点/标签重叠。我认为它比position\u jitter()
更可靠,如果您不能/不想硬编码所有道奇值,它会很有用:
ggplot(data, aes(n, value)) +
geom_point() +
geom_line() +
# geom_text(aes(label = value), position = "jitter")
geom_text_repel(aes(label = value),
point.padding = unit(.25, "lines"))
geom\u label\u repel()
另一个选项是库(ggrepel)
以自动最小化点/标签重叠。我认为它比position\u jitter()
更可靠,如果您不能/不想硬编码所有道奇值,它会很有用:
ggplot(data, aes(n, value)) +
geom_point() +
geom_line() +
# geom_text(aes(label = value), position = "jitter")
geom_text_repel(aes(label = value),
point.padding = unit(.25, "lines"))
geom\u label\u repel()
只需使用几何图形文本。只需使用几何图形文本。