Reputation: 636
I am creating a scatterplot (around 1,500,000 points), and I running regressions through it based on a factor variable (see below "halfs"). This is the picture:
As you can see it is difficult to see the "red" regression lines.
This is the data:
For_Cov Per_chg halfs
1 0.8372001 0.002400000 upper half
2 0.7236001 0.002800111 upper half
3 0.6036000 0.000800000 upper half
4 0.8540000 0.000000000 upper half
5 0.9080001 0.003200000 upper half
6 0.8248000 0.000000000 upper half
7 0.1132000 0.000000000 upper half
8 0.2044000 0.007600000 upper half
9 0.2476001 0.085200000 upper half
10 0.2368000 0.003600000 upper half
This is the code:
ggplot(grid_gdp_full, aes(x = For_Cov, y = Def_Chg, group = factor(halfs))) +
geom_point(aes(colour = halfs), alpha = 0.1) +
stat_smooth(method = "lm", formula = y ~ x + I(x^2), size = 4, fullrange=TRUE, aes(group = halfs, colour = halfs), alpha = 1) +
xlab("Initial") +
ylab("Percent") +
ylim(0,0.10) +
scale_x_reverse() +
theme_bw() +theme(
axis.text=element_text(size=12)
,axis.title=element_text(size=14,face="bold")
,plot.background = element_blank()
,panel.grid.major = element_blank()
,panel.grid.minor = element_blank()
,panel.border = element_blank()
,panel.background = element_blank()
) +
theme(axis.line = element_line(color = 'black'))
Does anyone how to change the code so that the regression lines will have different colors (regression line is created by stat_smooth) - possibly grey and blue for the two factor levels without changing the initial color of the points?
Here is the code I am using at the moment and I am still not able to get the two different colored lines:
ggplot(grid_gdp_full, aes(x = For_Cov, y = Def_Chg, fill = factor(halfs))) +
geom_point(aes(colour = halfs), alpha = 0.1, colour="transparent",shape=21) +
stat_smooth(method = "lm", formula = y ~ x + I(x^2), size = 2, fullrange=TRUE, aes(group = factor(halfs)), alpha = 1) +
xlab("Initial") +
ylab("Percent") +
ylim(0,0.1) +
scale_x_reverse() +
theme_bw() +theme(
axis.text=element_text(size=12)
,axis.title=element_text(size=14,face="bold")
,plot.background = element_blank()
,panel.grid.major = element_blank()
,panel.grid.minor = element_blank()
,panel.border = element_blank()
,panel.background = element_blank()
) +
theme(axis.line = element_line(color = 'black')) +
scale_colour_manual(values = c("red","blue")) +
scale_fill_manual(values = c("grey","green"))
dev.off()
Upvotes: 0
Views: 10812
Reputation: 59355
OK. I see what you want now (I think...): you want the line groups to be a different color than the point groups. In essence you need four colors: two for point groups and two for the line groups.
This worked on your sample, recoded with For_Cov<0.5
to "lower" to split the data.
grid_gdp_full$halfs <- ifelse(grid_gdp_full$For_Cov<0.5,"lower half","upper half")
grid_gdp_full <- cbind(grid_gdp_full,col =
ifelse(grid_gdp_full$halfs=="lower half","lm: lower","lm: upper"))
ggplot(grid_gdp_full, aes(x = For_Cov, y = Per_chg)) +
geom_point(aes(colour = factor(halfs)), alpha = 0.9, size=5) +
stat_smooth(method = "lm", formula = y ~ x + I(x^2), size = 4, fullrange=TRUE,
aes(group = col, colour = factor(col)), alpha = 1) +
xlab("Initial") +
ylab("Percent") +
ylim(0,0.10) +
scale_x_reverse() +
theme_bw() +theme(
axis.text=element_text(size=12)
,axis.title=element_text(size=14,face="bold")
,plot.background = element_blank()
,panel.grid.major = element_blank()
,panel.grid.minor = element_blank()
,panel.border = element_blank()
,panel.background = element_blank()
) +
theme(axis.line = element_line(color = 'black'))
Produces this:
I made the points bigger and set alpha=0.9
so you can see the points in this small sample. Also you reversed the x axis, which makes "lower" and "upper" kind of confusing.
Upvotes: 2
Reputation: 15441
Taking advantage of point shape 21 we can use fill
for points and colour
for lines. Setting colours by scale_manual as we want. Note the colour=transparent
in geom_point
so we remove colour borders around points.
ggplot(mtcars, aes(x=cyl,y=mpg,fill=factor(gear))) +
geom_point(aes(fill=factor(gear)),colour="transparent",shape=21) +
stat_smooth(aes(colour=factor(gear)),method="lm",se = FALSE) +
scale_colour_manual(values = c("red","blue", "green")) +
scale_fill_manual(values = c("orange","pink", "red"))
Upvotes: 4