1

all Treatment are in the same bar but I want them separated in a graph Species wise differentiated by color.

I tried with the below code but it's not separating Treatments

   Species Treatment avg_n_roots avg_lng_root avg_dia_root
   <chr>   <chr>           <dbl>        <dbl>        <dbl>
 1 x1      t1                  4            7          0.6
 2 x1      t2                  5            6          0.9
 3 x1      t3                  4            8          0.7
 4 x1      t4                  3            6          0.8
 5 x2      t1                  4            8          0.9
 6 x2      t2                  5            7          0.3
 7 x2      t3                  8            8          0.5
 8 x2      t4                  3            5          0.7
 9 x3      t1                  4            6          0.3
10 x3      t2                  5            5          0.7
11 x3      t3                  6            4          0.7
12 x3      t4                  9            3          0.8
ths_data_1 |> 
  tidyr::pivot_longer(c(avg_n_roots, avg_lng_root, avg_dia_root)) |> 
  ggplot(aes(x = Species, y = value, color = name)) + 
  geom_col(alpha = 0, position = "dodge") + 
  facet_wrap(~Species, scales = "free_x") + theme_minimal() +
 theme(legend.position = "bottom")

I want my graph to look like this, differentiated for each treatment.

Croton_the

Kat
  • 15,669
  • 3
  • 18
  • 51
  • 1
    It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. If you want to post your data type `dput(NAME_OF_DATASET)` into the console and copy the output starting with `structure(....` into your post. If your dataset has a lot of observations you could do e.g. `dput(head(NAME_OF_DATASET, 10))` for the first ten rows of data. – stefan Nov 19 '22 at 10:05
  • 1
    This said it's not completely clear how your final chart should look like. But as a first step I would suggest to reshape your data, i.e. do something like `ths_data_1 |> tidyr::pivot_longer(c(avg_n_roots, avg_lng_root, avg_dia_root)) |> ggplot(aes(x = Species, y = value, fill = name)) + geom_col(alpha = 0.3, position = "dodge") + theme(legend.position = "bottom")` – stefan Nov 19 '22 at 10:07
  • I applied `facet_wrap(~Species)` on your code which helped but I still want to differentiate between treatments. – Nava Ajay Shankar A Nov 19 '22 at 13:29
  • If you facet by `Species` then perhaps you could map `Treatment` on x. – stefan Nov 19 '22 at 13:35

0 Answers0