I have a table with this kind of data for vehicles (with more rows).
**
So far I got a boxplot using car segments as categories and pricing for the Y-axis, however I added the points with geom_jitter but like to colour them according to the region, so getting two colours for either Europe or China. As of now, the points have the same colour as the boxes and I am not sure about how to input the "region" category colour.
Here's the code I used so far
Boxplot_data%>%
ggplot(aes(Segment, Price, colour=Segment))
geom_boxplot()
geom_jitter( alpha=0.5 )
your text
I tried using a few different ways to obtain the coloured points according to the Region category, but none worked.
CodePudding user response:
Here is one way how we could do it:
library(tidyverse)
df %>%
mutate(Price = parse_number(Price)) %>%
ggplot(aes(Segment, Price, fill = Segment))
geom_boxplot()
geom_jitter(aes(color = Continent), size = 4)
scale_fill_manual(values = c("red", "blue"))
scale_color_manual(values = c("green", "gold"))
theme_bw()
data:
df <- structure(list(Segment = c("C", "C", "SUV", "C", "SUV", "SUV",
"C", "C", "SUV", "SUV", "SUV"), Price = c("\200 31,545.00", "\200 38,970.00",
"\200 68,895.00", "\200 36,250.00", "\200 24,345.00", "\200 32,975.00",
"\200 36,000.00", "\200 39,900.00", "\200 25,959.00", "\200 42,800.00",
"\200 44,500.00"), Continent = c("Europe", "Europe", "Europe",
"Europe", "Europe", "Europe", "China", "China", "China", "China",
"China")), class = "data.frame", row.names = c("Tiguan", "Tiguan All",
"Touareg", "Touran", "T-Roc", "T-Roc Cab", "Ora Funky1", "Ora Funky2",
"Seres 3", "Seres 5", "Lynk Co 01"))
CodePudding user response:
Try this:
df %>%
mutate(Price1 = parse_number(Price)) %>%
ggplot(aes(Segment, Price1, colour=interaction(Segment, Continent)))
geom_boxplot()
geom_jitter( alpha=0.5 )
theme_bw()
labs(y = "Price (Euros)", color = "legend title")