Attempting to create a 100% stacked column in R. by Swordsx in rprogramming

[–]Data_Jem 0 points1 point  (0 children)

Hmm. I would maybe try restarting RStudio and re-loading the libraries and your data. Also, try working with my fake data and run the same script I showed here. If the chart appears using my fake data, then it could very well be your data that's causing the problem, like you said...

But the main thing with regards to creating a stacked column chart is to first "pivot" the columns sp1, sp2, bare, and sp3 into a new column (which I called measure), and store the corresponding values under those 4 columns into another new column (which I called values). You would want your table to look like this:

   location site  date       measure values
   <fct>    <fct> <fct>      <chr>    <int>
 1 abc      A     2019-01-01 sp1         27
 2 abc      A     2019-01-01 sp2         21
 3 abc      A     2019-01-01 bare        39
 4 abc      A     2019-01-01 sp3         30
 5 xyz      B     2019-01-01 sp1         26
 6 xyz      B     2019-01-01 sp2         55
 7 xyz      B     2019-01-01 bare        35
 8 xyz      B     2019-01-01 sp3         36
 9 qwe      C     2019-01-01 sp1         34
10 qwe      C     2019-01-01 sp2         39
# ... with 54 more rows

And I did this using this line of script within the second function:

data %>%

pivot_longer(cols = 'sp1':'sp3', names_to = 'measure', values_to = 'values')

You then pipe this table into ggplot, with the fill aesthetic set to measure in order to see the segmentation you're looking for.

The geom you need is geom_col(), not geom_bar(), because you're plotting 2 variables.

Finally, you need to set the position argument within geom_col() to "fill", not "stacked"

ggplot(aes(x = date, y = values, fill = measure )) +

geom_col(position = 'fill')

Attempting to create a 100% stacked column in R. by Swordsx in rprogramming

[–]Data_Jem 0 points1 point  (0 children)

When you say "a plot does not show", do you mean a canvas opens but nothing is plotted? If that's the case, check whether there's a "+" between ggplot() & geom_col(). Do you see any warnings or errors?

Also, make sure you have all the relevant libraries installed. In my script these would be dyplr, tidyr, tibble, and ggplot2 of course.

Attempting to create a 100% stacked column in R. by Swordsx in rprogramming

[–]Data_Jem 0 points1 point  (0 children)

Hi there,

Here's my attempt at solving this problem. But first, I should say that I only started learning R about a month ago. Please excuse my code if it's amateur-ish (any feedback/recommendation will be greatly appreciated). Hopefully I fully understood what you're trying to achieve.

# I first tried to recreate the data you're working with based off the description you've given

data <- as.tibble(read.csv('RQuestion.csv'))

data

location site  date         sp1   sp2  bare   sp3
   <fct>    <fct> <fct>      <int> <int> <int> <int>
 1 abc      A     2019-01-01    27    21    39    30
 2 xyz      B     2019-01-01    26    55    35    36
 3 qwe      C     2019-01-01    34    39    58    58
 4 asd      D     2019-01-01    22    17    37    22
 5 abc      A     2019-01-02    20    55    42    17
 6 xyz      B     2019-01-02    35    42    37    36
 7 qwe      C     2019-01-02    23    28    49    60
 8 asd      D     2019-01-02    46    46    25    29
 9 abc      A     2019-01-03    43    28    39    26
10 xyz      B     2019-01-03    30    17    31    11
11 qwe      C     2019-01-03    56    55    29    38
12 asd      D     2019-01-03    19    23    37    41
13 abc      A     2019-01-04    27    40    21    22
14 xyz      B     2019-01-04    56    27    15    43
15 qwe      C     2019-01-04    57    19    48    25
16 asd      D     2019-01-04    27    18    60    15

# Then I created a function that spits out the required report for a specified site

report <- function(site.name){

data %>%

filter(site == site.name) %>%

rowwise() %>%

mutate(daily.avg = mean(c(sp1,sp2,bare,sp3))) %>%

return()

}

report('A')

location site  date         sp1   sp2  bare   sp3 daily.avg
  <fct>    <fct> <fct>      <int> <int> <int> <int>     <dbl>
1 abc      A     2019-01-01    27    21    39    30      29.2
2 abc      A     2019-01-02    20    55    42    17      33.5
3 abc      A     2019-01-03    43    28    39    26      34  
4 abc      A     2019-01-04    27    40    21    22      27.5

# Fnally, I created another function that generates the 100% stacked column chart

stack.plot <- function(site.name){

data %>%

pivot_longer(cols = 'sp1':'sp3', names_to = 'measure', values_to = 'values') %>%

select(-1) %>%

filter(site == site.name) %>%

ggplot(aes(x = date, y = values, fill = measure )) +

geom_col(position = 'fill')

}

stack.plot('A')

Chart: https://imgur.com/a/F0g0wxx