-1

i want to plot a stacked column using ggplot2 with R1, R2, R3 as the y variables while the varieties names remain in the x variable.

i have tried it on excel it worked but i decided importing the dataset in csv format to R for a more captivating outlook as this is part of my final year project.

          varieties   R1   R2   R3 Relative.yield   SD
1                bd 0.40 2.65 1.45           1.50 1.13
2              bdj1 4.60   NA 2.80           3.70 1.27
3              bdj2 2.40 1.90 0.50           1.60 0.98
4              bdj3 2.40 1.65 5.20           3.08 1.87
5         challenge 2.10 5.15 1.35           2.87 2.01
6             doris 4.20 2.50 2.55           3.08 0.97
7               fel 0.80 2.40 0.75           1.32 0.94
8              fel2   NA 0.70 1.90           1.30 0.85
9             felbv 0.10 2.95 2.05           1.70 1.46
10            felnn 1.50 4.05 1.25           2.27 1.55
11             lad1 0.55 2.20 0.20           0.98 1.07
12             lad2 0.50   NA 0.50           0.50 0.00
13             lad3 1.10 3.90 1.00           2.00 1.65
14             lad4 1.50 1.65 0.50           1.22 0.63
15          molete1 2.60 1.80 2.75           2.38 0.51
16          molete2 1.70 4.70 4.20           3.53 1.61
17 mother's delight 0.10 4.00 1.90           2.00 1.95
18         ojaoba1a 1.90 3.45 2.75           2.70 0.78
19         ojaoba1b 4.20 2.75 4.30           3.75 0.87
20             ojoo 2.80   NA 3.60           3.20 0.57
21            omini 0.20 0.30 0.25           0.25 0.05
22            papa1 2.20 6.40 3.55           4.05 2.14
23              pk5 1.00 2.75 1.10           1.62 0.98
24              pk6 2.30 1.30 3.10           2.23 0.90
25          sango1a 0.40 0.90 1.55           0.95 0.58
26          sango1b 2.60 5.10 3.15           3.62 1.31
27          sango2a 0.50 0.55 0.75           0.60 0.13
28          sango2b 2.95   NA 2.60           2.78 0.25
29            usman 0.60 3.50 1.20           1.77 1.53
30              yau 0.05 0.85 0.20           0.37 0.43
> barplot(yield$R1)
> barplot(yield$Relative.yield)
> barplot(yield$Relative.yield, names.arg = varieties)
Error in barplot.default(yield$Relative.yield, names.arg = varieties) : 
  object 'varieties' not found
    > ggplot(data = yield, mapping = aes(x = varieties, y = yield[,2:4])) + geom_()
Error in geom_() : could not find function "geom_"
> ggplot(data = yield, mapping = aes(x = varieties, y = yield[,2:4])) + geom()
Error in geom() : could not find function "geom"
Jon Spring
  • 55,165
  • 4
  • 35
  • 53
Matthew
  • 1
  • 2
  • 1
    Welcome to Stack Overflow! Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Feb 16 '19 at 02:47
  • The code you posted is showing error messages. It's hard to know what you're trying to do--for example, `geom_` and `geom` aren't functions, as shown by the error messages you got – camille Feb 16 '19 at 16:16

1 Answers1

0

You should put it in long format first, with tidyr::gather provides this functionality:

library(tidyverse)
gather(df[1:4],R, value, R1:R3) %>%
  ggplot(aes(varieties,value, fill = R)) + geom_col()
#> Warning: Removed 5 rows containing missing values (position_stack).

data

df <- read.table(h=T,strin=F,text=
                   "          varieties   R1   R2   R3 Relative.yield   SD
                 1                bd 0.40 2.65 1.45           1.50 1.13
                 2              bdj1 4.60   NA 2.80           3.70 1.27
                 3              bdj2 2.40 1.90 0.50           1.60 0.98
                 4              bdj3 2.40 1.65 5.20           3.08 1.87
                 5         challenge 2.10 5.15 1.35           2.87 2.01
                 6             doris 4.20 2.50 2.55           3.08 0.97
                 7               fel 0.80 2.40 0.75           1.32 0.94
                 8              fel2   NA 0.70 1.90           1.30 0.85
                 9             felbv 0.10 2.95 2.05           1.70 1.46
                 10            felnn 1.50 4.05 1.25           2.27 1.55
                 11             lad1 0.55 2.20 0.20           0.98 1.07
                 12             lad2 0.50   NA 0.50           0.50 0.00
                 13             lad3 1.10 3.90 1.00           2.00 1.65
                 14             lad4 1.50 1.65 0.50           1.22 0.63
                 15          molete1 2.60 1.80 2.75           2.38 0.51
                 16          molete2 1.70 4.70 4.20           3.53 1.61
                 17 'mother\\'s delight' 0.10 4.00 1.90           2.00 1.95
                 18         ojaoba1a 1.90 3.45 2.75           2.70 0.78
                 19         ojaoba1b 4.20 2.75 4.30           3.75 0.87
                 20             ojoo 2.80   NA 3.60           3.20 0.57
                 21            omini 0.20 0.30 0.25           0.25 0.05
                 22            papa1 2.20 6.40 3.55           4.05 2.14
                 23              pk5 1.00 2.75 1.10           1.62 0.98
                 24              pk6 2.30 1.30 3.10           2.23 0.90
                 25          sango1a 0.40 0.90 1.55           0.95 0.58
                 26          sango1b 2.60 5.10 3.15           3.62 1.31
                 27          sango2a 0.50 0.55 0.75           0.60 0.13
                 28          sango2b 2.95   NA 2.60           2.78 0.25
                 29            usman 0.60 3.50 1.20           1.77 1.53
                 30              yau 0.05 0.85 0.20           0.37 0.43"
)
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
  • I think the OP wants it the other way around, like variety is on the x-axis and the key is the fill, but it's unclear – camille Feb 16 '19 at 16:17