0

I am now plotting with R. I hope to plot graph as follows:
[enter image description here]

I tried with geom_label_repel and the code is:

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))

Actually, I create a point matrix "annotation_data" in the code, trying to avoid overlapping, because "geom_label_repel" claims that the labels will avoid to overlap the point data. However, for me it seems that it does not work....

And the result is: [enter image description here

As you can see, the labels are overlapped with other lines and also the slope of the leader lines are different which makes the graph a little bit messy. Is that possible to generate graph 1 purely with R?

  • 1
    Maybe just use annotate and position them yourself? Probably easier than to use calculations – Robin Gertenbach Mar 10 '17 at 15:32
  • Hello @RobinGertenbach, thank you for your advice! I tried once, but it seems that geom_label_repel is the only possible way to get a line connecting labels and the line..... With annotate, I do not if it is possible. – strawrange Mar 10 '17 at 15:49
  • @strawrange, If you provide some reproducible data I can attempt to help. [How to make a great reproducible example R](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Reilstein Mar 11 '17 at 01:46
  • Thank you so much for your help! I have already updated my code~@Reilstein – strawrange Mar 11 '17 at 11:35

0 Answers0