diff --git a/ch03.Rmd b/ch03.Rmd index d67b8b0..b93ecc4 100644 --- a/ch03.Rmd +++ b/ch03.Rmd @@ -604,14 +604,14 @@ ggplot(cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar)) + ) ``` -Putting labels on stacked bar graphs requires finding the cumulative sum for each stack. To do this, first make sure the data is sorted properly -- if it isn't, the cumulative sum might be calculated in the wrong order. We'll use the `arrange()` function from the dplyr package. Note that we have to use the `rev()` function to reverse the order of `Cultivar`: +Putting labels on stacked bar graphs requires finding the cumulative sum for each stack. To do this, first make sure the data is sorted properly -- if it isn't, the cumulative sum might be calculated in the wrong order. We'll use the `arrange()` function from the dplyr package. Note that we have to use the `desc()` function from the dplyr package to reverse the order of `Cultivar`: ```{r} library(dplyr) # Sort by the Date and Cultivar columns ce <- cabbage_exp %>% - arrange(Date, rev(Cultivar)) + arrange(Date, desc(Cultivar)) ``` Once we make sure the data is sorted properly, we'll use `group_by()` to chunk it into groups by `Date`, then calculate a cumulative sum of `Weight` within each chunk: @@ -637,7 +637,7 @@ To put the labels in the middle of each bar (Figure \@ref(fig:FIG-BAR-LABEL-STAC ```{r FIG-BAR-LABEL-STACKED-MIDDLE, fig.cap="Labels in the middle of stacked bars", fig.height=3.5} ce <- cabbage_exp %>% - arrange(Date, rev(Cultivar)) + arrange(Date, desc(Cultivar)) # Calculate y position, placing it in the middle ce <- ce %>% @@ -766,4 +766,4 @@ ggplot(tophit, aes(x = avg, y = name)) + For more on changing the order of factor levels, see Recipe \@ref(RECIPE-DATAPREP-FACTOR-REORDER). Also see Recipe \@ref(RECIPE-DATAPREP-FACTOR-REORDER-VALUE) for details on changing the order of factor levels based on some other values. -For more on moving the legend, see Recipe \@ref(RECIPE-LEGEND-POSITION). To hide grid lines, see Recipe \@ref(RECIPE-APPEARANCE-HIDE-GRIDLINES). \ No newline at end of file +For more on moving the legend, see Recipe \@ref(RECIPE-LEGEND-POSITION). To hide grid lines, see Recipe \@ref(RECIPE-APPEARANCE-HIDE-GRIDLINES).