mef022
mef022

Reputation: 55

ggplot: color points by density as they approach a specific value?

I have a dataset containing 1,000 values for a model, these values are all within the same range (y=40-70), so the points overlap a ton. I'm interested in using color to show the density of the points converging on a single value (y=56.72) which I have indicated with a horizontal dashed line on the plot below. How can I color these points to show this?

ggplot(data, aes(x=model, y=value))+ 
geom_point(size=1) + 
geom_hline(yintercept=56.72, 
           linetype="dashed", 
            color = "black")

enter image description here

Upvotes: 1

Views: 759

Answers (2)

denis
denis

Reputation: 5673

I think that you should opt for an histogram or density plot:

n <- 500
data <- data.frame(model= rep("model",n),value =  rnorm(n,56.72,10))

ggplot(data, aes(x = value, y = after_stat(count))) +
  geom_histogram(binwidth = 1)+
  geom_density(size = 1)+
  geom_vline(xintercept = 56.72, linetype = "dashed", color = "black")+
  theme_bw()

enter image description here

Here is your plot with the same data:

ggplot(data, aes(x = model, y = value))+ 
  geom_point(size = 1) + 
  geom_hline(yintercept = 56.72, linetype = "dashed", color = "black")

enter image description here

If your model is iterative and do converge to the value, I suggest you plot as a function of the iteration to show the convergence. An other option, keeping a similar plot to your, is dodging the position of the points :

ggplot(data, aes(x = model, y = value))+ 
  geom_point(position = position_dodge2(width = 0.2),
             shape = 1,
             size = 2,
             stroke = 1,
             alpha = 0.5) + 
  geom_hline(yintercept = 56.72, linetype = "dashed", color = "black")

enter image description here

Here is a color density plot as you asked:

library(dplyr)
library(ggplot2)
data %>%
  mutate(bin = cut(value, breaks = 10:120)) %>%
  dplyr::group_by(bin) %>%
  mutate(density = dplyr::n()) %>%
  ggplot(aes(x = model, y = value, color = density))+ 
  geom_point(size = 1) + 
  geom_hline(yintercept = 56.72, linetype = "dashed", color = "black")+
  scale_colour_viridis_c(option = "A")

enter image description here

Upvotes: 2

eastclintw00d
eastclintw00d

Reputation: 2364

I would suggest to use the alpha parameter within the geom_point. You should use a value close to 0.

ggplot(data, aes(x=model, y=value)) + 
  geom_point(size=1, alpha = .1) + 
  geom_hline(yintercept=56.72, linetype="dashed", color = "black")

Upvotes: 0

Related Questions