I have a data frame that has the following columns:
library(dplyr)
mig_tend <- rep(c("Resident","Migrant", "Unknown"), 100)
pred_observed <- runif(length(mig_tend), min = -0.04270965, max = 0.01783518)
weighted_ <- runif(length(mig_tend), min = 3.648399e-07, max = 0.002123505)
scaled_PDSI <- runif(length(mig_tend), min = -0.842694, max = 1.957527)
pdsi <- runif(length(4), min = -2, max = 2)
y <- runif(length(4), min = -0.00613618, max = -0.002790441)
df1 <- data.frame(mig_tend = as.factor(mig_tend),
pred_observed = pred_observed,
weighted = weighted_,
scaled = scaled_PDSI
)
I am trying to create a plot where the size of the points vary based on the weighted
values for that specific point. I would like the point to be smaller when the value of weighted
is larger and for the point to be larger when the value of weighted
is smaller.
reprex <- df1 %>%
# Plot against PDSI
ggplot(aes(x = scaled, col = as.factor(mig_tend)))
# geom_hline(yintercept = 0, linetype = 2, col = "grey")
# add in predictions
geom_point(aes(y = pred_observed, size = weighted))
scale_size_continuous(range = c(0.01, 0.2))
labs(col = "", fill = "", size = "Weights",
y = "Predicted_Observation",
x = "PDSI")
theme_bw()
theme(plot.title = element_text(hjust = 0.5),
legend.position = "right")
CodePudding user response:
Simplest approach might be to use the negative value, and then tweak the name and labels to undo that:
geom_point(aes(y = pred_observed, size = -weighted))
scale_size_continuous(range = c(0.01, 1.2),
labels = ~scales::comma(x = -.x),
name = "weighted")
Or alternatively you could look at is the reciprocal, though this might be too extreme for your data in highlighting smaller values. In the example below, this is emphasized even more by using scale_size_area
, but the upside of that is that the size values map more directly to our perception -- ie in this case it's showing how much you'd have to "dilute" one unit with to get to the underlying number.
geom_point(aes(y = pred_observed, size = 1/weighted))
scale_size_area(max_size = 10,
breaks = c(10000, 100000, 500000),
labels = ~paste0("1/", scales::comma(x = .x)),
name = "weighted")