0
votes

My data frame looks like this:

plant  distance
one      1
one      3
one      2
one      3
one      7
one      4
one      6
one      8
one      9
two      1
two      6
two      4
two      8
two      5
two      3
three ……

I want to split distance of each level into groups by interval(for instance,interval=3).

plant  distance group
  one      1    1
  one      3    1
  one      2    1
  one      3    1
  one      7    3
  one      4    2
  one      6    2
  one      8    3
  one      9    3
  two      1    1
  two      6    2
  two      4    2
  two      8    3
  two      5    2
  two      3    1
  three ……

And compute percentage of each group

plant group percentage
one     1     0.44
one     2     0.22
one     3     0.33
two     1     0.33
two     2     0.50
two     3     0.17
three ……

Finally, plot the percentages of each level of each group similar like this: enter image description here

and I do not know how to split each level by interval. Sorry for my English! Thank you for your help!

1
What exactly do you want? What have you tried? For example, you could split your data frame by using split(my_data$distance, my_data$plant), you could group it by cut(my_data$distance, breaks = seq(min(my_data$distance), max(my_data$distance), 3)) etc.lukeA
@lukeA Thank you for your reply, I have tried many times and I have updated the post.just_rookie

1 Answers

1
votes

Here's one way to do it using dplyr:

library(dplyr)
library(ggplot2)
my_data %>%
  mutate(group = factor(cut(distance, seq(0, max(distance), 3), F))) %>%
  group_by(plant, group) %>%
  summarise(percentage = n()) %>%
  mutate(percentage = percentage / sum(percentage)) %>%
  ggplot(aes(x = plant, y = percentage, fill = group)) + 
  geom_bar(stat = "identity", position = "stack")

enter image description here