【问题标题】:How to manually set colours to a categorical variables using ggplot()? [duplicate]如何使用 ggplot() 手动将颜色设置为分类变量? [复制]
【发布时间】:2015-10-09 11:43:50
【问题描述】:

这是我的示例数据:

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")

这是我得到的输出。

我希望在这张图中做两件事。

  1. 我希望手动为这些分类变量设置颜色 (Attending_Education\Not_AE)。例如。 Attending_Education 为深绿色,Not_AE 为红色。

  2. 在经济活动的传说中,工作\非工作类别不需要黑色。我需要深绿色和红色。

我是 R 新手。我也尝试过 palette(),也发现了 @ 下面的链接。但似乎没有任何效果 How to assign specfic colours to specifc categorical variables in R?

注意:请看我的要求。

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

感谢您的帮助。谢谢大家。

【问题讨论】:

    标签: r ggplot2 categorical-data


    【解决方案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"))
    

    对于你的第二个,我不清楚你想要什么。经济活动用形状而不是颜色来表示。那么在该图例中使用深绿色/红色意味着什么?

    【讨论】:

    • 第一个问题我从尼克肯尼迪那里得到了答案。谢谢你。
    • 第二个问题,对于两种不同的形状,我需要与教育状态相同的两种颜色
    • @VeeramaniNatarajan 但是由于您使用颜色来表示教育状况,这不会令人困惑吗?这意味着这两个变量之间存在某种关联。
    • 我仅在过去几周内学习 R。我知道这很混乱。但作为新人,我不知道要说清楚
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多