I really like the ggplot2 package, but often run into supposedly basic problems that stops me for many hours. I have a bunch of ggplot objects from previous simulations that I wish to change the appearance of (shape, alpha, colour, size), rather than to re-run the simulations. There are many similar questions here on stackoverflow, but they are all slightly different and I have not yet been able to extract the overall knowledge to effortlessly create plots. I am happy if someone can point me in the right direction.
The plots were created with a code equivalent to this:
ggplot(data=df) + geom_point(aes(x=Test, y=Val, ymax=max(Val), group=State, colour=State), shape=20, position=position_dodge(.5), alpha=1/2)
Here is a reproducible example:
require(ggplot2)
# Create some data.
state <- c(TRUE,FALSE)
effect <- c(0.5, 1.0)
test <- c("A","B")
# Create combinations.
df <- expand.grid(Test=test, State=state, Effect=effect)
# Add some values.
df$Val <- rnorm(nrow(df))
# The plots were created this way.
gp <- ggplot(data=df) + geom_point(aes(x=Test, y=Val, ymax=max(Val), group=State, colour=State), shape=20, position=position_dodge(.5), alpha=1/2)
gp <- gp + facet_wrap(~ Effect)
gp <- gp + theme_bw()
print(gp)
# Change colours and re-order legend works.
cols <- c("TRUE" = "red","FALSE" = "blue")
gp <- gp + scale_colour_manual(values = cols, breaks = c("TRUE", "FALSE")) # Change and re-order legend.
print(gp)
# Change 'shape', 'size' or 'alpha' this way does not work.
cols <- c("TRUE" = 1,"FALSE" = 3)
gp <- gp + scale_shape_manual(values=cols, breaks = c("TRUE", "FALSE"))
print(gp)
cols <- c("TRUE" = 5,"FALSE" = 10)
gp <- gp + scale_size_manual(values=cols, breaks = c("TRUE", "FALSE"))
print(gp)
cols <- c("TRUE" = 1,"FALSE" = 1/5)
gp <- gp + scale_alpha_manual(values=cols, breaks = c("TRUE", "FALSE"))
print(gp)
Perhaps I should have created the plots differently? Any comments are appreciated. I have found that if I specify 'shape' inside 'aes', I am able to change shape, but not without problems:
# Put shape inside aes.
gp <- ggplot(data=df) + geom_point(aes(x=Test, y=Val, ymax=max(Val), group=State, colour=State, shape=State), position=position_dodge(.4), alpha=1/2)
gp <- gp + facet_wrap(~ Effect)
gp <- gp + theme_bw()
print(gp)
# Change colours and re-order legend this way works (but adds another legend).
cols <- c("TRUE" = "red","FALSE" = "blue")
#gp <- gp + scale_colour_manual(values = cols) # Changes colour.
gp <- gp + scale_colour_manual(values = cols, breaks = c("TRUE", "FALSE")) # Changes colour, but adds a second legend when re-ordered.
print(gp)
# Try to change shape.
cols <- c("TRUE" = 1,"FALSE" = 3)
gp <- gp + scale_shape_manual(values=cols, breaks = c("TRUE", "FALSE")) # Back to one legend, but 'FALSE' does not have a symbol.
print(gp)
# Change 'size' etc. this way does not work.
cols <- c("TRUE" = 5,"FALSE" = 10)
gp <- gp + scale_size_manual(values=cols, breaks = c("TRUE", "FALSE"))
print(gp)

