0

I am trying to draw a barplot with point and line together using 4 different components. Please show me how to give legends comprising all the four components. thanks in advance. My datafile is:

d1 <- c("Uttar Pradesh", "Rajasthan","Maharashtra","Madhya Pradesh")
d2 <- c(12142, 12357, 15422, 17986)
d3 <- c(26571, 22123, 28119, 21177)
d4 <- c(38877, 31496, 35606, 37158)
d5 <- c(98145, 76275, 88596, 95433)
compend6 <- data.frame(d1, d2, d3, d4, d5)
colnames(compend6) <- c("Name of the State/ UT", "Deposits", "Borrowings", "Loans & Advances", "Total Liabilities")

My code is:

positions <- compend6$`Name of the State/ UT`
plot1_name <- "Total Liabilities"
plot2_name <- "Deposits"
plot3_name <- "Loans & Advances"
plot4_name <- "Borrowings"

gp1 <- compend6 %>% ggplot(aes(group = 1)) + 
  geom_bar(mapping = aes(x = `Name of the State/ UT`, y = .data[[plot1_name]]), 
           stat = "identity", fill = "yellow", color = "Black") + 
  geom_point(mapping = aes(x = `Name of the State/ UT`, y = .data[[plot2_name]], 
                           size = .data[[plot2_name]]), color = "Blue") + 
  geom_point(mapping = aes(x = `Name of the State/ UT`, y = .data[[plot3_name]], 
                           size = .data[[plot3_name]]), color = "Red", shape = 15) + 
  geom_line(mapping = aes(x = `Name of the State/ UT`, y = .data[[plot4_name]]), 
            color = "#218906", size = 2) + 
  ylab(expression("Balance Sheet Size")) +
  scale_x_discrete(limits = positions) +
  scale_y_continuous(limits = c(0,100000)) +
  theme(legend.position = c(0.8, 0.8)) +
  coord_flip()
gp1

my graph looks like this

I want the legends showing:

Total Liabilities = Yellow Bar
Deposits = Blue filled circle
Loans & Advances = Red square; and
Borrowings = Green line
  • Welcome to Stack Overflow. Could you please state for what variables you want legends and what values will be in each legend: in other words set out each legend title and the legend text. If you could include the data for the question that would help enormously: use `dput(compend)` if this is a large dataset consider what you need to provide a minimal working example these links [reprex] and [MWE] will be a great help. – Peter May 09 '20 at 10:35
  • Thanks; any chance you can put an extract of your data into the question as a dataframe or tibble object to make your question reproducible? – Peter May 09 '20 at 11:52
  • Sorry if I was not clear: please paste the tibble `compend <- tibble(`Name of the State/ UT` = c(...values...), ...other variables etc...)` into the question. – Peter May 09 '20 at 12:08
  • That's really helpful - thanks and now the other clarification: Could you please state for what variables you want legends and what values will be in each legend: in other words set out each legend title and the respective legend text requirements. For example do you want a legend for the green line comprising a green line and the legend text "borrowings"? – Peter May 09 '20 at 12:57
  • Some points for clarification: 1) Is there a reason to have borrowings as a line rather than a shape? 2) do the red square and blue circle have to have their size proportional to their value? As they are mapped to the y-axis which gives the value in any case? – Peter May 09 '20 at 15:12
  • I had another thought and fixed the graph so that it is more like your original question. If it answers your question please accept the answer. – Peter May 09 '20 at 18:24
  • Oh !! YES. That's exactly what I wanted. Thanks dear. – Makarand Padhye May 10 '20 at 02:22

1 Answers1

0

Here is one approach to your a version which gives you the the data you want plotted on one graph with two legends. Some observations:

ggplot does not really allow the same aesthetic to be used by different variables for legends: It can be done in some cases but it is really tricky

In your original question you wanted colour aesthetic to be used for three variables: "Deposits", "Borrowings", "Loans & Advances" and two geoms: geom_line and geom_point. This is quite demanding!

If you are keen to explore multiple legends for the same aesthetic, check out these links:

https://www.r-bloggers.com/multiple-legends-for-the-same-aesthetic-2/ but I found the code did not run

The view from ggplot developers ( they consider it "tricky"): https://github.com/tidyverse/ggplot2/issues/2492

This link gives an idea of what is involved: How to have two different size legends in one ggplot? b

This link suggests base graphics may be more suitable. However, base graphics is not ideal for rotating or flipping columns! Is it possible to rotate a plot in R (base graphics)?

Which leaves you with something like this:

  library(dplyr)
  library(ggplot2)

  # data for points (or any other geom) for plotting on the bar graph in long format
  money <- 
    compend6 %>% 
    select(-`Total Liabilities`) %>% 
    pivot_longer(cols = c("Deposits", "Borrowings", "Loans & Advances"), names_to = "type", values_to = "val")

  # money

  # as points  

  ggplot(compend6,  aes(x = `Name of the State/ UT`))+
    geom_col(aes(y = `Total Liabilities`, fill = "Total Liabilities"), colour = "black")+
    scale_fill_manual(name = NULL, values = "yellow") +
    geom_point(data = money, aes(x = `Name of the State/ UT`, y = val, colour = type), size = 5) +
    scale_colour_manual(name = "Finance type", breaks = c("Deposits", "Borrowings", "Loans & Advances"), values = c("blue", "#218906", "red")) +
    coord_flip()+
    ggtitle("Two way legend with bar and points")



  # as lines    
  ggplot(compend6,  aes(x = `Name of the State/ UT`))+
    geom_col(aes(y = `Total Liabilities`, fill = "Total Liabilities"), colour = "black")+
    scale_fill_manual(name = NULL, values = "yellow") +
    geom_line(data = money, aes(x = `Name of the State/ UT`, y = val, colour = type, group = type), size = 4) +
    scale_colour_manual(name = "Finance type", breaks = c("Deposits", "Borrowings", "Loans & Advances"), values = c("blue", "#218906", "red")) +
    coord_flip()+
    ggtitle("Two way legend with bar and lines")

If you really want individual legends you can arrange things as follows and is in keeping with your original question:


  ggplot(compend6,  aes(x = `Name of the State/ UT`))+
    geom_col(aes(y = `Total Liabilities`, fill = "Total Liabilities"), colour = "black")+
    scale_fill_manual(name = NULL, values = "yellow") +
    geom_line(aes(y = Borrowings, linetype = "Borrowings", group = 1), colour = "#218906", size = 2)+
    scale_linetype_manual(name = NULL, values = "solid")+
    geom_point(aes(y = Deposits, shape = "Deposits"), colour = "blue", size = 6)+
    scale_shape_manual(name = NULL, values = 16)+
    geom_point(aes(y = `Loans & Advances`, colour = "Loans & Advances"), shape = 15, size = 6)+
    scale_colour_manual(name = NULL, values = "red")+
    coord_flip()+
    ggtitle("Four way legend with bar, lines and points")


so a range of choices:

enter image description here Data

d1 <- c("Uttar Pradesh", "Rajasthan","Maharashtra","Madhya Pradesh")
d2 <- c(12142, 12357, 15422, 17986)
d3 <- c(26571, 22123, 28119, 21177)
d4 <- c(38877, 31496, 35606, 37158)
d5 <- c(98145, 76275, 88596, 95433)
compend6 <- data.frame(d1, d2, d3, d4, d5)
colnames(compend6) <- c("Name of the State/ UT", "Deposits", "Borrowings", "Loans & Advances", "Total Liabilities")


Peter
  • 11,500
  • 5
  • 21
  • 31