【问题标题】:Which suitable plot for this Data in R? [closed]哪个适合 R 中的数据? [关闭]
【发布时间】:2021-07-15 19:49:16
【问题描述】:

我是 R 中数据可视化的初学者,我想将这些数据绘制成每个国家/地区在同一个图中显示多年来的肉类、牛肉和鱼类消费量,哪种类型的图可能合适?

df <- data.frame(country=c("Italy", "France", "Germany","Italy", "France", "Germany","Italy", "France", "Germany","Italy", "France", "Germany"),
                 years=c(1992, 1992, 1992,1995,1995,1995,1997,1997,1997,1999,1999,1999),
                 meat=c(7,8,9,10,11,12,13,14,15,16,17,18),
                 beef=c(70,80,90,100,110,120,130,140,150,160,170,180),
                 fish=c(10,20,30,40,50,60,70,80,90,100,110,120)
)

【问题讨论】:

    标签: r data-visualization


    【解决方案1】:

    我投票结束这个问题,因为我认为这会导致基于意见的答案。不过,您可以使用 ggplot2 并绘制每个国家/地区每年的消费量和产品:

    library(tidyr)
    library(ggplot2)
    
    df %>% 
      pivot_longer(c("meat", "beef", "fish")) %>% 
      ggplot(aes(x=years, y=value, color=name)) +
      geom_path() + 
      facet_wrap(~ country) + 
      labs(title = "Meat, beef, and fish consumption over years per country")
    

    给你

    如果您想使用ggplot2,请查看Top 50 ggplot2 Visualizations - The Master List (With Full R Code) 的绘图示例。选择认为合适的剧情。

    编辑

    如果您想将所有国家绘制成一个图并用不同的线型区分它们,您可以使用

    df %>% 
      pivot_longer(c("meat", "beef", "fish")) %>% 
      ggplot(aes(x=years, y=value, color=name)) +
      geom_line(aes(linetype=country)) + 
      labs(title = "Meat, beef, and fish consumption over years per country")
    

    如果要汇总所有国家/地区,请删除 geom_line()aes(linetype=country)

    【讨论】:

    • 谢谢你的回答,其实我希望所有国家都在同一个地块上。
    • 添加了新的解决方案。
    猜你喜欢
    • 2011-11-25
    • 1970-01-01
    • 2019-02-09
    • 2014-08-11
    • 1970-01-01
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多