perep1972
perep1972

Reputation: 147

White fill in the legends of the bubble maps in R using ggplot?

I'm posting this quite stupid question after unsuccessfully googleing for a while. I have the following situation:

dta1 <- data.frame(LON = runif(20)*100, LAT = runif(20)*100, VALUE = runif(20)*10)
dta2 <- data.frame(LON = runif(20)*100, LAT = runif(20)*100, VALUE = runif(20)*10)
ggplot() +
              geom_point(data=dta1,  color = "blue", alpha = 0.5, aes(x=LON, y=LAT, size = VALUE)) +
              geom_point(data=dta2,  color = "red",  alpha = 0.5, aes(x=LON, y=LAT, size = VALUE))

Which yields something like this (points' positions and sizes can change, it does not matter): enter image description here

The result is quite good, but I want the circles in the legend to be draw with black borders and filled with white. Any idea?

Upvotes: 0

Views: 102

Answers (3)

Edo
Edo

Reputation: 7858

Would this be an acceptable result?

I supposed your point was to make the size legend of an anonymous colour, unrelated to the blue or the red of the actual points.

set.seed(1)
dta1 <- data.frame(LON = runif(20)*100, LAT = runif(20)*100, VALUE = runif(20)*10)
dta2 <- data.frame(LON = runif(20)*100, LAT = runif(20)*100, VALUE = runif(20)*10)

library(ggplot2)
ggplot() +
 geom_point(data=dta1, alpha = 0.5, aes(x=LON, y=LAT, size = VALUE, colour = "value1")) +
 geom_point(data=dta2, alpha = 0.5, aes(x=LON, y=LAT, size = VALUE, colour = "value2")) +
 scale_color_manual(values = c(value1 = "blue", value2 = "red"))

enter image description here


Alternately, closely related to the "fill" answer:

library(ggplot2)
ggplot() +
geom_point(data = dta1, shape = 21, alpha = 0.5, aes(x = LON, y = LAT, size = VALUE, fill = "value1")) +
 geom_point(data = dta2, shape = 21, alpha = 0.5, aes(x = LON, y = LAT, size = VALUE, fill = "value2")) +
 scale_fill_manual(values = c(value1 = "blue", value2 = "red")) +
 theme_light() +
 guides(fill = FALSE)

enter image description here

Upvotes: 1

perep1972
perep1972

Reputation: 147

Alternatively, it could be something like this:

dta1$C <- 1
dta2$C <- 2
dta1 <- rbind(dta1, dta2)
ggplot(dta1, aes(x = LON, y = LAT, size = VALUE, fill = C)) +
   geom_point(shape=21) 

Which yields: enter image description here

But without the inferior (rectangle titled "C") part of the legend.

Upvotes: 0

Magnus Nordmo
Magnus Nordmo

Reputation: 951

Hmm. I dont know of a way of changing something in the legend only. In a way thats contrary to the point of the legend. This is the best i got.

library(ggplot2)
dta1 <- data.frame(LON = runif(20)*100, LAT = runif(20)*100, VALUE = runif(20)*10)
dta2 <- data.frame(LON = runif(20)*100, LAT = runif(20)*100, VALUE = runif(20)*10)
ggplot() +
  geom_point(data=dta1,  color = "blue", alpha = 0.5,shape = 21, aes(x=LON, y=LAT, size = VALUE)) +
  geom_point(data=dta2,  color = "red",  alpha = 0.5,shape = 21, aes(x=LON, y=LAT, size = VALUE)) + 
  theme_classic()

Created on 2020-12-09 by the reprex package (v0.3.0)

Upvotes: 0

Related Questions