2015-07-20 68 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相同的两种颜色,但由于您使用颜色来表示教育状态,这不会令人困惑吗?这意味着两个变量之间存在某种联系。 –