I have the following data in a table that I have inputted into R:
Group Light Dark
1 One 29 29
2 Two 72 54
3 Three 54 39
4 Four 17 7
5 Five 14 6
6 Six 3 9
7 Seven 8 17
What I want to do next is plot it out onto a graph, with a numerical value on the y axis and on the x axis have two bars for each "Group", one for Light and for Dark, per group. The problem I am having at the moment is I obviously cannot do plot (Group~Light)
or plot (Group~Dark)
as I have no numerical value on the y axis.
Is it possible just to have some advice on how I should format my data and then input it into R, so that I can plot it as mentioned and then carrying tests of statistical significance that will allow me to compare the various groups?
Thanks.
CodePudding user response:
To plot your data, you can mix you can tidyr::pivot_longer()
them, and then use ggplot2
:
library(ggplot2)
library(tidyr)
df %>%
pivot_longer(!Group, names_to = "color") %>%
ggplot(aes(x = Group, group = color, fill = color, y = value))
geom_bar(stat = 'identity', position = 'dodge')
CodePudding user response:
You're probably looking for barplot
.
barplot(t(d[-1]), beside=T, names.arg=d$Group, col=2:3, legend.text=names(d)[-1])
Data:
d <- structure(list(Group = c("One", "Two", "Three", "Four", "Five",
"Six", "Seven"), Light = c(29L, 72L, 54L, 17L, 14L, 3L, 8L),
Dark = c(29L, 54L, 39L, 7L, 6L, 9L, 17L)), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7"))