0

I'm trying to plot 3 different cross sections that are going to be coloured by a certain factor. This all works fine, however for some reason the geom_line() function doesn't manage to interrupt the plotted line by the factors. Here is my code (I have removed all code explanations) so far:

library(ggplot2)

qpbase_soll <- ggplot(qps_soll, aes(X, Ysoll))
qpbase_soll <- qpbase_soll + geom_line(aes(color = Katsoll), size = 1) + facet_grid(Profil ~ .) + labs(x = "Horizontaldistanz [m]") + labs(y = "Höhe über Meer [m]")

farben_qp2 <- c("red", "darkgreen", "orange", "lightgreen")
qpbase_soll <- qpbase_soll + scale_colour_manual(values=farben_qp2)

qpbase_soll <- qpbase_soll + coord_fixed(ratio=2/1)

qpbase_soll <- qpbase_soll + scale_x_continuous(breaks=c(0,5,10,15,20,25,30)) + scale_y_continuous(breaks=c(630:637))

library(grid)
qpbase_soll <- qpbase_soll + theme(panel.margin = unit(1, "lines"))

qpbase_soll <- qpbase_soll + labs(colour= "Oberfläche")
qpbase_soll <- qpbase_soll + ggtitle("Querprofile SOLL-Zustand") + theme(plot.title= element_text(face="bold"))

kommentar_y4 <- data.frame(X = 23, Ysoll = 635, lab = "Alle Y-Achsen zweifach überhöht", Profil = factor("QP2"))
qpbase_soll <- qpbase_soll + geom_text(data = kommentar_y4, label = "Alle Y-Achsen zweifach überhöht", size = 3.0, color = "darkgrey") 

qpbase_soll

My data.frame() contains X and Y values, as well as a category for all these locations according to the colours they should receive. So the line should actually be interrupted for all categories. The data sheet has the following entries:

Profil    X    Ysoll    Katsoll 
   QP1  0.0    636.4    Terrain
   QP1  0.5    636.3    Terrain
   QP1  1.6    638.5    Feldweg
   QP1  1.8    638.5    Feldweg
   QP1  2.0    639.1    Terrain  
...

What am I doing wrong?

zx8754
  • 52,746
  • 12
  • 114
  • 209
dru87
  • 113
  • 6
  • you are probably missing `group = Katsoll` so ggplot knows which variable to group by `color = Katsoll` isnt enough, so your 3rd line should say: `geom_line(aes(color = Katsoll, group = Katsoll), size = 1)`, den Fehler hab ich auch bereits mehrfach gemacht und mich rumgeärgert, ist leider nicht sehr intuitiv – grrgrrbla Jul 28 '15 at 08:15
  • @grrgrrbla: Thanks for the suggestion, but unfortunately the plot looks exactly the same with the code `group = Katsoll` added to the third line... – dru87 Jul 28 '15 at 08:37
  • Thanks to @grrgrrbla I actually found the answer myself. The clue was in the `group = ` argument, as I found this [thread](http://stackoverflow.com/questions/10357768/plotting-lines-and-the-group-aesthetic-in-ggplot2). The solution was to simply set `group = 1`, as ggplot tries to creat a line for each factor otherwise. Thanks for the tipp. – dru87 Jul 28 '15 at 09:58

0 Answers0