[英]How do you create a scatterplot from a .csv file with multiple columns/subsets?
我想從.csv文件創建一個散點圖,其中包含多個數據子集。 我想比較變量並包含一個Key。 這是我的數據集的一個例子(全套從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")
我想看一個散點圖,其中男性,女性,非洲裔美國人,高加索人的比較各自是一個單獨的顏色和一把鑰匙。 我錯過了一些我無法弄清楚的重要腳本。 有沒有辦法畫一條最合適的線?
你能嘗試以下方法嗎?
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))
可能會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.