我现在用R绘图。我希望如下绘制图表:
[]
我尝试使用geom_label_repel,代码为:
install.packages("ggplot2")
install.packages("ggrepel")
library(ggplot2)
library(ggrepel)
data = structure(list(x = 1200, slope = 1, intercept = 3200), .Names = c("x",
"slope", "intercept"), row.names = c(NA, -1L), class = "data.frame")
colnames(data) = c("x", "slope", "intercept")
annotation_data = structure(list(x = c(NA, NA, NA), y = c(1200, 1200, 1200), annotation = c(NA,
NA, NA)), .Names = c("x", "y", "annotation"), row.names = c(NA, -3L), class = "data.frame")
annotation_data[seq(4,123,3),"y"] = seq(100,4000,100)
annotation_data[seq(5,123,3),"y"] = seq(100,4000,100)
annotation_data[seq(6,123,3),"y"] = seq(100,4000,100)
annotation_data[seq(1,123,3),"x"] = (data$intercept - annotation_data[seq(1,123,3),"y"]) / data$slope
annotation_data[seq(2,123,3),"x"] = (data$intercept - annotation_data[seq(2,123,3),"y"] - 150) / data$slope
annotation_data[seq(3,123,3),"x"] = (data$intercept - annotation_data[seq(3,123,3),"y"] - 400) / data$slope
annotation_data[1,"annotation"] = paste("0% LKW 2% Steigung,\n10% LKW 2% Steigung,\n20% LKW 2% Steigung",sep = "")
annotation_data[2,"annotation"] = paste("10% LKW 4% Steigung,\n20% LKW 4% Steigung",sep = "")
annotation_data[3,"annotation"] = paste("10% LKW 4% Steigung",sep = "")
y = ggplot(annotation_data) + geom_point(aes(x = x, y = y), size = 0) + geom_label_repel(aes(x = x,y = y, label = annotation),box.padding = unit(2, "lines"),segment.color = 'black') +
geom_abline(slope = - data$slope, intercept = data$intercept, size = 1, linetype = "dashed") +
geom_segment(aes(x = (data$intercept - 1500) / data$slope, y = 1500, xend = (data$intercept - 500) / data$slope, yend = 500), linetype = "solid", size = 1) +
geom_abline(slope = - data$slope, intercept = data$intercept - 150, size = 1, linetype = "dashed") +
geom_segment(aes(x = (data$intercept - 1650) / data$slope, y = 1500, xend = (data$intercept - 650) / data$slope, yend = 500), linetype = "solid", size = 1) +
geom_abline(slope = - data$slope, intercept = data$intercept - 400, size = 1, linetype = "dashed") +
geom_segment(aes(x = (data$intercept - 1900) / data$slope, y = 1500, xend = (data$intercept - 900) / data$slope, yend = 500), linetype = "solid", size = 1) +
xlab(expression(Q[F]~"[Mfz/h]")) + ylab(expression(Q[R]~"[Mfz/h]")) + scale_y_continuous(breaks=seq(0, 2000, 200), limits = c(0,2000), expand = c(0,0))+ scale_x_continuous(breaks=seq(0, 4000, 500), limits = c(0,4000), expand = c(0,0))
实际上,我创建了一个点矩阵" annotation_data"在代码中,试图避免重叠,因为" geom_label_repel"声称标签将避免重叠点数据。但是,对我来说似乎它不起作用....
结果是: [
正如您所看到的,标签与其他线条重叠,并且引线的斜率也不同,这使得图形有点混乱。是否可以纯粹用R?
生成图1