【问题标题】:How do you create a scatterplot from a .csv file with multiple columns/subsets?如何从具有多个列/子集的 .csv 文件创建散点图?
【发布时间】:2019-04-17 18:25:07
【问题描述】:

我想从包含多个数据子集的 .csv 文件创建散点图。我想比较变量并包含一个键。这是我的数据集的一个示例(完整的数据集从 1900 年到 2014 年)。

Year    Race    Sex ALE
1900    Both    Both Sexes  47.3
1900    Both    Female  48.3
1900    Both    Male    46.3
1900    African American    Both Sexes  33
1900    African American    Female  33.5
1900    African American    Male    32.5
1900    Caucasian   Both Sexes  47.6
1900    Caucasian   Female  48.7
1900    Caucasian   Male    46.6

我已将我的数据集命名为:“生活” 该图以四条散点图线显示,但均以蓝色显示。

options(scipen = 999)
library(scales)
ggplot(data=life, aes(x=Year, y=ALE, group=1)) + 
  geom_point(colour="blue", size=.5, shape=9, fill="blue") +
  xlab("Year") + 
  ylab("Life Expectancy") + 
  ggtitle("Average Life Expectancy") 

我想看到一个散点图,其中男性、女性、非裔美国人、高加索人的比较是各自的一条线,用一个单独的颜色和一个键。我错过了一些我无法弄清楚的重要脚本。有没有办法画一条最合适的线?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您可以尝试以下方法吗:

    ggplot(data = lif, aes(x = Year, y = ALE)) +
      geom_point(aes(colour = interaction(Race, Sex),
                 size = .5, shape = 9) +
      geom_smooth() +
      xlab("Year") +
      ylab("Life Expectancy") +
      ggtitle("Average Life Expectancy")
    

    我没有您的数据,因此无法测试此代码,但我认为aes(colour = interaction(Race, Sex)) 可能会有所帮助。

    【讨论】:

      猜你喜欢
      • 2017-09-15
      • 1970-01-01
      • 1970-01-01
      • 2016-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-10
      • 1970-01-01
      相关资源
      最近更新 更多