ggplot2 scale_color_manual not making legend - r

Here is my code. I'm getting the plot I want but it doesn't have a legend for the colors. I have Grade.7.ELA.4s...White as green and Grade.7.Math.4s as blue and thought the scale_color_manual would create a legend but when I plot it no legend appears.
PerW.vs.7ELA4s <- ggplot(Explore, aes(Explore$Percent.White, value)) +
geom_point(aes(Percent.White, Grade.7.ELA.4s...White),
color="green", alpha=.55) +
geom_point(aes(Percent.White, Grade.7.Math.4s...White),
color="blue", alpha=.5) +
geom_smooth(aes(Percent.White, Grade.7.ELA.4s...White),
color="green", alpha=.55, se=F) +
geom_smooth(aes(Percent.White, Grade.7.Math.4s...White),
color="blue", alpha=.5, se=F) +
scale_color_manual(name="", values=c("ELA"="green", "Math"="blue")) +
labs(y="# 7th Grade ELA 4's", x="Percent White")

Related

how to add a legend in ggplot in r

I would like to include a legend inside the top right of my plot, indicating the parameter values of the plots.
ggplot() +
geom_line(data=data1, aes(x=x, y=y1), color='green', linetype = "twodash", size=0.5) +
geom_line(data=data2, aes(x=x, y=y2), color='red', linetype="longdash", size=0.5) +
geom_line(data=data3, aes(x=x, y=y3), color='blue') +
theme_classic() +
labs(x='input, x',
y='output, f(x)')
Can someone please say how this is done. Thanks.
I think this is a very good source of "what to do with ggplot"
http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/#changing-the-position-of-the-legend
from there
# Position legend in graph, where x,y is 0,0 (bottom left) to 1,1 (top right)
bp + theme(legend.position=c(1, 1))
should/could do it?

legend to show multiple layers (ggplot)

I'm trying to show both the geom_line and the geom_point in the legend, however I can't seem to get the geom_line to appear.
graph <- ggplot(mar, aes(x=long, y=lat)) + xlab("Longitude") + ylab("Latitude") + labs(size = "Distance from predicted \n to known Roman road (m)")
graph + theme_light() + geom_point(aes(size=distance$NEAR_DIST)) + geom_line(color="white", size=0.5)
This generates a graph with geom_point (size of points are based on distance from the line in geom_line and another line), but I can't get the geom_line to appear in the legend. Any ideas on how to do this?
Answered by Craig:
graph <- ggplot(mar, aes(x=long, y=lat)) + xlab("Longitude") + ylab("Latitude") + labs(size = "Distance from predicted \n to known Roman road (m)")
graph + theme_dark() + geom_point(aes(size=distance$NEAR_DIST)) + geom_line(aes(color = "LCP Roman Road"), size = 0.5) + scale_color_manual(name = "tt", values = "white")
assigned color via an aesthetic.

Use position_jitterdodge to plot points, and add highlighted points that are also dodged

I have some data where x is categorical, y is numeric, and color.var is another categorical variable that I would like to color by. My goal is to plot all of the points using position_jitterdodge(), and then highlight a couple of the points, draw a line between them, and add labels, while making sure these highlighted points line up with the corresponding strips of points that were plotted using position_jitterdodge(). The highlighted points are aligned properly when all factors are present in the variable used to dodge, but it does not work well when some factors are missing.
Minimal (non-)working example
library(ggplot2)
Generate some data
d = data.frame(x = c(rep('x1', 1000), rep('x2', 1000)),
y = runif(n=2000, min=0, max=1),
color.var= rep(c('color1', 'color2'), 1000),
facet.var = rep(c('facet1', 'facet1', 'facet2', 'facet2'), 500))
head(d)
dd = d[c(1,2,3,4,1997,1998, 1999,2000),]
dd
df1 = dd[dd$color.var=='color1',] ## data for first set of points, labels, and the line connecting them
df2 = dd[dd$color.var=='color2',] ## data for second set of points, labels, and the line connecting them
df1
dw = .75 ## Define the dodge.width
Plot all points
Here are all of the points, separated using position_jitterdodge() and the aesthetic fill.
ggplot() +
geom_point(data=d, aes(x=x, y=y, fill=color.var), position=position_jitterdodge(dodge.width=dw), size=3, alpha=1, shape=21, color='darkgray') +
facet_wrap(~facet.var) +
scale_fill_manual(values=c( 'lightblue','gray'))+
theme(axis.title = element_blank()) +
theme(legend.position="top")
That works well.
Additional highlighted points.
Here is the same plot, with additional points in dd added.
ggplot() +
geom_point(data=d, aes(x=x, y=y, fill =color.var), position=position_jitterdodge(dodge.width=dw), size=3, alpha=1, shape=21, color='darkgray') +
geom_point(data=dd, aes(x=x, y=y, color=color.var ), position=position_dodge(width=.75), size=4 ) +
geom_line(data=dd, aes(x=x, y=y, color=color.var, group=color.var ), position=position_dodge(width=.75), size=1 ) +
geom_label(data=dd, aes(x=x, y=y, color=color.var, group=color.var, label=round(y,1)), position=position_dodge(width=.75), vjust=-.5) +
facet_wrap(~facet.var) +
scale_fill_manual(values=c( 'lightblue','gray'))+
scale_color_manual(values=c( 'blue', 'gray40')) +
theme(axis.title = element_blank())+
theme(legend.position="top")
This is what I want it to look like. However, this only works properly if both factors of the color.var variable are in the set of points to highlight.
If both factors aren't present in the new data, the horizonal alignment fails.
Highlight points, only one factor present
Here is an example where only the 'color1' factor (blue) is present. Note that data=dd was replaced with data=df1 (data that only contains blue highlighted dots) in this code.
ggplot() +
geom_point(data=d, aes(x=x, y=y, fill =color.var), position=position_jitterdodge(dodge.width=dw), size=3, alpha=1, shape=21, color='darkgray') +
geom_point(data=df1, aes(x=x, y=y, color=color.var ), position=position_dodge(width=.75), size=4 ) +
geom_line(data=df1, aes(x=x, y=y, color=color.var, group=color.var ), position=position_dodge(width=.75), size=1 ) +
geom_label(data=df1, aes(x=x, y=y, color=color.var, group=color.var, label=round(y,1)), position=position_dodge(width=.75), vjust=-.5) +
facet_wrap(~facet.var) +
scale_fill_manual(values=c( 'lightblue','gray'))+
scale_color_manual(values=c( 'blue', 'gray40')) +
theme(axis.title = element_blank())+
theme(legend.position="top") +
scale_x_discrete(drop=F)
The highlight blue dots appear between the blue and gray dots, instead of aligned with the blue dots. Note that the additional code scale_x_discrete(drop=F) had no apparent effect on the alignment.
A manual solution
One possible fix is to edit the x coordinate manually, like this
ggplot(data=d, aes(x=x, y=y)) +
geom_point(aes(fill=color.var), position=position_jitterdodge(dodge.width=dw), size=3, alpha=1, shape=21, color='darkgray') +
geom_point(data=df1, aes(x=as.numeric(x)-dw/4, y=y), alpha=.9, size=4 , color='blue') + ## first set of points
geom_line( data=df1, aes(x=as.numeric(x)-dw/4, y=y , group=color.var ), color='blue', size=1) + ## first line
geom_label(data=df1, aes(x=as.numeric(x)-dw/4, y=y , label=round(y,1)), color='blue', vjust=-.25)+ ## first set of labels
facet_wrap(~facet.var) +
scale_fill_manual(values=c( 'lightblue','gray'))+
theme(axis.title = element_blank() +
theme(legend.position="top")
An adjustment of 1/4 of the dodge.width seems to work. This works fine, but it seems like there should be a better way, especially since I will eventually want to do this with 4-5 sets of highlighted points/lines, which may all be all be the same color.var, like the blue 'color1' factor above. Repeating this 4-5 times would be cumbersome. I will also eventually want to do this will 5-10 different figures. I suppose dodge.width*1/4 will always work, and copying and pasting might do the trick, but would like to know if there is a better way.
Here is a solution based on #aosmith's comment. Basically, just need to add this code before using ggplot:
library(dplyr) ## needed for group_by()
library(tidyr) ## needed for complete()
df1 = df1 %>% group_by(facet.var, x) %>% complete(color.var)
That adds extra rows to the data so that all the levels of color.var are present. Then the code given in the question, along with a couple of small edits that fix the legend, can be used:
ggplot() +
geom_point(data=d , aes(x=x, y=y, fill =color.var), position=position_jitterdodge(dodge.width=dw), size=3, alpha=1, shape=21, color='darkgray', show.legend=T) +
geom_point(data=df1, aes(x=x, y=y, color=color.var ), position=position_dodge(width=.75), size=4, show.legend=T ) +
geom_line( data=df1, aes(x=x, y=y, color=color.var, group=color.var ), position=position_dodge(width=.75), size=1, show.legend=F ) +
geom_label(data=df1, aes(x=x, y=y, color=color.var, group=color.var, label=round(y,1)), position=position_dodge(width=.75), vjust=-.5, show.legend=F) +
facet_wrap(~facet.var) +
scale_fill_manual( values=c( 'lightblue','gray'), name='Background dots', guide=guide_legend(override.aes = list(color=c('lightblue', 'gray')))) +
scale_color_manual(values=c( 'blue', 'gray40') , name='Highlighted dots') +
theme(axis.title = element_blank())+
theme(legend.position="top")+
scale_x_discrete(drop=F)

ggplot line color "black" becomes red

I used the following script to make the graph below
hypttauplot <- qplot(fuclin_csf, fitHYPTTAU, data=selTAU, geom=c("smooth"),
method="glm", color='black', linetype=BL_HYPT) +
theme_classic() + xlab("Time (years)") + ylab("Tau (pg/ml)") +
scale_x_continuous(expand=c(0,0)) +
ggtitle("A. Hypertension") +
theme(legend.position = "none")
But now my questions is: why are the lines red instead of black? And how can I change them to black?
You have to use I() to set the aesthetics manually in qplot(), e.g. colour=I("black") (setting vs. mapping of aesthetics).
# mapping
qplot(carat, price, data=diamonds, color="black")
# equivalent to ggplot(data=diamonds, aes(carat, price, color="black")) + geom_point()
# setting
qplot(carat, price, data=diamonds, color=I("black"))
# equivalent to ggplot(data=diamonds, aes(carat, price), color="black") + geom_point()
You can use scale_colour_manual to specify an own set of mappings.

R - Background colour on ggplot split across a diagonal?

Using R, library(ggplot2)
I have a ggplot that is a perfect square. From the top left corner, to the bottom right corner, I have a line.
So say xlim(0,100), ylim(0,100) with geom_abline(intercept=100, slope=-1).
How do I make everything above the line have say a blue background and below a red?
library(ggplot2)
dat = data.frame(x=c(0,100), ymin=0, y=c(100,0), ymax=100)
ggplot(dat) +
geom_ribbon(aes(x, ymin=y, ymax=ymax), fill="blue") +
geom_ribbon(aes(x, ymin=ymin, ymax=y), fill="red") +
geom_abline(aes(intercept=100, slope=-1)) +
#geom_line(aes(x,y), lwd=1) + # If you just want a line with the same extent as the data
theme_bw()
And analogously for more general curves:
x=seq(0,100,0.1)
dat = data.frame(x=x, ymin=0, y=2*x + 3*x^2 - 0.025*x^3)
dat$ymax=max(dat$y)
ggplot(dat) +
geom_ribbon(aes(x, ymin=y, ymax=ymax), fill="blue") +
geom_ribbon(aes(x, ymin=ymin, ymax=y), fill="red") +
geom_line(aes(x,y), colour="yellow", lwd=1) +
theme_bw()

Resources