在geom_histogram(.. density ..,group = 1)中填充颜色 [英] Fill colors in geom_histogram(..density.., group = 1)

查看:1273
本文介绍了在geom_histogram(.. density ..,group = 1)中填充颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个geom_histogram函数的基本问题

使用数据集:

  df < -  data.frame(value = factor(rep(c(A,B),c(100,200))))
pre>

我创建了一个直方图:

  ggplot(df, aes(x = value,fill = factor(value)))+ geom_histogram()

是一个直方图,A的计数为100,B的直方图为B B

如果我用下面的密度计算密度:

  ggplot(df,aes(x = value,fill = factor(value),..density ..))+ geom_histogram()
pre>

输出是一个直方图,A的密度为1,B的密度为1.我假设原因是密度是分别在A和B上计算的。



创建的直方图:

  ggplot(df,aes( x = value,group = 1,fill = factor(value),.. density ..))+ geom_histogram()

是一个直方图,其中A是0.33,B是0.66,但填充颜色是黑色的,我无法找到一种方法来获取此版本图中以前直方图中使用的填充颜色。



如何使用基于因子(值)的填充颜色生成直方图的最后一个版本?

解决方案

我解决了这个问题:

  ggplot(df,aes(x = value, fill = factor(value)))+ 
geom_histogram(aes(y = .. count ../ sum(.. count ..)))


I have a basic problem with the geom_histogram function

With the dataset:

df <- data.frame(value = factor( rep(c("A","B"), c(100,200) )))

I create a histogram with:

ggplot(df, aes(x=value, fill = factor(value))) + geom_histogram()

and the output is a histogram with count 100 for A and 200 for B

If I instead plot the density with:

ggplot(df, aes(x=value, fill = factor(value), ..density..)) + geom_histogram()

the output is a histogram with density 1 for A and 1 for B. I assume the reason is that the density is calculated on A and B separately.

The histogram created with:

ggplot(df, aes(x=value, group = 1, fill = factor(value),..density..)) + geom_histogram()

Is a histogram where A is 0.33 and B is 0.66, but the fill color is black, and I cannot find a way to get the fill colors used in the previous histograms in this version of the plot.

How do I generate the last version of the histogram with fill colors based on factor(value)?

解决方案

I solved the problem with:

ggplot(df, aes(x=value, fill = factor(value))) + 
 geom_histogram(aes(y=..count../sum(..count..)))

这篇关于在geom_histogram(.. density ..,group = 1)中填充颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆