position <- (SCATGRAPH$position)
species <- (SCATGRAPH$species)
value <- (SCATGRAPH$value)
habitat <- (SCATGRAPH$habitat)
df <- data.frame(position,species,value,habitat)
ggplot(data=df, aes(x=habitat, y=value, group=species, colour=species, linetype=position)) + geom_line() + geom_point()
The above makes no graph, but elicits the message:
Error in `geom_line()`:
! Problem while converting geom to grob.
ℹ Error occurred in the 1st layer.
Caused by error in `draw_panel()`:
! `geom_line()` can't have varying colour, linewidth, and/or
alpha along the line when linetype isn't solid
Run `rlang::last_error()` to see where the error occurred.
The rlang::last_error() info is as follows
Backtrace:
1. base (local) `<fn>`(x)
2. ggplot2:::print.ggplot(x)
4. ggplot2:::ggplot_gtable.ggplot_built(data)
5. ggplot2:::by_layer(...)
12. ggplot2 (local) f(l = layers[[i]], d = data[[i]])
13. l$draw_geom(d, layout)
14. ggplot2 (local) draw_geom(..., self = self)
15. self$geom$draw_layer(...)
16. ggplot2 (local) draw_layer(..., self = self)
17. base::lapply(...)
18. ggplot2 (local) FUN(X[[i]], ...)
20. self$draw_panel(data, panel_params, coord, na.rm = FALSE)
21. ggplot2 (local) draw_panel(..., self = self)
When I try grouping by only one factor, either colour or linetype, it works exactly as expected
ggplot(data=df, aes(x=habitat, y=value, group=species, colour=species))+ geom_line()
Graph
The equivalent code also works when switching the "colour" out for "linetype", and again works if "species" is switched out for "position" (my other two-category variable - that I would like to associate with linetype).
There seem to be many ways of phrasing this request to R. Using geom_line(aes(linetype=position)) alone or in conjunction with the problem syntax above does not improve results nor does adding a scale_linetype_manual command as mentioned in other posts. Converting "species" or "position" to factors using factor() has not worked either.
EDIT: Here is my data
structure(list(habitat = structure(c(1L, 1L, 2L, 2L, 3L, 3L,
4L, 4L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L), levels = c("Upland interior",
"Upland edge", "Peatland edge", "Peatland interior"), class = "factor"),
position = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L), levels = c("On", "Off"), class = "factor"),
species = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("Deer", "Moose"), class = "factor"),
value = c(0.428571429, 0.31372549, 0.315789474, 0.315789474,
0.263157895, 0, 0.166666667, 0, 0.285714286, 0.196078431,
0, 0.263157895, 0.052631579, 0.157894737, 0, 0.15)), row.names = c(NA,
-16L), class = "data.frame")
Some of ggplot2's geoms (like geom_line) only have one color, linewidth, alpha, etc. per series of observations that should be grouped together aesthetically. By default, ggplot2 uses some heuristics to figure out which observations should be grouped together. The group aesthetic allows us to specify this explicitly. https://ggplot2.tidyverse.org/reference/aes_group_order.html
In your case, you have series which are distinguished by the combination of their species and their position. To make groups for each combination, we can use group = interaction(species, position). (We could also use paste(species, position) -- anything that gives a different value for each combination.)

(There is a separate but related question that sometimes comes up about how to depict a single series with varying aesthetics as it goes. For that, geom_segment and ggforce::geom_link are two good approaches.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With