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.
Related
I am trying to link the new order of the items of my legend to certain colors.
However, everytime I try something, I have to chose either between getting the order of the items as I want it or chosing the colors I want.
Here are the codes I used (they are both the exact same except for the part in bold that is the last line):
1). Colors:
TPC_T2<-ggplot(T2_M4M1, aes(x=Temperature, y=mm_per_day, colour=Population, group=Population))
+ geom_line()
+ geom_point()
+ theme(panel.background =element_rect(fill="white", colour="lightgrey"))
**+ scale_color_manual(values=c("forestgreen", "royalblue","lightcoral"))**
2). Order:
TPC_T2<-ggplot(T2_M4M1, aes(x=Temperature, y=mm_per_day, colour=Population, group=Population))
+ geom_line()
+ geom_point()
+ theme(panel.background = element_rect(fill="white",colour="lightgrey"))
**+scale_colour_discrete(breaks=c("SAR~200m","MOR~900m","PAC~1600m"))**
I tried to add these two lines (in bold) in the same code but it doesn't work. I also tried to do it like this:
+ scale_fill_manual(breaks=c("SAR~200m","MOR~900m","PAC~1600m"), values=c("royalblue", "forestgreen", "lightcoral"))
But it doesn't work neither.
I would like to have the order by altitude and with that colors :
SAR~200m in red
MOR~900m in green
PAC~1600m in blue
Do you have any idea why it doesn't work ?
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
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"))
Working with RStudio 0.98.1103, I am creating two versions of exactly the same graph: One with colors and one without. Since both graphs are exactly the same (apart from the coloring) I want to avoid typing nearly the same commands again. Hence, I create the colored plot, save it, manipulate it to make it black-grey-white and save the reduced version:
library(ggplot2)
bp <- ggplot(data=PlantGrowth, aes(x=group, y=weight)) +
geom_line(aes(color=group)) + theme(legend.position="none")
bp_bw <- bp + theme_bw() +
geom_line() + theme(legend.position="none")
ggsave("bp_bw.png", bp_bw)
Although bp looks quite normal, bp_bw doesn't. There is still a blury color shining behind the black bars (red - green - blue):
Closeup:
How can I get rid of this colors, i.e. remove all color completely from bp? Only restriction: I have to create the colored graphs first (although of course a different order would work).
I think a better solution is to create a base and only add the coloring part when needed:
bp <- ggplot(data=PlantGrowth, aes(x=group, y=weight)) +
theme_bw() + theme(legend.position="none")
bp_col <- bp + geom_line(aes(color=group))
bp_bw <- bp + geom_line()
This (more-or-less) makes sense. Your bp_bw code doesn't get rid of the old colored lines, it just adds black lines on top. Anti-aliasing as the image is displayed/saved lets some of the color through on the edges.
My recommendation is to modify the color scale rather than overplot black on top:
bp_bw2 = bp + scale_color_manual(values = rep("black", 20)) + theme_bw()
This will change the colors to all black rather than plotting black on top of colors. The rep("black", 20) is kind of a hack. Apparently values aren't recycled by scale_color_manual, but extra values aren't used so you need to give it a vector at least as long as the number of colors.
This also has the advantage of not needing to repeat the geom call, and if you had previously defined a color scale this will overwrite it. If you want to be more general you could also add a scale_fill_manual(), and you probably want to specify guide = FALSE so that you don't get a very unhelpful legend.
You also might want to check out scale_colour_grey, just because it's B&W doesn't mean all the colors have to be the same.
I know I'm not the first to ask a question in this arena but I haven't been able to figure out the solution to my particular quandary. Here's a stripped-down example of my problem.
data<-data.frame(Est=c(1:20,1:20),Measured=c(1:5,5:9,1:6,3:6,1:6,3:6,1:4,4,4:8),variable=c(rep("Plot1",20),rep("Plot2",20)))
p<-ggplot(data,aes(y=Est,x=Measured,shape=variable))
p<- p + geom_point(stat="identity") +coord_fixed(xlim=c(0,25),ylim=c(0,25)) + theme_bw()
p #If you plot the figure at this point, the points stand alone in the legend
p<-p+ geom_abline(intercept=0,slope=1,aes(linetype="1:1",color="1:1"),show_guide=TRUE)
p # Once the geom_abline is added, there are lines through the points. :(
p<-p+scale_color_manual(name="Lines",
values=c("1:1"="black"))
p<- p + scale_linetype_manual(name="Lines",
values=c("1:1"=2))
p<-p + scale_shape_manual(values=c(0,20), name = "")
p<- p+xlab(expression(paste("Measured volume(",ducks^{3},"",ha^{-1},")",sep="")))
p<-p+ ylab(expression(paste("Estimated volume (",ducks^{3},"",ha^{-1},")",sep="")))
As you can see, the legend for the points includes slashes (which I think are actually a line), and I would really prefer that the points were alone.
While the example code has only 1 line and linetype, the actual figure I've made includes five different lines of varying colors and linetypes, and thus I need a solution that allows me to include multiple geom_abline calls with color and linetype specified.
And no, I'm not really measuring the volume of anything in ducks, although that would be really entertaining to study...
Override the aesthetic mapping:
p + guides(shape = guide_legend(override.aes = list(linetype = 0)))
I always end up trying to override aesthetics by setting them to NULL, but for some reason that intuition is usually wrong.