Hello,
I have a data frame Table1 with the following structure.
Food description | Mean.ch | Mean | Ratio.ch | Ratio |
---|---|---|---|---|
A | 10 | 10 | 7.48 | 7.48 |
B | 8.0E | 8 | 5.98E | 5.98 |
C | 8 | 8 | 5.98 | 5.98 |
D | 9.5 | 9.5 | 7.11 | 7.11 |
E | 4 | 4 | 2.99 | 2.99 |
F | 6.9 | 6.9 | 5.16 | 5.16 |
G | 11 | 11 | 8.23 | 8.23 |
H | 15 | 15 | 11.22 | 11.22 |
I | 13.0E | 13 | 9.72E | 9.72 |
J | 9.3 | 9.3 | 6.96 | 6.96 |
K | F | 5 | F | 3.74 |
L | 11 | 11 | 8.23 | 8.23 |
M | 16 | 16 | 11.97 | 11.97 |
N | 7 | 7 | 5.23 | 5.23 |
I created a plot with the following code:
Table1 %>%
mutate(Food.description = fct_reorder(Food.description,Ratio)) %>%
ggplot( aes(x=Food.description, y=Ratio)) +
geom_bar(stat="identity", fill="skyblue", alpha=.6, width=.4) +
coord_flip() +
ggtitle("Top 10 foods consumed" , ) +
xlab("Food description") +
ylab("Ratio") +
geom_text(aes(label=Ratio.ch), size = 2.7, col = "gray9") +
theme_bw()+
geom_line(aes(x=Food.description, y=cumsum(Ratio),group=1), lty = 2, col="black")
The resulting plot is attached.
The broken line represent cumulative “Ratio” values as computed in the last line of code. I would like to add the following information to the plot, but I don’t know how to do it:
- A legend specifying the broken like added in the last line of code represents “Cumulative ratio” values.
- The actual cumulative values where the row of the food items and the broken lines intercept. That’s to say, starting at the second food item (from top to bottom), include 23.19 (11.97+11.22), 32.89 (11.97+9.7), …
Thank you,
AG
Additional relevant information:
OS: Windows 10 (64-bit)
R version: 3.6.2
R studio version: 3.5
Loaded packages: (not sure whether I need all of them for the plot, but they are included in my code)
library(forcats)
library(ggplot2)
library(dplyr)
library(readr)
library(formattable)
library(tidyr)