trevorld / ggpattern

ggplot geoms with pattern fills
https://trevorldavis.com/R/ggpattern/dev/
Other
361 stars 18 forks source link

How to run it with variables more than 6? #39

Closed tacitli closed 3 years ago

tacitli commented 3 years ago

Hi! I came across an error when changing the dataframe to 7 variables:

>      df <- data.frame(
>        trt     = c("a", "b", "c","d", "e", "f","g"),
>        outcome = c(2.3, 1.9, 3.2,2.1,1.3,1.5,1.7)
>      )
> 

we will see a warning as:

### Warning message:
### The shape palette can deal with a maximum of 6 discrete values because more than 6 becomes difficult to discriminate; you have 7. Consider specifying shapes manually if you must have them. 

And in the plot, the 7th variable will not show up. image

Adding scale_shape_manual(values=1:7) is not helpful.

How can I fix that?

trevorld commented 3 years ago

Have you tried manually specifying your seven desired "pch" shape in geom_bar_pattern() i.e. geom_bar_pattern(shape = c(14, 5, 3, 6, 4, 8, 20))?

tacitli commented 3 years ago

Have you tried manually specifying your seven desired "pch" shape in geom_bar_pattern() i.e. geom_bar_pattern(shape = c(14, 5, 3, 6, 4, 8, 20))?

Thank you Trevorld! However, I can't locate where to adjust shape manually in my script?

Sorry for forgeting to link my entire code

p <- ggplot(df, aes(trt, outcome)) +
  geom_col_pattern(
    aes(
      pattern_shape  = trt,
      pattern_angle  = trt,
      pattern_colour = trt
    ),
    pattern          = 'points',
    pattern_spacing  = 0.02,
    colour           = 'black',
    pattern_density  = 0.15,
    fill             = 'white',
    pattern_option_1 = 0.1
  ) +
  theme_bw() +
  labs(
    title    = "ggpattern::geom_col_pattern()",
    subtitle = "pattern = 'points'"
  ) +
  theme(legend.position = 'none') +
  scale_pattern_angle_discrete(range = c(0, 30)) +
  coord_fixed(ratio = 1/2)

p
trevorld commented 3 years ago

You should move pattern_shape = trt out of the aes() and convert it to pattern_shape = c(14, 5, 3, 6, 4, 8, 20) (this should have as many elements as rows in your data frame, you can choose other points() shape values as desired).

tacitli commented 3 years ago

You should move pattern_shape = trt out of the aes() and convert it to pattern_shape = c(14, 5, 3, 6, 4, 8, 20) (this should have as many elements as rows in your data frame, you can choose other points() shape values as desired).

Wow😯, I just understand how it works and successfully plot the right figure. Much thanks!