Adding a legend to a ggplot2 plot which contains multiple elements - r

This is a similar question to here however I could not get their solution to work for me. I want to add a legend to a ggplot2 plot, when using more than one independent data frame to generate the plot.
Here is an example based on the data sets available in R:
a=longley
b=iris
a$scaled=scale(a$Unemployed,center=TRUE,scale=TRUE)
b$scaled=scale(b$Sepal.Length,center=TRUE,scale=TRUE)
ggplot () +
geom_density(data=a,aes(x=scaled),fill="red",alpha=0.25) +
geom_density(data=b,aes(x=scaled),fill="blue",alpha=0.25) +
scale_colour_manual("",breaks=c("a","b"),values=c("red","blue"))
The plot produced looks like this:
ie. no legend.
How would I add a legend to this?

Very minor syntactic change required. Move the fill= part into the aes() statement in each geom.
a=longley
b=iris
a$scaled=scale(a$Unemployed,center=TRUE,scale=TRUE)
b$scaled=scale(b$Sepal.Length,center=TRUE,scale=TRUE)
ggplot () +
geom_density(data=a,aes(x=scaled,fill="red"),alpha=0.25) +
geom_density(data=b,aes(x=scaled,fill="blue"),alpha=0.25)
This should work alone and will give you the default r color scheme. Or, if you really want to change the colors from the defaults, you can add the manual scale. However, since you want the scale to apply to the fill parameter, make sure to specify scale_fill_manual rather than scale_colour_manual.
ggplot () +
geom_density(data=a,aes(x=scaled,fill="red"),alpha=0.25) +
geom_density(data=b,aes(x=scaled,fill="blue"),alpha=0.25) +
scale_fill_manual("",breaks=c("a","b"),values=c("red","blue"))
If you wanted to change the colors of the lines you would do that with the color aesthetic and would then be able to use the scale_color_manual or scale_colour_manual (same thing) option.
ggplot() +
geom_density(data=a, aes(x=scaled, fill="red", color="yellow"), alpha=0.25) +
geom_density(data=b, aes(x=scaled, fill="blue", color="green"), alpha=0.25) +
scale_fill_manual(values=c("red","blue")) +
scale_color_manual(values=c("yellow", "green"))

Related

Defining colours in combined map

At the moment I'm working on a combined map in which on the one hand choropleths will be shown and on the other hand bubbles will be added.
For the most part, I manage to do this, but there are problems with the color design.
I can only determine the colour of the bubbles within the geom_point function. However, as soon as I want to take the bubble shape with an outline colour and use the fill argument within aes(), the colours of the choropleths, which are defined in scale_fill_manual, are used.
Hence my question: Is it possible to use a second scale_fill argument for the bubble colour?
Or how can I change the legend so that the extra value that defines the bubble color is not added to the choropleth values?
Here is an example for better understanding:
#load data
lapply(c("rgdal","ggplot2","broom"),require,character.only=TRUE)
URL<-"https://ec.europa.eu/eurostat/cache/GISCO/distribution/v2/nuts/download/ref-nuts-2016-10m.shp.zip"
fil<-basename(URL)
if(!file.exists(fil))download.file(URL,fil)
fils<-unzip(fil)
shp<-grep("shp$",fils,value=TRUE)[4]
lay<-ogrListLayers(shp)[1]
de<-readOGR(shp,lay)
de<-de[de$CNTR_CODE=="DE",]
de.df<-tidy(de,region="NUTS_ID")
#choropleth data
values<-data.frame(group=unique(de.df$group),
emp=runif(length(unique(de.df$group)),1,100))
de.df<-merge(de.df,values,by="group")
de.df$emp_c<-cut(de.df$emp,breaks=c(seq(0,100,by=20)))
#point/bubble data
nuts_centr<-coordinates(de)
proj<-data.frame(group=de$NUTS_ID,
long=nuts_centr[,1],
lat=nuts_centr[,2],
size=runif(length(unique(de$NUTS_ID)),1,100))
#colours
col<-colorRampPalette(c("#fff7fb","#014636"))
This is the plot where the bubble colour is determined within the given colour palette of the choroplets and the legend is accordingly wrong:
ggplot() +
geom_polygon(data=de.df,
aes(long,lat,group=group,fill=emp_c),
alpha=0.75,colour="gray",lwd=0.1) +
geom_point(data=proj,
aes(long,lat,size=size,fill="#ef6548"),
colour="black",pch=21,alpha=.75) +
scale_size_continuous(name="Employment",range=c(1,8)) +
scale_fill_manual(name="Projects",values=col(7)) +
theme(line=element_blank(),
axis.text=element_blank(),
axis.title=element_blank(),
panel.background=element_blank()) +
coord_equal()
Resulting in:
This is what it should look like, apart from the missing outline colour of the bubbles:
ggplot() +
geom_polygon(data=de.df,
aes(long,lat,group=group,fill=emp_c),
alpha=0.75,colour="gray",lwd=0.1) +
geom_point(data=proj,
aes(long,lat,size=size),
colour="#ef6548",alpha=.75) +
scale_size_continuous(name="Employment",range=c(1,8)) +
scale_fill_manual(name="Projects",values=col(6)) +
theme(line=element_blank(),
axis.text=element_blank(),
axis.title=element_blank(),
panel.background=element_blank()) +
coord_equal()
Resulting in:
As a novice with geodata and with R I am grateful for any tips, also in other areas, which may have been written inefficiently.

Easiest way of editing labels in legend when using color and shape in aes

I wonder if there is a function like:
scale_color_hue()
but for shapes.
Editing the labels of a legend when using colors in aes is esay with that function, but I cannot do it when mixing colors and shapes.
For instance, the command:
ggplot(subdata, aes(x=factor(rGPUs), y=Speedup, colour=factor(Factor), shape=factor(Factor), group=Factor, ymin=0)) +
geom_line(size=1) + geom_point(size=4) +
scale_color_hue(labels = c("1X", "1.5X", "2X"))
generates this chart:
I would like to plot it without the second legend. Nevertheless, I would not like to use the functions:
scale_color_manual()
scale_shape_manual()
since they need the labels together with the values, such as it is done in:
Editing legend (text) labels in ggplot
R manually set shape by factor
among many others.

Using color-filled circles for ggplot legends

I have the following code
TRP_C<-100/(100+650)
FPR_C<-200/(200+650)
C<-data.frame(TPR=TRP_C,FPR=FPR_C)
TRP_D<-120/(120+30)
FPR_D<-350/(350+500)
D<-data.frame(TPR=TRP_D,FPR=FPR_D)
ggplot(NULL, aes(x=FPR, y=TPR)) +
geom_point(data=C,shape=1,aes(fill="A"),size=4,color="red")+
geom_point(data=D,shape=1,aes(fill="B"),size=4,color="green")
The problem is it gives me a ggplot which the points are not clear on it at all.
I think, if i can make the points filled then it would be more clear in the diagram.
So, how can i make the legend ,and points filled?
Use shape (insert value from 21-25) inside geom_point() and scale_fill_manual for colors.
So your code looks like this
ggplot(NULL, aes(x=FPR, y=TPR)) +
geom_point(data=C,shape=21,aes(fill="A"),size=4) +
geom_point(data=D,shape=21,aes(fill="B"),size=4) +
scale_fill_manual(values=c("red", "green"))
And output

Using a uniform color palette among different ggplot2 graphs with factor variable

I am using ggplot2 to create several plots about the same data. In particular I am interested in plotting observations according to a factor variable with 6 levels ("cluster").
But the plots produced by ggplot2 use different palettes every time!
For example, if I make a bar plot with this formula I get this result (this palette is what I expect to obtain):
qplot(cluster, data = data, fill = cluster) + ggtitle("Clusters")
And if I make a scatter plot and I try to color the observations according to their belonging to a cluster I get this result (notice that the color palette is different):
ggplot(data, aes(liens_ratio,RT_ratio)) +
geom_point(col=data$cluster, size=data$nombre_de_tweet/100+2) +
geom_smooth() +
ggtitle("Links - RTs")
Any idea on how to solve this issue?
I can't be certain this will work in your specific case without a reproducible example, but I'm reasonably confident that all you need to do is set your color inside an aes() call within the geom you want to color. That is,
ggplot(data, aes(x = liens_ratio, y = RT_ratio)) +
geom_point(aes(color = cluster, size = nombre_de_tweet/100+2)) +
geom_smooth() +
ggtitle("Links - RTs")
If all plots you make use the same data and this basic format, the color palette should be the same regardless of the geom used. Additional elements, such as the line from geom_smooth() will not be changed unless they are also explicitly colored.
The palette will just be the default one, of course; to change it look into scale_color_manual.

geom_text positions per group

I am using geom_line, geom_point, and geom_text to plot something like the picture below:
I am grouping, and coloring my data frame, but I want the geom_text not to be so close to each other.
I want to put the one text on top, and the other on bottom. Or at least, hide the one of the two. Is there any way I can do this?
You can specify custom aesthetics in different geom_text() calls. You can include only a subset of the data (such as just one group) in each call, and give each geom_text() a custom hjust or vjust value for each subset.
ggplot(dat, aes(x, y, group=mygroups, color=mygroups, label=mylabel)) +
geom_point() +
geom_line() +
geom_text(data=dat[dat$mygroups=='group1',], aes(vjust=1)) +
geom_text(data=dat[dat$mygroups=='group2',], aes(vjust=-1))

Resources