0

Here is
code sample:

require(rgdal)
require(ggmap)
require(maptools)
library(RColorBrewer)



polska <- get_map(

  location=c(lon=20.9589934, lat=53.5149404), 
  zoom=7, 
  maptype="roadmap" 

)

mapa <- ggmap(polska)

nazwa <- c('LEKARZ - SPECJALISTA ONKOLOGII I HEMATOLOGII DZIECIĘCEJ',
           'LEKARZ - SPECJALISTA ONKOLOGII KLINICZNEJ',
           'LEKARZ - SPECJALISTA RADIOLOGII I DIAGNOSTYKI OBRAZOWEJ',
           'LEKARZ - SPECJALISTA RADIOTERAPII ONKOLOGICZNEJ','LEKARZ - SPECJALISTA KARDIOCHIRURGII',
           'LEKARZ - SPECJALISTA KARDIOLOGII',
           'LEKARZ - SPECJALISTA KARDIOLOGII DZIECIĘCEJ',
           'LEKARZ - RADIOLOGIA I DIAGNOSTYKA OBRAZOWA',
           'LEKARZ - SPECJALISTA CHIRURGII ONKOLOGICZNEJ',
           'LEKARZ - SPECJALISTA GINEKOLOGII ONKOLOGICZNEJ','LEKARZ - SPECJALISTA ONKOLOGII I HEMATOLOGII DZIECIĘCEJ',
           'LEKARZ - SPECJALISTA ONKOLOGII KLINICZNEJ',
           'LEKARZ - SPECJALISTA RADIOLOGII I DIAGNOSTYKI OBRAZOWEJ',
           'LEKARZ - SPECJALISTA RADIOTERAPII ONKOLOGICZNEJ','LEKARZ - SPECJALISTA KARDIOCHIRURGII',
           'LEKARZ - SPECJALISTA KARDIOLOGII',
           'LEKARZ - SPECJALISTA KARDIOLOGII DZIECIĘCEJ',
           'LEKARZ - RADIOLOGIA I DIAGNOSTYKA OBRAZOWA',
           'LEKARZ - SPECJALISTA CHIRURGII ONKOLOGICZNEJ',
           'LEKARZ - SPECJALISTA GINEKOLOGII ONKOLOGICZNEJ','LEKARZ - SPECJALISTA ONKOLOGII I HEMATOLOGII DZIECIĘCEJ',
           'LEKARZ - SPECJALISTA ONKOLOGII KLINICZNEJ',
           'LEKARZ - SPECJALISTA RADIOLOGII I DIAGNOSTYKI OBRAZOWEJ',
           'LEKARZ - SPECJALISTA RADIOTERAPII ONKOLOGICZNEJ','LEKARZ - SPECJALISTA KARDIOCHIRURGII',
           'LEKARZ - SPECJALISTA KARDIOLOGII',
           'LEKARZ - SPECJALISTA KARDIOLOGII DZIECIĘCEJ',
           'LEKARZ - RADIOLOGIA I DIAGNOSTYKA OBRAZOWA',
           'LEKARZ - SPECJALISTA CHIRURGII ONKOLOGICZNEJ',
           'LEKARZ - SPECJALISTA GINEKOLOGII ONKOLOGICZNEJ','LEKARZ - SPECJALISTA ONKOLOGII I HEMATOLOGII DZIECIĘCEJ',
           'LEKARZ - SPECJALISTA ONKOLOGII KLINICZNEJ',
           'LEKARZ - SPECJALISTA RADIOLOGII I DIAGNOSTYKI OBRAZOWEJ',
           'LEKARZ - SPECJALISTA RADIOTERAPII ONKOLOGICZNEJ','LEKARZ - SPECJALISTA KARDIOCHIRURGII',
           'LEKARZ - SPECJALISTA KARDIOLOGII',
           'LEKARZ - SPECJALISTA KARDIOLOGII DZIECIĘCEJ',
           'LEKARZ - RADIOLOGIA I DIAGNOSTYKA OBRAZOWA',
           'LEKARZ - SPECJALISTA CHIRURGII ONKOLOGICZNEJ',
           'LEKARZ - SPECJALISTA GINEKOLOGII ONKOLOGICZNEJ','LEKARZ - SPECJALISTA ONKOLOGII I HEMATOLOGII DZIECIĘCEJ',
           'LEKARZ - SPECJALISTA ONKOLOGII KLINICZNEJ',
           'LEKARZ - SPECJALISTA RADIOLOGII I DIAGNOSTYKI OBRAZOWEJ',
           'LEKARZ - SPECJALISTA RADIOTERAPII ONKOLOGICZNEJ','LEKARZ - SPECJALISTA KARDIOCHIRURGII',
           'LEKARZ - SPECJALISTA KARDIOLOGII',
           'LEKARZ - SPECJALISTA KARDIOLOGII DZIECIĘCEJ',
           'LEKARZ - RADIOLOGIA I DIAGNOSTYKA OBRAZOWA',
           'LEKARZ - SPECJALISTA CHIRURGII ONKOLOGICZNEJ')

Lat <- c(53.772908,         54.179697,         53.77253,         53.769866,         53.824712,         53.770479,         54.155989,         53.77253,         53.900444,
         53.769866,         53.769866,         53.784476,         53.776881,         51.765548,         53.770238,         53.626192,         54.179697,         54.155989,         53.770238,         54.179697,       54.161008,         53.241782,
         53.693514,         54.497984,         54.179697,         53.770238,         54.179697,         54.12597,         53.793636,         53.769866,
         53.77253,         53.813859,         54.082334,         53.769866,         54.179697,         54.153899,        53.693514,
         53.769866,         54.179697,         51.765548,         53.820852,         53.144716,         53.586071,         53.144716,         53.793636,
         53.424625,         53.241782,         53.769866,         53.557383)


Lon <- c(20.484436,
         19.429063,         20.471922,         20.492113,         22.3542,         20.477155,         19.425851,         20.471922,         20.573356,         20.492113,         20.492113,         20.494275,
         20.505175,         19.455863,         20.479557,         21.798478,         19.429063,         19.425851,         20.479557,         19.429063,         19.418135,
         20.192547,         19.975791,         18.544899,         19.429063,         20.479557,         19.429063,
         20.591212,         22.33971,         20.492113,         20.471922,         22.384305,         21.377269,       20.492113,
         19.429063,         19.423336,         19.975791,         20.492113,         19.429063,         19.455863,         22.364016,         23.191729,         19.566362,         23.191729,         22.33971,         19.587424,         20.192547,
         20.492113,         20.986225
)
personel <- data.frame(nazwa,Lat,Lon)

moja.paleta <- brewer.pal(10, "Set3")
personel$kolor <- moja.paleta[personel$nazwa]

punkty <- mapa+ geom_point( aes(x=Lon, y=Lat,fill=personel$kolor)
                               ,data=subset(personel,nazwa=nazwa)#& dziedzina=='onkologia'))
                              ,size=6
                              , color= "gold"
                              , pch=21

                              ) 

plot(punkty)

My question:

How to change guide label titles from this color names from moja.paleta to personel$nazwa Also how to change size of the legend labels and make this legend vertical not horizontal?

IMAGE

enter image description here

banshe
  • 75
  • 2
  • 9
  • This site works best when you ask one question at a time since only one answer can be accepted. You should attempt to include sample data in the question itself rather than requiring a download from another site. See [tips for creating a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). We're not here to write the code for you, we're here to help. – MrFlick Feb 24 '16 at 20:06

2 Answers2

3

I think this should work

moja.paleta <- brewer.pal(10, "Set3")
mapa + geom_point( aes(x=Lon, y=Lat,fill=nazwa),
   data=subset(personel,nazwa=nazwa)#& dziedzina=='onkologia')),
   size=6,
   color= "gold",
   pch=21) + 
   scale_fill_manual( values=setNames(moja.paleta,levels(personel$nazwa)) ) + 
   guides(fill  = guide_legend(ncol = 2)) + 
   theme(legend.position="bottom")

We use scale_fill_manual to set the colors for each of the levels and use the fill= aesthetic to color the points (note there is no $ there -- you should avoid that when specifying aesthetics.

Also i moved the guides to the bottom and changed the number of columns. And finally I had to choose a different color brewer pallet because the "Blues" did not have 10 values.

enter image description here

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • thanks a lot for the help, one more question if possible, If I add another column with some other variable is it possible to add diffrent shapes for points (using that variable?) – banshe Feb 24 '16 at 21:12
  • Yes. There'a a shape aesthetic for that (`aes(shape=)`) – MrFlick Feb 24 '16 at 21:19
  • And is it possible to use only shapes from pch =21:25 ? those are the only shapes that can be coloured I belive. `mapa + geom_point( aes(x=Lon, y=Lat,fill=nazwa, SHAPE= DZIEDZINA), data=subset(personel,nazwa=nazwa), size=6, color= "gold", #pch=21 )` something like that will works? and where to add those points shape I want to use? – banshe Feb 24 '16 at 21:44
  • That's getting a bit off topic. If you have a separate question about how ggplot2 handles shapes, I recommend searching for other solutions or opening a new question. – MrFlick Feb 24 '16 at 21:45
2

Everything that works in ggplot is applicable to ggmap. So for example this web page should answer most of your questions: http://www.cookbook-r.com/Graphs/Legends_%28ggplot2%29/

Because you don't give any example data, I can't verify if this works:

change the names of each legend item:

+ scale_fill_manual(labels=c("a", "b", "c")) 

Change size legend labels, change position legen

+ theme(legend.text = element_text(size = 16),
        legend.position="bottom")   #or top or so
Wave
  • 1,216
  • 1
  • 9
  • 22
  • I edited my post, now its with example data. ;) ill try your code, thx - I havent try with manual labels... I tryed to make a list with my personel$name and then put it somehow into legend – banshe Feb 24 '16 at 20:51
  • you can do labels=unlist(your.list) or you can just change the names in the dataframe, so you don't have to bother changing them in the plot – Wave Feb 24 '16 at 20:53