i have a data frame in R called data that looks like this :
Year Color var value
1 2023 blue A -0.06
2 2023 blue A 0.04
3 2023 blue G -0.27
4 2023 blue D 0.05
5 2023 blue L 0.18
6 2023 blue B 0.28
7 2023 blue B 0.15
8 2023 blue E 0.10
i want to plot this data using dotchart in R using ggplot2 package.
doing so i did:
ggplot(data, aes(x = value, y = Year, color = Color)) +
geom_point(size = 3) +
facet_wrap(~ var) +
theme_minimal() +
theme(legend.position = "none")
getting :
but i want to have each level of var column to be horizontally separated like this one :
How can i do it using ggplot2 and also and the vertical line to 0 across all categories ? Also why ggplot2 reports 2021 while there are no 2021 data ? I want to drop it as well.
data
structure(list(Year = c(2023, 2023, 2023, 2023, 2023, 2023, 2023,
2023, 2023, 2023, 2022, 2022, 2022, 2022, 2022, 2022, 2022, 2022,
2022, 2022, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020,
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019), Color = c("blue",
"blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue",
"blue", "red", "red", "red", "red", "red", "red", "red", "red",
"red", "red", "green", "green", "green", "green", "green", "green",
"green", "green", "green", "purple", "purple", "purple", "purple",
"purple", "purple", "purple", "purple", "purple"), var = c("A",
"A", "G", "D", "L", "B", "B", "E", "E", "C", "A", "A", "G", "D",
"L", "B", "B", "E", "E", "C", "A", "A", "G", "D", "B", "B", "E",
"E", "C", "A", "A", "G", "D", "B", "B", "E", "E", "C"), value = c(-0.06,
0.04, -0.27, 0.05, 0.18, 0.28, 0.15, 0.1, 0.02, -0.19, -0.04,
0.22, -0.13, 0.05, 0.12, 0.19, 0.07, 0, -0.42, -0.05, -0.07,
0.13, -0.23, 0.23, -0.09, -0.01, -0.11, 0.07, -0.23, -0.3, 0.15,
-0.19, 0.09, -0.03, 0.03, 0, 0.11, -0.09)), class = "data.frame", row.names = c(NA,
-38L))
To tell ggplot not to plot 2021, the Year
variable needs to be "discrete" (aka a factor). You can then use facet_grid
and geom_vline
to get something close to your desired plot.
df |>
mutate(Year=factor(Year)) |>
ggplot(aes(x = value, y = Year, color = Color)) +
geom_point(size = 3) +
facet_grid(var~., scales="free") +
labs(y="Year") +
geom_vline(xintercept=0) +
theme_bw() +
theme(legend.position = "none")