Home > Mobile >  R ggplot: facet_grid and dodge points
R ggplot: facet_grid and dodge points

Time:12-07

I would like to create a dodged geom_point plot after the facet_grid. But I would like my plot to get dodged only on the x-axis! It looks like the position_dodge() dodges the points along y-axis as well as the x-axis!

How do I control along which axis could I make the dodge:

My data looks similar to following:

  carat cut       color clarity depth table price     x     y     z grade       
  <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl> <chr>       
1  0.23 Ideal     E     SI2      61.5    55   326  3.95  3.98  2.43 low-quality 
2  0.21 Premium   E     SI1      59.8    61   326  3.89  3.84  2.31 High-quality
3  0.23 Good      E     VS1      56.9    65   327  4.05  4.07  2.31 low-quality 
4  0.29 Premium   I     VS2      62.4    58   334  4.2   4.23  2.63 High-quality
5  0.31 Good      J     SI2      63.3    58   335  4.34  4.35  2.75 low-quality 
6  0.24 Very Good J     VVS2     62.8    57   336  3.94  3.96  2.48 High-quality
...

My code as follows:

dmnd_data <- diamonds %>% head(100) %>% mutate(grade = ifelse(cut == "Premium", "High-quality", ifelse(cut == "Very Good", "High-quality", "low-quality"))

dmnd_data %>% ggplot(aes(color, cut, size = price))  
   geom_point(alpha = 0.7, position = position_dodge2(width = 0.5))   
   facet_grid(grade~., space = "free", scales = "free")

As you can see in the plot below that the points are not centered for each x-coordinate, they are dodged along both axis within each grid! Is there a way to control the points to dodge along only x-axis in each facet_grid??

facet_grid plot

CodePudding user response:

Does position_nudge() accomplish what you want here?

library(tidyverse)

dmnd_data <- diamonds %>% 
  head(100) %>% 
  mutate(
    grade = ifelse(
      cut == "Premium",
      "High-quality",
      ifelse(cut == "Very Good", "High-quality", "low-quality")
    ))
    
dmnd_data %>% ggplot(aes(color, cut, size = price))  
  geom_point(alpha = 0.7, position = position_nudge(x = 0.5))  
  facet_grid(grade ~ ., space = "free", scales = "free")

Created on 2021-12-06 by the reprex package (v0.3.0)

CodePudding user response:

In my opinion the dodging works fine as the points get dodged for each color. But if I understand you correctly you want the points to be dodged separately for each combo of color and cut. Not sure whether this could be achieved via position_dodge or ... but with the help of some data wrangling and a switch to a continuous scale you could do:

library(ggplot2)
library(dplyr)

dmnd_data <- diamonds %>%
  mutate(grade = ifelse(cut == "Premium", "High-quality", ifelse(cut == "Very Good", "High-quality", "low-quality"))) %>% 
  head(100)

width <- .5 

dmnd_data <- dmnd_data %>% 
  select(cut, color, grade, price) %>% 
  group_by(grade, cut, color) %>%
  mutate(nudge = if (n() > 1) seq(-width/2, width/2, length.out = n()) else 0) %>% 
  ungroup()

color_lvls <- levels(dmnd_data$color)

ggplot(dmnd_data, aes(color, cut, size = price))  
  geom_point(aes(as.numeric(color)   nudge), alpha = 0.7)  
  scale_x_continuous(breaks = seq_along(color_lvls), labels = color_lvls)  
  facet_grid(grade ~ ., space = "free", scales = "free")

  • Related