R/ ggplot2/ how to move from connected points in a scatter plot to filled and transparent triangles?
geometry, ggplot2, r
Solution
You can use `geom_polygon` for closed paths:
library(ggplot2)
p <- ggplot(df, aes(X1, X2))
p <- p + geom_polygon(aes(fill = factor(set)), alpha = .4)
p <- p + theme_bw()
p
Here, `alpha` is used to specify the degree of transparency.
Problem
Here are the x and y coordinates from a multidimensional scaling experiment: three cases with different distance metrics and scaling/ no scaling. "Set" is the metrics-scaling combination (1 to 6). Each case has a class label (0 or 4). ``` X1 X2 method scale class set 1 18.881729 -2.931111 euclidean no 0 1 2 -13.141592 -9.750710 euclidean no 4 1 3 -5.740138 12.681822 euclidean no 4 1 4 -21.886160 -15.467637 manhattan scaled 0 2 5 -16.755615 16.900148 manhattan scaled 4 2 6 38.641776 -1.432512 manhattan scaled 4 2 7 32.927820 -7.900971 minkowski no 0 3 8 -28.957697 -11.666982 minkowski no 4 3 9 -3.970123 19.567953 minkowski no 4 3 10 5.944225 25.819482 euclidean scaled 0 4 11 44.574669 -15.330675 euclidean scaled 4 4 12 -50.518894 -10.488807 euclidean scaled 4 4 13 14.287762 1.142065 manhattan no 0 5 14 -5.843410 -9.981600 manhattan no 4 5 15 -8.444351 8.839535 manhattan no 4 5 16 -24.838956 -8.194378 minkowski scaled 0 6 17 -11.435517 10.496471 minkowski scaled 4 6 18 36.274473 -2.302093 minkowski scaled 4 6 ``` and ggplotting: ``` p <- ggplot(df, aes(X1, X2)) p <- p + geom_point(aes(colour = factor(scale), shape = factor(method)), size=10) p <- p + geom_text(aes(label=class), size=5) p <- p + geom_line(aes(X1,X2, group=factor(set))) p <- p + theme_bw() p ``` I would like to make 6 filled and transparent triangles one for each group ("set"). The top triangle being Manhattan-No scaling. My experiments with geom_segment have not been successful and I am not sure whether geom_polygon is the right direction. Any advice? Thanks!