Home > Net >  Trying to plot together discrete stat_bin_2d with continuous stat_density_2d
Trying to plot together discrete stat_bin_2d with continuous stat_density_2d

Time:03-27

I would like to use stat_bin_2d and stat_density_2d on data with wind direction and anomaly. I need to group the wind direction into 8 groups. As you can see below, A is the default output from using stat_bin_2d, while B is the addition of stat_density_2d. C is after wind direction has been grouped into 8 factors, and plotted wind direction as factor. D is when the wind direction as factor was turned back into numeric. As you can see, there are gaps between the tiles. F is an attempt to plot together C (wind direction as factor) with continuous stat_density_2d.

Is there a way to match the scale of both discrete (it's based on continuous) with continuous?

Is there a way to widen the tiles in D to match C?

Is there a way to manually enter the bin information into stat_bin_2d so that it would produce C automatically, but the output is still continuous?

Below is my attempt to do so.

# For reproducability
set.seed(190)

#Create a sample dataset and turn them into a data.table
Direction = runif(1000, min = 0, max = 360)
Anomaly = runif(1000, min = -10, max = 20)
Data = data.table(Direction, Anomaly)

#Plot a simple stat_bin_2d
ggplot(data = Data, aes(x = Anomaly, y = Direction))   stat_bin_2d()   labs(title =
                                                                              "(A) Default stat_bin_2d")
#Plot a simple stat_bin_2d with stat_density_2d
ggplot(data = Data, aes(x = Anomaly, y = Direction))   stat_bin_2d()  
  stat_density_2d(
    geom = "polygon",
    colour = "red",
    alpha = 0,
    bins = 6
  )  
  labs(title = "(B) Default stat_bin_2d and stat_density_2d")

#Bins the direction into 16 bins, which are the major direction of the wind. First as abbreviation, second as mean direction.
Data[, Direction_Factor_Name := cut(
  Direction,
  breaks = seq(-22.5, 382.5, 45),
  labels = c("N", "NE", "E", "SE", "S", "SW", "W", "NW", "N")
)]
Data[, Direction_Factor := cut(Direction,
                               breaks = seq(-22.5, 382.5, 45),
                               labels = c(seq(0, 350, 45), 0))]
Data[, Direction_New := as.numeric(levels(Direction_Factor))[Direction_Factor]]

ggplot()   stat_bin_2d(data = Data, aes(x = Anomaly, y = Direction_Factor))   labs(title =
                                                                                     "(C) stat_bin_2d with Factor")

ggplot(data = Data, aes(x = Anomaly, y = Direction_New))   stat_bin_2d()   labs(title =
                                                                                  "(D) stat_bin_2d with \nas.numeric(levels(Direction_Factor))[Direction_Factor]")

#Try to plot using Direction as factor with stat_density_2d
ggplot(data = Data, aes(x = Anomaly, y = Direction_Factor))   stat_bin_2d()  
  stat_density_2d(
    geom = "polygon",
    colour = "red",
    alpha = 0,
    bins = 6
  )  
  labs(title = "(E) Default stat_bin_2d and stat_density_2d")
#Error in seq_len(n) : argument must be coercible to non-negative integer

#Try something else, ,but the y-axis does not match
ggplot(data = Data, aes(x = Anomaly, y = Direction))  
  stat_bin_2d(aes(y = Direction_Factor))  
  stat_bin_2d()  
  stat_density_2d(
    geom = "polygon",
    colour = "red",
    alpha = 0,
    bins = 6
  )  
  labs(title = "(F) stat_bin_2d and stat_density_2d\nand stat_bin_2d with factor.") 

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

Update: I'm using binwidth argument now. Below is an example of the overall plot I'm making. While the polarplot by Allan Cameron is really good, it's not suitable foor the purpose of this figure. I will probably use it somewhere else instead.

enter image description here

CodePudding user response:

You can work with your continuous data by setting the binwidth of both axes:

ggplot(data = Data, aes(x = Anomaly, y = Direction))   
  stat_bin_2d(binwidth = c(4, 45))

enter image description here

If you want to use discrete values on the y axis, make the binwidth 1:

ggplot(data = Data, aes(x = Anomaly, y = Direction_Factor_Name))   
  stat_bin_2d(binwidth = c(4, 1))

enter image description here

Or you could go for a more intuitive result using polar coordinates:

library(geomtextpath)

ggplot(data = Data, aes(x = Anomaly, y = Direction_Factor_Name))   
  stat_bin_2d(binwidth = c(4, 1))  
  coord_curvedpolar(theta = "y", start = -pi/8)  
  geom_vline(xintercept = 0, color = "white")  
  theme_minimal()  
  scale_fill_viridis_c(option = "C")  
  theme(axis.text = element_text(size = 14, face = 2),
        axis.title.x = element_blank())

enter image description here

  • Related