Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用百分比时美学中的组参数_R_Ggplot2 - Fatal编程技术网

R 使用百分比时美学中的组参数

R 使用百分比时美学中的组参数,r,ggplot2,R,Ggplot2,这是“数据科学R”中的数据可视化问题 问题5。在我们的比例条形图中,我们需要设置group=1。为什么?换句话说,这两张图有什么问题 ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, y = ..prop..)) ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = color, y = ..prop..)) 正如您所看到的代码及其结果,y

这是“数据科学R”中的数据可视化问题

问题5。在我们的比例条形图中,我们需要设置group=1。为什么?换句话说,这两张图有什么问题

ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, y = ..prop..))

ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, fill = color, y = ..prop..))
正如您所看到的代码及其结果,y-axix中存在一个问题,因为不包括组参数。我很清楚,所以在把原始代码改成下面的代码之后

ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))

ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, fill = color, y = ..prop.., group = 1))

ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, fill = color, y = ..prop.., group = color))
我可以处理第一个,它是黑色的。但问题是下一个。我知道,包括“group=1”将与黑条一样,因此将“group=1”更改为“group=color”。y轴现在是道具,但它不是我想要的,因为剪切变量(x轴)中所有级别的整体比例之和必须为1(=100%),但它超过了1。我想更改比例(y轴)的值

如果你能帮我解决这个问题,我将不胜感激


.prop..
计算组内的百分比。它需要一个分组变量,否则每个
x
都是它自己的组,
prop
=1,即每个
x
都是100%

当你把
group=1
prop
放在一起时,它是所有条目x的百分比,因为所有条目都属于同一个组。你已经发现了

在上一个绘图中,当您按
颜色进行分组时,百分比是在颜色范围内计算的。这意味着每种颜色的总和为1

这就是你想要达到的目标吗

ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, fill = color, y = ..count../sum(..count..)), position = "fill")


谢谢您回答这个问题!有没有其他方法可以绘制一个比例图,作为保持条形图形状的y轴?我的意思是切割变量的每一条不应该是100%,每一个比例的总和是1.0(100%),我现在弄明白了,我修改了你写的代码,特别是删除了位置,以便现在将条堆叠起来。谢谢你的回答!