1

I want to visualize the relationship of y with A, B, and C separately across 10 levels of a group variable using geom_point().

My current plot can only plot y against A but I want B and C to be shown in each plot in different colors.

I was wondering how this might be achievable in ggplot2?

library(ggplot2)

dat <- read.csv('https://raw.githubusercontent.com/rnorouzian/e/master/sng.csv')

ggplot(dat)+aes(x=A, y = y, fill = group)+geom_point()+ # How can I have `B` and `C` next to `A` with other colors
  facet_wrap(~group)

2 Answers 2

2

Try the standard way : getting the data in long format.

library(ggplot2)

dat %>%
  tidyr::pivot_longer(cols = A:C) %>%
  ggplot()+aes(x=value, y = y, color = name) + 
  geom_point() + facet_wrap(~group)

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

For distinction of points, I can think of color, size, and shape, is there anything else for distinguishing points from one another?
Yes, those are enough to separate the points, I think.
1

We can use ggscatter from ggpubr

library(ggpubr)
library(purrr)
ggscatter(dat, x = c("A", "B", "C"), y = "y", color = "group", palette = "jco")  %>% 
      invoke(ggarrange, ., ncol = 2, nrow = 2)

-output

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.