【问题标题】:How to add legend and table with data value into a chart with different lines using ggplot2如何使用ggplot2将具有数据值的图例和表格添加到具有不同线条的图表中
【发布时间】:2018-12-12 23:43:12
【问题描述】:

我有这个数据

TX_growth<-data.frame(year=c(2017,2016, 2015),statewide=c(61, 62,57),black=c(58,58,53),hispanic=c(59,60,55),white=c(65,64,61))

到目前为止,我使用以下代码拥有此图表:

My chart until now

    ggplot() + geom_line(data = TX_growth, aes(x=year, y= statewide), color = "blue", size=1) + 
    geom_line(data = TX_growth, aes(x=year, y= white), color = "red", size=1) + 
    geom_line(data = TX_growth, aes(x=year, y= black), color = "green", size=1) + 
    geom_line(data = TX_growth, aes(x=year, y= hispanic), color = "orange", size=1) + 
    labs(title = "Figure 1: Statewide Percent who Met or Exceeded Progress", 
         subtitle = "Greater percentage means that student subgroup progressed at higher percentage than previous year.", 
         x = "Year", y = "Percentage progress")+ theme_bw() + 
    scale_x_continuous(breaks=c(2017,2016,2015)) 

我想添加 (a) 图例,显示每行的名称和颜色,以及 (b) 下面的表格,其中包含我的数据框的所有值。像这样的:

What I want 而不是城市,我的图表将有“全州”、“白人”、“黑人”和“西班牙裔”。另外,我的表会有几年(从 2015 年到 2017 年),而不是几个月。我不想要季节或“冻结”线。我只想像他们那样添加图例和表格。

【问题讨论】:

  • 也许您应该以不同的方式构建数据,以便您可以将scale_colour_manual() 包含在您希望颜色所基于的变量中,然后它将自动包含一个图例

标签: r ggplot2 statistics visualization


【解决方案1】:

第 1 部分 - 修复图例

关于传说,这不是ggplot-方式。将您的数据从宽转换为长,然后将 what 键映射到颜色作为美学映射。

library(tidyverse)
TX_growth %>%
    gather(what, value, -year) %>%
    ggplot() +
        geom_line(aes(x=year, y= value, colour = what), size=1) +
        labs(
            title = "Figure 1: Statewide Percent who Met or Exceeded Progress",
            subtitle = "Greater percentage means that student subgroup progressed at higher percentage than previous year.",
            x = "Year", y = "Percentage progress") +
        theme_bw() +
        scale_x_continuous(breaks=c(2017,2016,2015))

第 2 部分 - 添加表格

关于表格,这似乎与Adding a table of values below the graph in ggplot2 有点重复。

综合各种帖子,我们可以使用egg::ggarrange在底部添加表格;这是一个最小的例子:

library(tidyverse)
gg.plot <- TX_growth %>%
    gather(what, value, -year) %>%
    ggplot() +
        geom_line(aes(x=year, y= value, colour = what), size=1) +
        theme_bw() +
        scale_x_continuous(breaks=c(2017,2016,2015))

gg.table <- TX_growth %>%
    gather(what, value, -year) %>%
    ggplot(aes(x = year, y = as.factor(what), label = value, colour = what)) +
        geom_text() +
        theme_bw() +
        scale_x_continuous(breaks=c(2017,2016,2015)) +
        guides(colour = FALSE) +
        theme_minimal() +
        theme(
            axis.title.y = element_blank())

library(egg)
ggarrange(gg.plot, gg.table, ncol = 1)

剩下要做的就是一些最终的图形抛光。

第 3 部分 - 经过一番打磨......

library(tidyverse)
gg.plot <- TX_growth %>%
    gather(Group, value, -year) %>%
    ggplot() +
        geom_line(aes(x = year, y = value, colour = Group)) +
        theme_bw() +
        scale_x_continuous(breaks = 2015:2017)

gg.table <- TX_growth %>%
    gather(Group, value, -year) %>%
    ggplot(aes(x = year, y = as.factor(Group), label = value, colour = Group)) +
        geom_text() +
        theme_bw() +
        scale_x_continuous(breaks = 2015:2017) +
        scale_y_discrete(position = "right") +
        guides(colour = FALSE) +
        theme_minimal() +
        theme(
            axis.title.y = element_blank(),
            axis.title.x = element_blank(),
            axis.text.x = element_blank(),
            panel.grid.major = element_blank(),
            panel.grid.minor = element_blank())

library(egg)
ggarrange(gg.plot, gg.table, ncol = 1, heights = c(4, 1))

【讨论】:

    猜你喜欢
    • 2023-04-11
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 2018-05-11
    相关资源
    最近更新 更多