Reputation: 9
I factorized 2 cols of my data, when plotted its yielding different graphs, Can anyone please guide whats wrong I'm doing?
Thanks.
Here is my data:
food <- data_frame(Foods = rep(c('FoodA','FoodB','FoodC','FoodD'), times = 4),
Stage = rep(c('E', 'L', 'P', 'A'), each = 4),
Mean = c(2.3, 2.1, 4.3, 3.8, 19.8, 29.0, 42.2, 29.5, 8.4, 11.9, 15.0, 12.1, 10.0, 9.1, 9.1, 11.8),
SD = c(0.49, 0.35, 0.70, 0.59, 1.13, 1.71, 1.72, 1.53, 0.63, 1.11, 0.74, 0.51, 0.39, 0.52, 0.37, 0.27))
I want to sort Foods
col in alphabetical order, hence, I have factored Foods
col and plotted. Graphs correctly represents as per Mean
col.
#Foods
col factored:
food <- food |> mutate(Foods = factor(Foods, labels = sort(unique(Foods))))
food |> ggplot(aes(Foods, Mean, fill = Stage)) + geom_bar(stat = 'identity', position = 'stack')
I aslo want to stack bars in 'E', 'L', 'P', 'A' order. hence, when I factorize the col: Stage
and plotted. But, this time graphs looks differently. 'L' data is plotted to 'P'.
food <- food |> mutate(Foods = factor(Foods, labels = sort(unique(Foods)))) |> mutate(Stage = factor(Stage, labels = c('E', 'L', 'P', 'A')))
food |> ggplot(aes(Foods, Mean, fill = Stage)) + geom_bar(stat = 'identity', position = 'stack')
Here are graphs:
Graph1: [Foods
col only factorised][1] [1]: https://i.sstatic.net/Dl6bY.png
Graph2: [Foods
& Stage
col factorised][2]: https://i.sstatic.net/9YADz.png
Kindy please help me, or guide me what wrong I'm doing? why data of is wrongly plotted?
Upvotes: 0
Views: 18