为 ggplot() 中的所有 y 轴元素提供图例,其中在单个图形中使用了条形图、点和线

Providing legends for all the y axis elements in ggplot() where bar, points and line have been used in a single graph

我正在尝试使用 4 个不同的组件绘制一个包含点和线的条形图。请告诉我如何给出包含所有四个部分的图例。 提前致谢。 我的数据文件是:

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")

我的代码是:

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

这是您的版本的一种方法,它为您提供了您想要绘制在带有两个图例的图表上的数据。 一些观察:

ggplot 并没有真正允许不同的变量对图例使用相同的美学:在某些情况下可以做到,但确实很棘手

在您最初的问题中,您希望将颜色美学用于三个变量:"Deposits"、"Borrowings"、"Loans & Advances" 和两个几何对象:geom_line 和 geom_point.这要求很高!

如果您热衷于探索同一美学的多个传说,请查看这些 link:

https://www.r-bloggers.com/multiple-legends-for-the-same-aesthetic-2/但是我发现代码没有运行

ggplot 开发者的观点(他们认为"tricky"):https://github.com/tidyverse/ggplot2/issues/2492

这 link 给出了所涉及内容的概念: b

这 link 建议基本图形可能更合适。但是,基本图形对于旋转或翻转列来说并不理想! Is it possible to rotate a plot in R (base graphics)?

这给你留下了这样的东西:

  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")

如果你真的想要个人传说,你可以按如下方式安排,并与你原来的问题保持一致:


  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")


所以选择范围:

数据

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")