0

I have attached a sample plot code. The x-axis is discrete (chr type), y axis is numeric.

  1. How do I join points to see the change in count with days i.e. connect points of same color
  2. I want x-axis to be in the pre-defined sequence (not alphbetical) i.e. - "Sun", "Mon", "Tue"
days <- c("Sun", "Mon", "Tue", "Sun", "Mon", "Tue")
count <- c(1, 2, 3, 2.5, 3, 4.5)
variant_type <- c("A", "A", "A", "B", "B", "B")

tbl <- tibble(days, count, variant_type)

ggplot(data = tbl,
       aes(x = days,
           y = count,
           color = variant_type)) + 
  geom_line() + 
  geom_point() + 
  theme_bw() + 
  theme(legend.position = "right",
        aspect.ratio = 1)

enter image description here

1 Answer 1

2

In order to accomplish this, you will need to explicitly define the grouping structure by mapping group to the variant_type variable from within the ggplot() aesthetic, where it will be applied to all layers. Then you simply have to map the colour aesthetic to the variant_type variable within geom_point and geom_line.

library(ggplot2)
library(tibble)

days <- c("Sun", "Mon", "Tue", "Sun", "Mon", "Tue")
count <- c(1, 2, 3, 2.5, 3, 4.5)
variant_type <- c("A", "A", "A", "B", "B", "B")

tbl <- tibble(days, count, variant_type)

ggplot(data = tbl,
       aes(x = days,
           y = count,
           group = variant_type)) + 
  geom_point(aes(colour = variant_type)) + 
  geom_line(aes(colour = variant_type)) + 
  theme_bw() + 
  theme(legend.position = "right",
        aspect.ratio = 1)

Created on 2021-03-08 by the reprex package (v0.3.0)

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

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.