0

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)
2
  • df$Val <- rnorm(nrow(comb)) clear what the comb is Commented Feb 22, 2015 at 12:57
  • Sorry, 'comb' should be 'df' to generate numbers. Now changed. Commented Feb 22, 2015 at 15:23

1 Answer 1

1

I simplified your code some, corrected one point, and illustrated the technique with shapes.

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)
set.seed(1234) # needed for reproducibility
df$Val <- rnorm(nrow(df))  # changed comb to df
cols <- c("TRUE" = "red", "FALSE" = "green")
shapes <- c("TRUE" = 15, "FALSE" = 20) # added this for the shapes

ggplot(data=df) + 
  geom_point(aes(x=Test, y=Val, shape = State), size = 8) +  
  scale_shape_manual(values=shapes, breaks = c("TRUE", "FALSE"))  

enter image description here

EDIT Here are shapes and colors. Note that I added set.seed() above for reproducibility and tweaked cols and shapes:

ggplot(data=df) + 
  geom_point(aes(x=Test, y=Val, shape = State, color = State), size = 8) +  
  scale_shape_manual(values=shapes, breaks = c("TRUE", "FALSE")) +
  scale_color_manual( values= c("forestgreen", "cadetblue"), breaks = c("TRUE", "FALSE"))

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the example. It works with shapes but it is not possible to add:
Thanks for the example. It works with shapes but it is not possible to add: + scale_colour_manual(values=cols, breaks = c("TRUE", "FALSE")) to change the colour. If colour is specified within the aes it works. I guess it is it only possible to change parameters that have been specified within aes? That's something that I did not know. Also I just realised I can start over again by ggplot(data=gp$data), where gp is the old plot object...
I did both in the second plot. Does this answer your question?
Yes, thanks! I take it that this confirms that the parameters need to be specified inside geom_point(aes()) to be changeable using scale_*_manual?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.