j_eiros
j_eiros

Reputation: 86

Modyfing the Legend in ggplot2

I've got a problem interacting with the labels in ggplot2.

I have two data sets (Temperature vs. Time) from two experiments but recorded at different timesteps. I've managed to merge the data frames and put them in a long fashion to plot them in the same graph, using the melt function from the reshape2 library. So, the initial data frames look something like this:

> d1
    step   Temp
1  512.5 301.16
2  525.0 299.89
3  537.5 299.39
4  550.0 300.58
5  562.5 300.20
6  575.0 300.17
7  587.5 300.62
8  600.0 300.51
9  612.5 300.96
10 625.0 300.21
> d2
   step   Temp
1   520 299.19
2   540 300.39
3   560 299.67
4   580 299.43
5   600 299.78
6   620 300.74
7   640 301.03
8   660 300.39
9   680 300.54
10  700 300.25

I combine it like this:

> mrgd <- merge(d1, d2, by = "step", all = T)
step Temp.x Temp.y
1  512.5 301.16     NA
2  520.0     NA 299.19
...

And put it into long format for ggplot2 with this:

> melt1 <- melt(mrgd3, id = "step")
> melt1
    step variable  value
1  512.5   Temp.x 301.16
2  520.0   Temp.x     NA
...

Now, I want to for example do a histogram of the distribution of values. I do it like this:

p <- ggplot(data = melt1, aes(x = value, color = variable, fill = variable)) + geom_histogram(alpha = 0.4)

I get this plot

My problem is when I try to modify the Legend of this graph, I don't know how to! I've followed what is suggested in the R Graphics Cookbook book, but I've had no luck.

I've tried to do this, for example (to change the labels of the Legend):

> p + scale_fill_discrete(labels = c("d1", "d2"))

But I just create a "new" Legend box, like so

enter image description here

Or even removing the Legend completely

> p + scale_fill_discrete(guide = F)

I just get this

enter image description here

Finally, doing this also doesn't help

> p + scale_fill_discrete("")

Again, it just adds a new Legend box

enter image description here

Does anyone know what's happening here? It looks as if I'm actually modyfing another Label object, if that makes any sense. I've looked into other related questions in this site, but I haven't found someone having the same problem as me.

Upvotes: 2

Views: 797

Answers (2)

Nick Kennedy
Nick Kennedy

Reputation: 12640

The most straightforward thing to do would be to not use reshape2 or merge at all, but instead to rbind your data frames:

dfNew <- rbind(data.frame(d1, Group = "d1"),
  data.frame(d2, Group = "d2"))
ggplot(dfNew, aes(x = Temp, color = Group, fill = Group)) +
  geom_histogram(alpha = 0.4) +
  labs(fill = "", color = "")

If you wanted to vary alpha by group:

ggplot(dfNew, aes(x = Temp, color = Group, fill = Group, alpha = Group)) +
  geom_histogram() +
  labs(fill = "", color = "") +
  scale_alpha_manual("", values = c(d1 = 0.4, d2 = 0.8))

Note also that the default position for geom_histogram is "stacked". There won't be overlap of the bars unless you use geom_histogram(position = identity).

Histogram

Upvotes: 1

Vlo
Vlo

Reputation: 3188

Get rid of the aes(color = variable...) to remove the scale that belongs to aes(color = ...).

ggplot(data = melt1, aes(x = value, fill = variable)) + 
  geom_histogram(alpha = 0.4) + 
  scale_fill_discrete(labels = c("d1", "d1")) # Change the labels for `fill` scale

enter image description here

This second plot contains aes(color = variable...). Color in this case will draw colored outlines around the histogram bins. You can turn off the scale so that you only have one legend, the one created from fill

ggplot(data = melt1, aes(x = value, color = variable, fill = variable)) +
  geom_histogram(alpha = 0.4) +
  scale_fill_discrete(labels = c("d1", "d1")) +
  scale_color_discrete(guide = F) # Turn off the color (outline) scale

enter image description here

Upvotes: 4

Related Questions