Adding features to a plot

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)

Here is a rough version of what I think you want.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)
library(forcats)
Table1 <- read.csv("~/R/Play/Dummy.csv")

Table1 %>%
  
  mutate(Food.description = fct_reorder(Food.description,Ratio)) %>%
  arrange(desc(Food.description)) %>% 
  mutate(CumSum = cumsum(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, linetype = "Cumsum"), col="black") + 
  geom_text(aes(y = CumSum, label = c("", CumSum[2:14]))) +
  scale_linetype_manual(values = c("Cumsum" = "dashed")) + labs(linetype = "")

Created on 2021-05-31 by the reprex package (v0.3.0)

Thanks FJCC. That is exactly what I needed.
Although, could you suggest me how to modify the legend title, so I get something like "Cumulative ratio" instead "Cumsum"?
Appreciated.

You just need to change this part of the code.

...
theme_bw()+
  
  geom_line(aes(x=Food.description, y=cumsum(Ratio),group=1, linetype = "Cumulative ratio"), col="black") + 
  geom_text(aes(y = CumSum, label = c("", CumSum[2:14]))) +
  scale_linetype_manual(values = c("Cumulative ratio" = "dashed")) + labs(linetype = "")

Thanks again FJCC. That was extremely useful.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.