Change size of the text and order the values using dot chart

Dear friends,
I am learning how to use dotchart as an alternative to bar chart. I have the following data set, please use the link;[Dropbox - File Deleted - Simplify your life]
The goal is to plot the crime percent by year, faceted by crime type (i.e. Primary.Type). There are two primary goals, sort the crime percent for each crime type, and reduce the size of the axis labels.
Here is my code to achieve this;

ggdotchart(street_crime_year_pct %>% arrange(Primary.Type,crime_percent),x="Year",y="crime_percent",
           color="tomato", facet.by = c("Primary.Type"),
           dot.size=2,rotate=TRUE,
           ggtheme=theme_pubr())+
          labs(x="Type of Criminal Activity",y="Crime Percent", title="Yearly Trend of Different types of Crimes on Streets of Chicago")

This code gives me the following output;


As seen , the values are not sorted despite of using arrange. Moreover we can't read the values for Year properly. Can I kindly get help to sort these two issues?thanks in advance.

I don't know how to do this with ggpubr but here is a solution with ggplot2

df <- read.csv("data.csv")
library(ggplot2)

reorder_within <- function(x, by, within, fun = mean, sep = "___", ...) {
  new_x <- paste(x, within, sep = sep)
  stats::reorder(new_x, by, FUN = fun)
}

scale_y_reordered <- function(..., sep = "___") {
  reg <- paste0(sep, ".+$")
  ggplot2::scale_y_discrete(labels = function(x) gsub(reg, "", x), ...)
}

ggplot(df, aes(x = crime_percent, y = reorder_within(Year, desc(crime_percent), Primary.Type))) +
  geom_point(color="orange", size=2) +
  geom_segment(aes(x=0,
                   xend=crime_percent,
                   yend=reorder_within(Year, desc(crime_percent), Primary.Type)),
               color="grey") +
  scale_y_reordered() +
  labs(x="Type of Criminal Activity",
       y="Crime Percent",
       title="Yearly Trend of Different types of Crimes on Streets of Chicago") +
  facet_wrap(vars(Primary.Type), scales = "free_y") +
  theme(strip.text.x = element_text(size = 5))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.