Changing the labels on ggplot legend - r

I'm using ggplot so I can get a gradient onto a map to show data over a large scale. There are points between 0 and 35,000 to be visualised. I have got this to work, but the legend is automatically showing labels for every 10,000.
Ideally I want the legend to show the maximum amount, so probably it would just show 0 at the bottom and 35,000 at the top. Is this doable?
My ggplot code is below if this helps.
ggplot() +
geom_map(data = datafile, aes(map_id = Health_Board, fill = datafile$"2007"), map = Scot) +
geom_polygon(data = Scot, aes(x = long, y = lat, group = group), colour = "gray", fill = NA) +
expand_limits(x = Scot$long, y = Scot$lat) +
scale_fill_gradient(low = ("lightyellow"), high = ("red"), limits = c(0,35000)) +
ggtitle("2007") +
coord_fixed(1.2) +
theme(axis.text.x = element_blank(), axis.text.y = element_blank(),
axis.ticks = element_blank(), axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.border = element_blank(), panel.background = element_blank(),
legend.title = element_text(face = "bold"),
plot.title = element_text(face = "bold", hjust = 0.5))

You can include the "breaks" argument. Like this:
scale_fill_gradient(low = ("lightyellow"), high = ("red"),
breaks=c(min(lat),max(lat)),
limits = c(0,35000)) +
If you want more, its possible to include the "labels" argument.
scale_fill_gradient(low = ("lightyellow"), high = ("red"),
breaks=c(min(lat),max(lat)),
labels=c("Minimum","Maximum"),
limits = c(0,35000)) +

Related

Combine 2 plots (ggplot) but use one legend and colour scheme

I have created 2 plots and combined them as seen below but i now wish to plot this with one legend and the same colour scheme for both plots, does anyone know what changes i need to make this?
code used to create plots
#plotting actual from 1998-2020
mapplot_temp = ggplot(map_preds20, aes(x = long, y = lat, group = group)) +
geom_polygon(aes(fill = GDP_pdiff), colour = "black")
mapplot1 = mapplot_temp + scale_fill_gradient(name = 'GDP per capita change %', low =
'red', high = 'yellow', na.value = 'grey') +
theme_bw() +
theme(panel.border = element_blank(),
panel.grid = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.ticks = element_blank(),
plot.title = element_text(hjust = 0.5, size = 20, face = "bold"))
#plotting predicted from 2021-2040
mapplot_temp2 = ggplot(map_preds40, aes(x = long, y = lat, group = group)) +
geom_polygon(aes(fill = GDP_pdiff), colour = "black")
mapplot2 = mapplot_temp2 + scale_fill_gradient(name = 'GDP per capita change % ($)', low = 'red', high = 'yellow', na.value = 'grey') +
ggtitle('Predicted GDP per capita change') +
theme_bw() +
theme(panel.border = element_blank(),
panel.grid = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.ticks = element_blank(),
plot.title = element_text(hjust = 0.5, size = 20, face = "bold"))
#combine plots with one legend and same colour scheme
mapplot1 + mapplot2
plot

GGplot Mapping Question - Change legend Scale

I have successfully made some maps in R using ggplot but need help changing the scale of the legend so that all the four maps i made have the same scale.
Here is my code for one of my maps:
invmap0206 <- ggplot(invLAmap0206, aes(long, lat, group = group)) +
geom_polygon(aes(fill = wnvrate0206), colour = "grey50") +
ggtitle("WNV NID Rates 2002-2006 by Louisiana Parish") +
coord_quickmap()
invmap0206_2 <- invmap0206 + scale_fill_gradient(name = "Rate per 100,000", low = "lightblue", high = "darkblue", na.value = "grey50") +
theme(axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
rect = element_blank())
invmap0206_2
Here is the result:
I would like the scale to be 0-8. Any help would be appreciated.

how can I draw a connecting line between the label and the plot in R

This is my script for the plot,
data = data.frame(Kingdom = c("Bacteria", "Archaea"),
Total = c(273523, 2616))
sizeRange <- c(0,30)
library(ggplot2)
ggplot(data, aes(x=0,y=Kingdom,color=Kingdom)) +
geom_point(aes(size = Total,alpha=10),colour="blue",stroke=2) +
scale_size(range = sizeRange)+
theme_bw() +
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "white"))
somebody, please tell me how can I get a connecting line between my y-axis label and the plot
My plot looks like this
I want something like this
A clean alternative would be to label the points directly, and remove the y-axis if wanted. e.g.:
ggplot(data, aes(x=0,y=Kingdom,color=Kingdom)) +
ggrepel::geom_text_repel(aes(label = Kingdom), vjust = -1,colour="black") +
geom_point(aes(size = Total),colour="blue",stroke=2) +
scale_size(range = sizeRange)+
theme_bw() +
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "white"),
axis.text.y=element_blank(),
axis.title.y = element_blank(),
axis.ticks.y=element_blank())
you can manually add segments, but then the alpha of your points will kind of show them.
Here is a try, altought it's not perfect if the x axis expend.
ggplot(data, aes(x=0,y=Kingdom,color=Kingdom)) +
# Added the segments here before the points.
# I tried to alpha it but I can't figure out how to limit the
# segment to the point border.
geom_segment(x = rep(-100,2), xend = rep(0,2),
y = c(1, 2), yend = c(1,2),colour="blue", alpha = 10) +
geom_point(aes(size = Total,alpha=10),colour="blue",stroke=2) +
scale_size(range = sizeRange)+
theme_bw() + guides(alpha = "none") + # remove alpha from legend.
theme(panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "white"))

How to assign geom_point colors based on string values

I'm trying to map some points across a map of the ocean... this is my dataframe (rik_data)
location mean_longitude mean_latitude
NSTR002 -63.53341 44.47846
NSTR002 -63.53341 44.47846
NSTR001 -63.52704 44.46643
NSTR001 -63.52704 44.46643
NSTR003 -63.50115 44.41449
HFX014 -63.24095 44.21091
HFX014 -63.24095 44.21091
HFX023 -63.22477 44.19080
HFX0165 -63.21937 44.16828
HFX0165 -63.21937 44.16828
HFX020 -63.20010 44.12228
HFX020 -63.20010 44.12228
I want to plot these points so that every location starting with "HFX" is one color versus every location starting with "NSTR" is another colour. I'm using this code for the graph.
canada = map_data("worldHires", "Canada")
p = ggplot(data = canada) +
geom_polygon(data = canada, aes(x=long, y = lat, group = group), fill = "lightgrey") +
coord_sf(xlim=c(-64.5,-62.8), ylim=c(42.7,45), expand = FALSE) +
#HOW TO ASSIGN COLOR BY STRINGS?
geom_point(data = rik_data,
mapping = aes(x = mean_longitude,
y = mean_latitude), color = "black",
size = 4, alpha = 0.5) +
labs(colour = "Location") +
theme(panel.background = element_rect(fill = "#add8e6"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.ticks.y = element_blank(),
axis.ticks.x = element_blank(),
axis.title.y =element_blank(),
axis.title.x = element_blank(),
axis.text.x=element_blank(),
axis.text.y = element_blank(),
legend.position = c(0.2, 0.2),
legend.background = element_blank(),
text = element_text(size = 25,
family = "sans"))
Doesn anyone know how to assign red to locations starting with "NSTR" and black to locations starting with "HFX"?
You can create the classification within the ggplot call using a grepl (this assumes all cases are either HFX or NSTR). Replace your geom_point statement with something like this:
geom_point(data = dd,
mapping = aes(x = mean_longitude,
y = mean_latitude,
color = grepl("^HFX",location)),
size = 4, alpha = 0.5) +
scale_color_discrete(breaks=c(0,1),labels=c("NSTR","HFX"))

Increase the radius of the Circle in ggplot2

I've had trouble increasing the radius of the circle on a map. I've tried the scale_size_continuous with maximum size up to 1000, but the circle size remained the same after all.
My code is in the following:
states <- map_data("state")
gg1 <- ggplot(data=states) +
geom_polygon(data=states, aes(x=long,y=lat,group=group),
fill = "orange", color = "white") +
coord_fixed(ratio = 1.3)+
geom_circle(data = origCity, aes(x0 = Long, y0 = Lat, r = Percent),
fill = "black", color = "black", alpha = 0.5, show.legend = FALSE) +
scale_size_continuous(range = c(20, 5))+
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank())+
theme(axis.ticks.x = element_blank(),
axis.title.x = element_blank(),
axis.text.x = element_blank())+
theme(axis.ticks.y = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank())
print(gg1)
Any suggestions are greatly appreciated!

Resources