-1

I want to use ggplot for generating a plot with 4 different values. In the sample data-frame below I have 4 coloums and I want to plot them all together in one plot. I want "Code" to be the x-axis, "Cor" to be the y-axis, "Vari" to be displayed by different colors and "Con" to be displayed by different symbols.

I know how to plot it with 3 of the 4 colums and so far I just made 2 different plots, one with only "Vari" and one with only "Con". But I want to combine them. I tried:

library(ggplot2)
library(scales)
library(dplyr)

data %>% 
  mutate(bin = Cor < 0) %>%
  
  # plot the data using a facet_grid with free y scales
  ggplot(aes(x = Code, y = Cor, fill = Con, shape = Vari) +
  facet_grid(bin ~ ., scale='free_y') +
  theme(legend.position="bottom") +
  theme(axis.text.x = element_text(angle=90, vjust=0.6)) + 
  theme(strip.text.y = element_blank())

But I think I have to use the shapes in a different way. I also would like to pick the colors and shapes manually, I know that I have to add

scale_fill_manual(values=c())

for the colors, but I stuggle with the shapes.

structure(list(Vari = c("PM", "TMK", "VPM", 
                             "TMK", "TXK", "TNK", "TGK", "VPM", "TMK", 
                             "TXK", "TNK", "TGK", "VPM", "TMK", "VPM", 
                             "TMK", "TNK", "TGK", "VPM", "TMK", "TXK", 
                             "TNK", "TGK", "VPM", "TNK", "TGK", "VPM", 
                             "TMK", "TXK", "TNK", "TGK", "TNK", "TGK", 
                             "TMK", "TXK", "TNK", "TGK"), Code = c("R10", 
                                                                                         "J06", "J20-J22", "J20-J22", "J20-J22", "J20-J22", "J20-J22", 
                                                                                         "J20-J22", "J20-J22", "J20-J22", "J20-J22", "J20-J22", "J06", 
                                                                                         "J06", "J20", "J20", "J20", "J20", "J20-J22", "J20-J22", "J20-J22", 
                                                                                         "J20-J22", "J20-J22", "J20", "J20", "J20", "J20-J22", "J20-J22", 
                                                                                         "J20-J22", "J20-J22", "J20-J22", "H00-H06", "H00-H06", "B85-B89", 
                                                                                         "B85-B89", "I20-I25", "I20-I25"), Corr = c(-0.569, -0.5125, 
                                                                                                                                             -0.5739, -0.5843, -0.5603, -0.5744, -0.5547, -0.6168, -0.5897, 
                                                                                                                                             -0.5458, -0.5867, -0.5628, -0.5047, -0.5086, -0.5172, -0.512, 
                                                                                                                                             -0.5229, -0.5257, -0.6172, -0.6003, -0.5599, -0.602, -0.5912, 
                                                                                                                                             -0.5032, -0.5121, -0.5187, -0.5966, -0.59, -0.5661, -0.5879, 
                                                                                                                                             -0.5589, 0.5104, 0.5758, 0.5491, 0.528, -0.5153, -0.5516), Con = c("Hi", 
                                                                                                                                                                                                                      "Eg", "Eg", "Eg", "Eg", "Eg", "Eg", "WFX", 
                                                                                                                                                                                                                      "WFX", "WFX", "WFX", "WFX", "WFM", 
                                                                                                                                                                                                                      "WFM", "WFM", "WFM", "WFM", "WFM", 
                                                                                                                                                                                                                      "WFM", "WFM", "WFM", "WFM", "WFM", 
                                                                                                                                                                                                                      "No2", "No2", "No2", "No2", "No2", "No2", 
                                                                                                                                                                                                                      "No2", "No2", "PM25", "PM25", "MAM", "MAM", 
                                                                                                                                                                                                                      "Hi", "Hi")), row.names = c(23L, 35L, 36L, 37L, 38L, 
                                                                                                                                                                                                                                                              39L, 40L, 43L, 44L, 45L, 46L, 47L, 50L, 51L, 52L, 53L, 54L, 55L, 
                                                                                                                                                                                                                                                              58L, 59L, 60L, 61L, 62L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 
                                                                                                                                                                                                                                                              74L, 75L, 86L, 87L, 100L, 101L), class = "data.frame")
Mr.Spock
  • 511
  • 2
  • 13
  • 4
    Your code has multiple errors. There is a missing `)` for `ggplot()` and no plotting geometry. For example `geom_point()`. You have also misspelled `Corr` in 3 places. On the bright side, you have provided an excellent sample of data. It will be much easier to help if you post working code and a clear explanation of what you are hoping to achieve. – Ian Campbell Apr 19 '21 at 20:19
  • 1
    We have some indentation and whitespace interfering with readability here. – Wyck Apr 20 '21 at 19:20

1 Answers1

2

Not so sure what the problem is - I feel that from your question you should know the answer...

I'd use color aesthetic instead, because there are not many shapes that have a fill.

And there is a scale_(*aes*)_manual for each aesthetic. I have added this, although the values 1:6 are pretty random.

df <- structure(list(Vari = c(
  "PM", "TMK", "VPM",
  "TMK", "TXK", "TNK", "TGK", "VPM", "TMK",
  "TXK", "TNK", "TGK", "VPM", "TMK", "VPM",
  "TMK", "TNK", "TGK", "VPM", "TMK", "TXK",
  "TNK", "TGK", "VPM", "TNK", "TGK", "VPM",
  "TMK", "TXK", "TNK", "TGK", "TNK", "TGK",
  "TMK", "TXK", "TNK", "TGK"
), Code = c(
  "R10",
  "J06", "J20-J22", "J20-J22", "J20-J22", "J20-J22", "J20-J22",
  "J20-J22", "J20-J22", "J20-J22", "J20-J22", "J20-J22", "J06",
  "J06", "J20", "J20", "J20", "J20", "J20-J22", "J20-J22", "J20-J22",
  "J20-J22", "J20-J22", "J20", "J20", "J20", "J20-J22", "J20-J22",
  "J20-J22", "J20-J22", "J20-J22", "H00-H06", "H00-H06", "B85-B89",
  "B85-B89", "I20-I25", "I20-I25"
), Corr = c(
  -0.569, -0.5125,
  -0.5739, -0.5843, -0.5603, -0.5744, -0.5547, -0.6168, -0.5897,
  -0.5458, -0.5867, -0.5628, -0.5047, -0.5086, -0.5172, -0.512,
  -0.5229, -0.5257, -0.6172, -0.6003, -0.5599, -0.602, -0.5912,
  -0.5032, -0.5121, -0.5187, -0.5966, -0.59, -0.5661, -0.5879,
  -0.5589, 0.5104, 0.5758, 0.5491, 0.528, -0.5153, -0.5516
), Con = c(
  "Hi",
  "Eg", "Eg", "Eg", "Eg", "Eg", "Eg", "WFX",
  "WFX", "WFX", "WFX", "WFX", "WFM",
  "WFM", "WFM", "WFM", "WFM", "WFM",
  "WFM", "WFM", "WFM", "WFM", "WFM",
  "No2", "No2", "No2", "No2", "No2", "No2",
  "No2", "No2", "PM25", "PM25", "MAM", "MAM",
  "Hi", "Hi"
)), row.names = c(
  23L, 35L, 36L, 37L, 38L,
  39L, 40L, 43L, 44L, 45L, 46L, 47L, 50L, 51L, 52L, 53L, 54L, 55L,
  58L, 59L, 60L, 61L, 62L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L,
  74L, 75L, 86L, 87L, 100L, 101L
), class = "data.frame")

library(tidyverse)

df %>%
  mutate(bin = Corr < 0) %>%
  ggplot(aes(x = Code, y = Corr, color = Con, shape = Vari)) +
  geom_point() +
  colorblindr::scale_color_OkabeIto() +
  scale_shape_manual(values = 1:6) +
  facet_grid(bin ~ ., scale = "free_y") +
  theme(
    legend.position = "bottom",
    axis.text.x = element_text(angle = 90, vjust = 0.6),
    strip.text.y = element_blank()
  )

Created on 2021-04-19 by the reprex package (v2.0.0)

tjebo
  • 21,977
  • 7
  • 58
  • 94
  • Thank you so much, that works! Could you tell me how to make the symbols bigger please? – Mr.Spock Apr 19 '21 at 21:02
  • 1
    change the value for the size argument in geom_point. e.g. `geom_point( size = 3)` – tjebo Apr 19 '21 at 21:03
  • Thanks, and how could I fill these symbols? I tried using the shape-numbers from 15 and above, but that doesnt seem to work. – Mr.Spock Apr 19 '21 at 21:19
  • @Mr.Spock only shape 21 to 25 are fillable. I have to constantly look that up. See here http://www.sthda.com/english/wiki/ggplot2-point-shapes or `ggpubr::show_point_shapes()` – tjebo Apr 21 '21 at 07:05
  • @Mr.Spock but check also this thread on SO: https://stackoverflow.com/questions/66642492/are-there-any-other-additional-fillable-shapes-in-ggplot/66646626#66646626 – tjebo Apr 21 '21 at 07:07