how to separate bubble plot legend by variable in ggplot2 - r

I'm trying to build a bubble plot in ggplot2 that has two variables (Tmin and Tmax). The problem is that the legends are layered one on top of the other. I would like two separate legends. Also, I would like to adjust the bubble increment in the legend from 4 to 6 bubble sizes. Here's what I have thus far:
ggplot() + geom_path(data = westUS, aes(x = long, y = lat, group = group)) +
geom_point(data = myData, aes(x = long, y = lat, size = Tmin), pch = 1, color = "blue", stroke = 0.3) + scale_size_continuous(range = c(-2, 7.5)) + theme_bw() +
geom_point(data = myData, aes(x = long, y = lat, size = Tmax), pch = 1, color = "red", stroke = 0.3)
This is how the plot looks currently
Thanks!

Related

How to create an legend for ggplot for just one series of data and then add legend for additional horizontal lines?

I'm wanting to prepare a simple plot with some points and horizontal lines with a legend. The code below generates the desired plot and a legend but the legend symbols are combinations of the shape and line, when I would just like a shape for the shape and a line for the line.
dat <- iris %>% select(Sepal.Length)
dat$Type <- "Sepal.Length"
ggplot() +
geom_point(data = dat, aes(x = as.numeric(row.names(dat)), y = Sepal.Length, colour = Type), shape = 10, size = 2) +
geom_hline(aes(yintercept = 6, colour = "Some line"), linetype = "dashed")
Custom linetypes and shapes are assigned using scale_*_manual, like so:
dat %>%
ggplot() +
geom_point(aes(x = as.numeric(row.names(dat)), y = Sepal.Length, shape = Type), size = 2) +
geom_hline(aes(yintercept = 6, linetype = 'Some line')) +
scale_linetype_manual(values = c('Some line' = 'dashed')) +
scale_shape_manual(values = c('Sepal.Length' = 10))

How add legend in ggplot

I've written this code:
ggplot() +
geom_sf(aes(fill = dat$color_province)) +
theme_void() +
geom_point(data = producer,
aes(x = producer$MX, y = producer$MY), size = 3, col = "green", shape = 17, alpha = 0.6) +
geom_point(data = distribution,
aes(x = distribution$MX, y = distribution$MY), size = 4.5, col = "yellow", shape = 15) +
geom_point(data = retailer,
aes(x = retailer$MX, y = retailer$MY), size = 3, col = "slateblue", shape = 16) +
geom_point(data = Demand,
aes(x = Demand$MX, y = Demand$MY, size = Demand$De), col = "slateblue", shape = 17, alpha = 0.7) +
scale_fill_manual(values = c("#ff3333", "#ffc266"),
name = "Situation")
and now I want to add a legend to identify all points in my plot. How can I do it?
Here's an example on some data that everyone can run, since it uses built-in datasets that come with R. Here, I made color and size be dynamic aesthetics with the name of the series, and then mapped those series values to different aesthetic values using scale_*_manual, where * are the aesthetics you want to vary by series. This generates an automatic legend. By giving each aesthetic the same name ("source" here), ggplot2 knows to combine them into one legend.
(By the way, it's unnecessary and can lead to errors to refer to variables in ggplot2 aesthetics using the form retailer$MY; each geom will assume the variable is within the data frame referred to with data =, so you can just use MY in that case.)
ggplot() +
geom_point(data = mtcars,
aes(x = wt, y = mpg, color = "mtcars", size = "mtcars")) +
geom_point(data = attitude,
aes(x = rating/20, y = complaints/3, color = "attitude", size = "attitude")) +
scale_color_manual(values = c("mtcars" = "slateblue", "attitude" = "red"), name = "source") +
scale_size_manual(values = c("mtcars" = 3, "attitude" = 4.5), name = "source")

ggplot2 palette legend does not show

I just started using ggplot2 R package and the following question should be very trivial, however I spent 2h on it without success.
I just need to show the scale_fill_distiller legend of the RdBu palette from -1 to 1 (red to blue) on my ggplot.
Here's a code example:
## Load ggplot2 package
require(ggplot2)
## Create data.frame for 4 countries
shape = map_data("world") %>%
filter(region == "Germany" | region == 'Italy' | region == 'France' | region == 'UK')
## Order data.frame by country name
shape = shape[with(shape, order(region)), ]
rownames(shape) = NULL # remove rownames
##### Assign 4 different values (between -1 and 1) to each country by creating a new column 'id'
## These will be the values to be plotted with ggplot2 palette
shape[c(1:605),'id'] = 0.2
shape[c(606:1173),'id'] = -0.4
shape[c(1174:1774),'id'] = -0.9
shape[c(1775:2764),'id'] = 0.7
##### Make plot
ggplot() +
## plot countries borders
geom_polygon(data = shape, aes(x = long, y = lat, group = group, fill = id), colour = 'black') +
## adjust coordinates
coord_map() +
## remove any background
ggthemes::theme_map() +
## add colour palette
scale_fill_distiller(palette = 'RdBu', limits = c(1, -1), breaks = 50)
The legend of the RdBu palette should pop out automatically but here it doesn't. Is there any layer that is masking it?
OR
Is there any way to create a new legend from scratch and add it to the above plot?
I need something like the picture below, but from -1 to 1 (Red to Blue) and vertical:
Thanks
The range specified in limits should be c(min, max) and not c(max, min):
this works as expected:
library(ggmap)
library(ggplot2)
library(ggthemes)
ggplot() +
geom_polygon(data = shape,
aes(x = long, y = lat, group = group, fill = id), colour = 'black') +
coord_map() +
ggthemes::theme_map() +
scale_fill_distiller(palette = 'RdBu', limits = c(-1,1))
while limits = c(1, -1) produces a plot without a colorbar:
ggplot() +
geom_polygon(data = shape,
aes(x = long, y = lat, group = group, fill = id), colour = 'black') +
coord_map() +
ggthemes::theme_map() +
scale_fill_distiller(palette = 'RdBu', limits = c(1, -1))
If you would like to map the values in reverse order, you can use the direction argument:
ggplot() +
geom_polygon(data = shape,
aes(x = long, y = lat, group = group, fill = id), colour = 'black') +
coord_map() +
ggthemes::theme_map() +
scale_fill_distiller(palette = "RdBu",
limits = c(-1, 1),
breaks = c(-1, 0, 1),
direction = 1)

Producing a map with barplots using geom_subplot2d

I am trying to create a map with barplots for each state. I used geom_subplot2d. I am having a couple of issues and I was hoping somebody can help fix them
The border color I want it to be black but it is red no matter what value I use.
The color of the legend items (barplot) is also absorbed by each state. Is there a way that it can be separate?
Here is the code that produces the diagram:
p <- ggplot() +
geom_polygon(data = total,
aes(x = x, y = y, group = region, colour = "black",
fill = cut(eval(as.symbol(var1)), 10))) +
scale_fill_brewer(palette = "Blues")
testplot <- p +
geom_subplot2d(aes(long, lat, subplot = geom_bar(aes(yy, ..count.., fill = yy))),
bins = c(25,20), ref = NULL, width = rel(0.5), data = simdat) +
scale_fill_brewer(palette = "YlGn")
print(testplot)

Legend units for geom_point size

I have used geom_point in ggplot2 to display values as the area of each point:
geom_point(aes(size = sqrt(z/pi))
However, the legend units are the transformed values, is it possible to have the legend display the original values alongside their respective bubble size?
Edit: sorry I should have provided more information to begin with
library(ggplot2)
data <- data.frame(x = sample(1:10), y = sample(1:10), z = sample(1:10), colour = c("red", "yellow", "green","pink","black","brown","grey","white","purple","beige"))
ggplot(data, aes(x = x, y = y)) + geom_point(aes(size = sqrt(z/pi)), pch = 21) + aes(fill = colour) + scale_fill_brewer(palette = "set1")
Try adding:
+scale_colour_manual(guide = guide_legend(override.aes=aes(size=values)))

Resources