简体   繁体   English

ggplot2中的着色密度图

[英]Coloring density plot in ggplot2

When I use following code to generate a density plot: 当我使用以下代码生成密度图时:

require(ggplot2)
set.seed(seed=10)
n <- 10000
s.data <- data.frame(score = rnorm(n,500,100),
                     gender = sample(c("Male","Female","No Response"),size=n,replace=T,prob=c(.4,.55,.05)),
                     major = sample(c("A","B","C","D"),size=n,replace=T,prob=c(.02,.25,.05,.68))) 
ggplot(s.data, aes(major,..density..,fill=major,group=1)) + 
  geom_histogram() + facet_wrap(~ gender)

在此处输入图片说明

I cannot distinguish between categories of "major" by color. 我无法按颜色区分“主要”类别。

What I want to get is density plot similar to this frequency plot in the sense of colors and legend: 我要得到的是在颜色和图例意义上类似于该频率图的密度图:

ggplot(s.data, aes(major,fill=major)) + 
    geom_histogram() + facet_wrap(~ gender)

在此处输入图片说明

This question is following my question ( here ) which is already answered here . 这个问题在我的问题( 这里 )之后,已经在这里回答

You can still try frequency plot with facet parameter scale="free_y" : 您仍然可以尝试使用方面参数scale="free_y"绘制频率图:

ggplot(s.data, aes(major,..count..,fill=major)) + 
  geom_histogram() + facet_wrap(~ gender, scale="free_y")

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM