首页 > 解决方案 > 如何在ggplot2中将颜色添加到构面网格

问题描述

我在 ggplot 中做了一个 facet_grid,如下所示:

    ggplot(DATASET, aes((jitter(WordCount)),Retweet, group=1))+
      geom_point(aes(alpha=1/150))+
      facet_grid(Dictionary ~ ., margins = TRUE, scales = "free")+
      labs(x = "Word Count", y = "Retweet Count", title = "A")+
      theme_bw()+ 
      theme(legend.position = "none")

在此处输入图像描述

如何使每个图形使用不同颜色的点?

作为参考,这是我的数据框的结构:

data.frame':    939006 obs. of  4 variables:
 $ Retweet   : num  1388 762 748 436 342 ...
 $ id        : int  1 2 3 4 5 6 7 8 9 10 ...
 $ Dictionary: Factor w/ 3 levels "Emotional","Moral",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ WordCount : num  1 1 2 2 1 1 3 1 2 1 ...

标签: rggplot2aes

解决方案


正如评论中回答的那样,为颜色美学分配一个变量。

ggplot(DATASET, aes(WordCount), etweet, group = 1, color = Dictonary)) +
  geom_jitter(aes(alpha = 1/150)) +
  facet_grid(Dictionary ~ ., margins = TRUE, scales = "free") +
  labs(x = "Word Count", y = "Retweet Count", title = "A") +
  theme_bw() + 
  theme(legend.position = "none")

推荐阅读