Reputation: 25
I'm new to R. My goal is to create a nice xy-scatter plot with regression lines and confidence ellipse. I've managed to teach myself how to create the xy-plots, regression lines and circles using ggplot2, but now I'm stuck with the colors.
In my example with random numbers I have 4 data groups, and I want to change the colors. I managed to change them using colorbrewer, but I don't like any of their presets and want to define my own.
Here's my code, so far with colorbrewer:
library(ggplot2)
data = read.csv("test.csv")
data$type <- factor(data$type, levels=c("bbb","ccc","aaa","ddd"))
g = aes(x=alpha, y=beta, color=type, group=type)
p_test <- ggplot(subset(data, id %in% c("a1", "b1"))) +
geom_point(g) +
stat_ellipse(g) +
geom_smooth(g, method=lm, se=FALSE) +
scale_color_brewer(palette="Set3") +
expand_limits(x=c(1,11), y=c(1,11)) +
xlab(expression(paste(italic(blah[p]), " in q ", r^-1))) +
ylab(expression(paste(lambda, " in K ", l^-2*M^-3))) +
ggtitle("blahblah") +
theme(legend.justification=c(0,1), legend.position=c(0,1), legend.title=element_blank())
The data table is like this:
> data
id alpha beta type
1 a1 1.2 1.1 aaa
2 a1 6.6 3.4 bbb
3 a1 3.7 6.4 ccc
4 a1 2.7 6.4 aaa
5 a1 5.3 6.3 bbb
6 a1 4.5 8.1 ccc
7 a1 10.5 9.0 bbb
8 a1 9.9 5.3 aaa
9 a1 2.8 5.3 bbb
10 a1 5.4 1.6 ccc
11 a1 3.4 10.7 aaa
12 a1 8.0 4.0 bbb
13 a1 6.1 6.5 ccc
14 a1 8.8 9.0 bbb
15 a1 8.1 1.1 aaa
16 a1 4.2 3.8 bbb
17 a1 9.1 1.2 ccc
18 a1 4.4 2.3 aaa
19 a1 8.8 7.8 bbb
20 a1 11.0 2.5 ccc
21 a1 7.0 9.4 bbb
22 b1 9.5 7.8 ddd
23 b1 7.6 4.5 ddd
24 b1 10.8 7.5 ddd
25 b1 5.6 4.5 ddd
26 b1 8.9 11.0 ddd
27 c1 8.1 7.0 fff
28 c1 7.5 9.3 fff
29 c1 3.1 6.0 ggg
Then I tried to change the color. And that's where my problems start:
1.) First I wanted to try the rainbow gradient. So I replaced
scale_color_brewer(palette="Set3") +
with
scale_colour_gradientn(colours=rainbow(4)) +
Which gave me an error "Discrete value supplied to continuous scale". What does this mean?
2.) Next, I tried defining fixed colors using
scale_fill_manual(values=c("red", "blue", "green", "orange")) +
This returned a plot, but not with the defined colors. They look like the defaults? Why?
3.) At last, I tried to define my own gradient. I want it to range from yellow to red. So I used
scale_colour_gradientn(colours=c("yellow", "red")) +
Which again resulted in an error: Discrete value supplied to continuous scale.
Then I gave up.
Could someone please enlighten me what I'm doing wrong? Why is the colorbrewer working, but not the rest?
Upvotes: 1
Views: 963
Reputation: 2289
Alistaire has already answered the question. Since the aesthetic color
is mapped to a factor
variable, ggplot expects the color scale to be discrete in nature (like your #2 above).
So try this:
library(ggplot2)
data = read.csv("test.csv")
data$type <- factor(data$type, levels=c("bbb","ccc","aaa","ddd"))
g = aes(x=alpha, y=beta, color=type, group=type)
p_test <- ggplot(subset(data, id %in% c("a1", "b1"))) +
geom_point(g) +
stat_ellipse(g) +
geom_smooth(g, method=lm, se=FALSE) +
expand_limits(x=c(1,11), y=c(1,11)) +
xlab(expression(paste(italic(blah[p]), " in q ", r^-1))) +
ylab(expression(paste(lambda, " in K ", l^-2*M^-3))) +
ggtitle("blahblah") +
theme(legend.justification=c(0,1), legend.position=c(0,1), legend.title=element_blank())
# These work fine
p_test + scale_color_manual(values = rainbow(4))
p_test + scale_color_manual(values = c("red", "blue", "green", "orange"))
p_test + scale_color_manual(values = RColorBrewer::brewer.pal(4, "Set3"))
p_test + scale_color_discrete(h = c(0,360), c = 100, l = 50)
# This wont work
p_test + scale_color_gradient(low = "blue", high = "red")
The error Error: Discrete value supplied to continuous scale
is saying that the color scale is continuous in nature but the variable is discrete in nature (factor
).
Hope this helps...
Upvotes: 1