Skip to content

Instantly share code, notes, and snippets.

@karawoo
Created April 19, 2016 21:45
Show Gist options
  • Save karawoo/571f9ac339dbabc1a81d43da305df266 to your computer and use it in GitHub Desktop.
Save karawoo/571f9ac339dbabc1a81d43da305df266 to your computer and use it in GitHub Desktop.
## Load ggplot package
library("ggplot2")
## Create sample data
dat <- structure(list(x = c(2.69753522456158, 9.77329733015504,
0.579014160879888, 6.01934352936223,
5.53224114519544, 7.52123757028021, 1.1418573057279,
5.81543114883825, 1.98635061332025,
8.98725295660552),
y = c(1.1341254551895, 8.69686821205542, 1.0851298507303,
0.175342436507344, 7.66043466781266,
0.173072947375476, 6.66614091775846,
2.26951052718796, 7.18269514336716, 7.8130083645694),
addition = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L),
.Label = c("a", "b"),
class = "factor"),
transect = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L),
.Label = c("c", "d"),
class = "factor"),
pH = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L),
.Label = c("6", "7", "8"),
class = "factor")),
.Names = c("x", "y", "addition", "transect", "pH"),
row.names = c(NA, -10L), class = "data.frame")
## Plot with border color representing addition, fill representing pH, and shape
## representing transect. This uses two shapes (#23 and 24) which allow filling.
## I'm not sure why the legend for pH doesn't show multiple colors though...
ggplot(dat, aes(x = x, y = y)) +
geom_point(size = 5, stroke = 2, # stroke sets the border width
aes(color = addition, fill = pH, shape = transect)) +
scale_shape_manual(values = c(23, 24))
## Another option is to plot two sets of points, one on top of the other. If the
## top set is smaller than the bottom set, then you can see the bottom set
## forming a "border" around the top set. This causes different weirdness with
## the legend though.
ggplot(dat, aes(x = x, y = y)) +
geom_point(size = 6, aes(color = addition, shape = transect)) +
geom_point(size = 4, aes(color = pH, shape = transect))
## Just for kicks, here is another idea that doesn't use two different sets of
## colors but instead uses facetting to create a small multiples plot by
## addition.
ggplot(dat, aes(x = x, y = y)) +
geom_point(size = 4, aes(color = pH, shape = transect)) +
facet_wrap(~ addition)
@karawoo
Copy link
Author

karawoo commented Apr 19, 2016

Here is how these plots look:
plot1
plot2
plot3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment