Reputation: 1546
I have attached a sample plot code. The x-axis is discrete (chr type), y axis is numeric.
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)
Upvotes: 0
Views: 627
Reputation: 2849
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)
Upvotes: 2