2015-07-20 70 views
0

這是我的樣本數據如何使用ggplot()將顏色手動設置爲分類變量?

table1 
    xaxis yaxis     ae  work 
1  5 35736 Attending_Education  Working 
2  6 72286 Attending_Education  Working 
3  7 133316 Attending_Education  Working 
4  8 252520 Attending_Education  Working 
5  9 228964 Attending_Education  Working 
6  10 504676 Attending_Education  Working 

這是我用過的代碼。

p<-ggplot(table1,aes(x=table1$xaxis,y=table1$yaxis)) 

Economic_Activity<-factor(table1$work) 
Education_Status<-factor(table1$ae) 

p<-p+geom_point(aes(colour=Education_Status,shape=Economic_Activity),size=4) 
p+xlab("Population Ages")+ylab("Attending Education Institutions Count")+ggtitle("Attending Educational Institutions by Economic Activity Status :: INDIA 2001") 

這是我得到的輸出。 enter image description here

我希望在此圖中做兩件事。

  1. 我希望手動設置顏色爲這個分類變量(Attending_Education \ Not_AE)。例如。用於Attending_Education的深綠色和用於Not_AE的紅色。

  2. 在經濟活動的傳說中,我不需要工作\不工作類的黑色。我需要深綠色和紅色。

iam new to R.我曾嘗試調色板()也發現@下面的鏈接。但似乎沒有工作 How to assign specfic colours to specifc categorical variables in R?

注:請看我的要求。

Categories   Attending_Education \t \t \t Not_AE 
 
Working \t \t Green color\Round Shape \t \t Red Color\Round shape 
 
Not_Working \t Green color\Triangle Shape \t Red Color\Triangle shape

您的幫助表示讚賞。感謝大家。

回答

1

關於第一個問題,你需要使用scale_colour_manual

p + 
    xlab("Population Ages") + 
    ylab("Attending Education Institutions Count") + 
    ggtitle("Attending Educational Institutions by Economic Activity Status :: INDIA 2001") + 
    scale_colour_manual(values = c("Attending_Education" = "dark green", "Not_AE" = "red")) 

對於你的第二個,我不清楚你想要什麼。經濟活動表現爲形態,而不是顏色。那麼在那個傳奇中有什麼深綠色/紅色的意思呢?

+0

對於第一個問題,我從尼克肯尼迪得到了答案。感謝您。 –

+0

對於第二個問題,對於兩個不同的形狀,我需要與教育狀態 –

+1

@VeeramaniNatarajan相同的兩種顏色,但由於您使用顏色來表示教育狀態,這不會令人困惑嗎?這意味着兩個變量之間存在某種聯繫。 –