I have a data frame in R called df :
> df
# A tibble: 25 × 3
delta cat Year
<dbl> <chr> <dbl>
1 2.5 A 2019
2 2 A 2024
3 2.6 A 2020
4 4 A 2022
5 4.5 A 2023
6 3 B 2019
7 2.8 B 2024
8 2.95 B 2023
9 2.98 B 2022
10 3.07 B 2020
# ℹ 15 more rows
I have created a dot chart in R. using ggplot2 but in y axis that are the Years :
YEARS <- c(2019,2020, 2022, 2023, 2024)
x=df%>%
mutate_if(is.factor,as.character)%>%
group_by(cat) %>%
arrange(delta,.by_group = TRUE) %>%
mutate(labels = paste0(letters[1:n()], "/",Year))%>%
print(n=30)
x%>%
mutate(Year = factor(Year, levels = rev(YEARS)))%>%
group_by(Year)%>%
arrange(Year)%>%
ggplot(aes(x = delta, y = Year
)) +
geom_point(size = 3) +
geom_label_repel(aes(label = delta), # Add text labels showing `delta`
size = 3, # Adjust text size
box.padding = 0.3, # Padding around text box
point.padding = 0.2, # Padding around points
segment.color = "gray", # Line color
segment.size = 0.5) +
facet_grid(cat ~., scales="free_y") +
scale_y_discrete(labels = \(x) str_extract(x, "(?<=/).*")) +
labs(y =NULL) +
geom_vline(xintercept=0) +
theme_bw() +
theme(legend.position = "none", # Remove the legend
axis.text.x = element_text(angle = 0, hjust = 1), # Rotate x-axis labels
strip.text.y = element_text(size = 8, angle = 0, vjust = 0.5),
axis.text.y = element_text(size = 7),
strip.text = element_text(size = 14), # Increase facet label size
axis.title = element_text(size = 14), # Increase axis title size
axis.text = element_text(size = 10))+ # Increase axis text size
theme(strip.background = element_rect(color="black", fill="gray", size=1.5, linetype="solid"))+
labs(title = "",x = "")
how can I fix this ?
Data
df=structure(list(delta = c(2.5, 2, 2.6, 4, 4.5, 3,
2.8, 2.95, 2.98, 3.07, 2.2, 3.3, 3.4, 3.9,5, 3.7, 2.9, 2.9,
2.7, 2.9, 3.6, 2.3, 2.4, 2.3, 2.7), cat = c("A", "A",
"A", "A", "A", "B", "B", "B", "B", "B", "C", "C", "C", "C", "C",
"E","E","E","E","E", "D", "D", "D", "D", "D"), Year = c(2019,
2024, 2020, 2022, 2023, 2019, 2024, 2023, 2022, 2020, 2019, 2024,
2020, 2022, 2023, 2019, 2020, 2022, 2023, 2024, 2019, 2022, 2020,
2024, 2023)), row.names = c(NA, -25L), class = c("tbl_df", "tbl",
"data.frame"))
Maybe I did not understand correctly the question but I tried to order the years on the y-axis inside each category relative to delta.
I hypothesized that there is no duplicate couple cat-Year in df. Then I just ordered relative to delta (no need to do it inside groups as if it is ordered globally, inside the group it is also ordered) and then creating a factor like your variable label with levels ordered as in the sorted df. Then with your code for plotting, the years are ordered relative to delta in each category.
df %>%
arrange(delta) %>%
mutate(yaxis = paste0(cat, "/", Year),
yaxis = factor(yaxis, levels = yaxis)) %>%
ggplot(aes(x = delta, y = yaxis)) +
geom_point(size = 3) +
geom_label_repel(aes(label = delta), # Add text labels showing `delta`
size = 3, # Adjust text size
box.padding = 0.3, # Padding around text box
point.padding = 0.2, # Padding around points
segment.color = "gray", # Line color
segment.size = 0.5) +
facet_grid(cat ~., scales="free_y") +
scale_y_discrete(labels = \(x) str_extract(x, "(?<=/).*")) +
labs(y =NULL) +
geom_vline(xintercept=0) +
theme_bw() +
theme(legend.position = "none", # Remove the legend
axis.text.x = element_text(angle = 0, hjust = 1), # Rotate x-axis labels
strip.text.y = element_text(size = 8, angle = 0, vjust = 0.5),
axis.text.y = element_text(size = 7),
strip.text = element_text(size = 14), # Increase facet label size
axis.title = element_text(size = 14), # Increase axis title size
axis.text = element_text(size = 10))+ # Increase axis text size
theme(strip.background = element_rect(color="black", fill="gray", size=1.5, linetype="solid"))+
labs(title = "",x = "")
I hope I understood the problem!