如何使用ggplot()手动将颜色设置为分类变量? [英] How to manually set colours to a categorical variables using ggplot()?
问题描述
这是我的示例数据
table1
xaxis yaxis ae work
1 5 35736 Attending_Education Working
2 6 72286上岗教育工作
3 7 133316上岗教育工作
4 8 252520上岗教育工作
5 9 228964上岗教育工作
6 10 504676上岗教育工作
这是我使用的代码。
p< -ggplot(table1,aes(x = table1 $ xaxis,y = table1 $ yaxis))
Economic_Activity<-系数(表1 $工作)
Education_Status<-系数(表1 $ ae)
p< -p + geom_point(aes(color = Education_Status,shape = Economic_Activity ),size = 4)
p + xlab(人口年龄)+ ylab(参加教育机构的人数)+ ggtitle(按经济活动状况参加教育的机构::印度2001)
这是我得到的输出。
I希望在此图中做两件事。
-
我希望为此分类变量手动设置颜色(Attending_Education\Not_AE) 。例如。 Attending_Education为深绿色,Not_AE为红色。
-
在经济活动的传说中,我不需要黑色作为工作类别,而不是黑色。我需要深绿色和红色。
是R的新手。我也尝试过palette()@下面的链接。但似乎没有任何效果
如何将特定颜色分配给R中的特定类别变量?
注意:请查看我的要求。
类别Attending_Education Not_AE工作的绿色\圆形红色Red圆形Not_Working的绿色\三角形形状红色\三角形
我们非常感谢您的帮助。谢谢大家。
第一个问题,您需要使用 scale_colour_manual
:
p +
xlab(人口年龄)+
ylab(参加教育机构数量)+
ggtitle(按经济活动状况参加教育的机构::印度2001)+
scale_colour_manual(values = c( Attending_Education =深绿色, Not_AE =红色))
第二秒钟,我不清楚您想要什么。经济活动以形状而非颜色来表示。那么在该图例中显示深绿色/红色是什么意思?
This is my sample data
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
This is the code i had used.
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")
This is the output i got.
I Wish to do two things in this graph.
i wish to set color manually to this categorical variables (Attending_Education\Not_AE). For example. Dark Green color for Attending_Education and red color for Not_AE.
In the legend of economic activity, i don't need black color for working\not_working category. i need the Dark green and red color.
iam new to R. i had tried palette() too found @ below link. but nothing seems to work How to assign specfic colours to specifc categorical variables in R?
Note: please look at my requirements.
Categories Attending_Education Not_AE
Working Green color\Round Shape Red Color\Round shape
Not_Working Green color\Triangle Shape Red Color\Triangle shape
Your help is appreciated. Thanking u all.
For your first question, you need to use 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"))
For your second, I'm not clear what you want. Economic activity is represented as shape, not colour. So what would be the meaning of having dark green/red colours within that legend?
这篇关于如何使用ggplot()手动将颜色设置为分类变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!