I wanna make a spatial map of trends, for that I am using my varaible called sen. I want to represent those values with points in a scale determined by the size of the points and if the value is negative they are represented with blue and positive with red. This my code:
library(ggplot2)
#Creating a funtion for the point size
point_size <- function(x) {
ifelse(x >= 0, 0.05 + x * 0.01, 0.05 - x * 0.01)
}
#Creating a funtion for color
color <- function(x) {
ifelse(x >= 0, "red", "blue")
}
#Data
data <- data.frame(Z = c(2.7, 1.8, 2.1, 1.4, -1.9, -2.3),
Sen = c(0.03, -0.03, 0.01, -0.015, 0.025, -0.05),
lon = c(-70.123, -71.456, -69.789, -72.012, -73.345, -68.901),
lat = c(-33.456, -32.012, -34.345, -31.678, -33.901, -35.234))
#Adjusting the size of the points
data$size <- point_size(data$Sen)
#Adjusting the color of the points
data$color <- color(data$Sen)
#Creating the dotmap with filled circles and black border
map_point <- ggplot(data, aes(x = lon, y = lat, size = size, fill = color)) +
geom_point(shape = 21, color = "black") +
scale_size_continuous(range = c(2, 8)) +
labs(title = "Map of points", x = "Longitude", y = "Latitude", size = "Size", fill = "Value") +
theme_minimal() +
scale_fill_manual(values = c("blue" = "blue", "red" = "red"),
labels = c("Negatives", "Positives"))
print(mapa_puntos)`
But my goal is to have a scale that goes from -0.05 to 0.05 with a step of 0.01, so it will have 10 points in the legend: 5 blue and 5 red: is this possible to do in R? With the previous code, the only thing I get is to represent the information separately.
You can get the desired output by setting the breaks
, labels
and limits
arguments inside the scale_size_continuous
function. Using your data and setting the limits to -0.5 and 0.5, results in a blank graph, since all the values are higher than 0.5; thus, I changed the limits to -0.6 and 0.6. Consider adjusting these values for your real data.
scale_size_continuous(breaks = seq(-0.06, 0.06, 0.01),
labels = seq(-0.06, 0.06, 0.01),
limits = c(-0.06, 0.06))