Saara
Saara

Reputation: 115

Adjusting legend.title ,legend.text and legend color in ggplot2

I am unable to change the color of the Segmentation legend in the plot. I need two different colors for it as a text in the legend and as well as in the visual plot.

   er<-  ggmap(sq_map2) + 
   geom_point(data = sisquoc, size = 3,  aes(fill = Segmentation)) +
   geom_line(data = sisquoc, size = 3,  aes(color =SpeedMeterPerSecond)) +
   geom_text(data = sisquoc, aes(label = paste("  ", 
   as.character(Location_ids), sep="")), 
         angle = 60, hjust = 0, color = "sienna4",size = 6 ) 


   gg<- er  +   labs(x ="Longitude", y = "Latitude") +
   theme(axis.title = element_text(size=20), 
     panel.background = element_rect(fill = "white",size = 0.5, linetype = 
   "dotted"),
     panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour 
   = "black"), 
     panel.grid.minor = element_line(size = 0.5, linetype = 'dotted',colour 
   = "black"),
     panel.border = element_rect(colour = "black", fill=NA, size=0.5),
     axis.text.y   = element_text(size=18),
     axis.text.x   = element_text(size=18))

   gg  + theme(legend.position="right", 
         legend.title = element_text(colour="Black", size=18),
         legend.text = element_text(colour="black", size = 15),
         legend.background = element_rect(fill="grey90",
                                    size=0.5, linetype="solid", 
                                    colour ="black"))  + scale_color_continuous(name="Speed (m/s)\n")

Upvotes: 3

Views: 942

Answers (1)

adibender
adibender

Reputation: 7568

Something like the following should work. Just specify the legend title explicitly and add \n at the end of the string, which adds an extra blank row:

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col=Petal.Length))+ 
  geom_point() + scale_color_continuous(name="my scale\n")

Alternatively you could try changing the legend orientation, which however usually is most compact when the legend is at the bottom.

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col=Petal.Length))+ 
  geom_point() + theme(legend.direction = "horizontal", legend.position = "bottom")

Upvotes: 2

Related Questions