How to create a range plot with groups?

Hi.
I want to create a range plot. The X-axis should show the value of "days" (the min is replicate "one" values, and max replicate "two" values) and the Y-axis the "isolates." However, I also, for each isolate, want to show the values for each of the "treatment" (e.g., in different colors). I don't really know where to begin and how to solve this issue, and would very much appreciate any help I could get.
Thank you.

x <- wrapr::build_frame(
"isolate" , "treatment" , "days", "replicate" |
"SKA34" , "viable_co" , 18L , "one" |
"SKA34" , "mono" , 6L , "one" |
"SKA34" , "killed_co" , 4L , "one" |
"SKA34" , "killed_coco", 1L , "one" |
"MED222" , "viable_co" , 18L , "one" |
"MED222" , "mono" , 10L , "one" |
"MED222" , "killed_co" , 5L , "one" |
"MED222" , "killed_coco", 5L , "one" |
"SKA34" , "viable_co" , 18L , "two" |
"SKA34" , "mono" , 4L , "two" |
"SKA34" , "killed_co" , 6L , "two" |
"SKA34" , "killed_coco", 1L , "two" |
"MED222" , "viable_co" , 18L , "two" |
"MED222" , "mono" , 8L , "two" |
"MED222" , "killed_co" , 5L , "two" |
"MED222" , "killed_coco", 5L , "two" )

Something like this perhaps?

ggplot(x) +
  geom_segment(
    data = x |> pivot_wider(names_from = replicate, values_from = days),
    aes(
      x = one,
      xend = two,
      y = paste(isolate, treatment)
    ),
    linewidth = 2
  ) +
  geom_point(data = x,
             aes(
               x = days,
               y = paste(isolate, treatment),
               color = treatment
             ),
             size = 6)

Thank for your the reply.
Almost! On the Y-axis, I only want the species name, but not the treatment. E.g., only "SKA34" and "MED222" and then for each species, four lines in different colors for each treatment. Perhaps that could be done with facet_wrap somehow?

You can simply set the labels of the Y-axis explicitly and facet the chart with

ggplot(x) +
  geom_segment(
    data = x |> pivot_wider(names_from = replicate, values_from = days),
    aes(
      x = one,
      xend = two,
      y = paste(isolate, treatment)
    ),
    linewidth = 2
  ) +
  scale_y_discrete(labels = x$treatment) +
  geom_point(data = x,
             aes(
               x = days,
               y = paste(isolate, treatment),
               color = treatment
             ),
             size = 6) +
  facet_wrap(~ isolate,
              scales = "free_y",
              ncol = 1,
              strip.position = "left") +
  theme(strip.text.y.left = element_text(angle = 0),
        strip.placement = "outside")

Thank you! This is what I'm after.
However, if I want to remove the Y-labels of the treatments (killed_coco etc), and to instead put the species names where these labels are now, is that possible to do?

You can set the labels to whatever you want

This topic was automatically closed 42 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.