ggplot根据数据帧中的值重新排序堆积条形图 [英] ggplot reorder stacked bar plot based on values in data frame
问题描述
我正在用R中的ggplot2制作堆叠的条形图,其中特定的条形码排列在y轴上。
#创建可再现的数据
library(ggplot2)
d < - read.csv(text ='日期,地点,长度,金额
1,4,3,1.1
1,3,1,2
1,2,3,4
1,1,3, 5
2,0,0,0
3,3,3,1.8
3,2,1,3.54
3,1,3,1.1',header = T )
ggplot(d,aes(x = Day,y = Length))+ geom_bar(aes(fill = Amount,order = Location),stat =identity)
ggplot(d,aes(x = Day,y = Length))+ geom_bar(aes(fill = Amount,order = rev(Location)),stat =identity)
第一张ggplot图按位置顺序显示数据,位置= 1最接近x轴,每个增加的位置值的数据堆叠在一起第二个ggplot图以不同的顺序显示数据,但它不会将数据与最靠近X轴的位置值叠加在一起,下一个最高位置堆栈的数据d在第一个柱栏的x轴位置的第二个,就像我期望它基于以前的帖子。
下面的代码片段确实以所需的方式显示数据,但我认为这是简单和小型示例数据集的一个伪影。没有指定堆叠顺序,所以我认为ggplot是基于Amount的值进行堆叠。
ggplot(d,aes( x = Day,y = Length))+ geom_bar(aes(fill = Amount),stat =identity)
$ b $我想要的是强制ggplot以递减的位置值(位置= 4最接近x轴,位置= 3下一个,...和位置= 1位于栏柱最顶端)通过调用 order =
或一些等价的参数。任何想法或建议?
它似乎应该很容易,因为我只处理数字。如果要求ggplot以对应于减少的列(如果您离开x轴)数字的方式堆叠数据,应该不难吗?
试试:
ggplot(d,aes(x = Day ,y =长度))+
geom_bar(aes(fill = Amount,order = -Location),stat =identity)
请注意我是如何将 rev
与 -
交换的。使用 rev
可以做出非常不同的事情:如果您颠倒列 Location中的值的顺序, / code>,这可能就是任何事情。
I am making stacked bar plots with ggplot2 in R with specific bar ordering about the y-axis.
# create reproducible data
library(ggplot2)
d <- read.csv(text='Day,Location,Length,Amount
1,4,3,1.1
1,3,1,2
1,2,3,4
1,1,3,5
2,0,0,0
3,3,3,1.8
3,2,1,3.54
3,1,3,1.1',header=T)
ggplot(d, aes(x = Day, y = Length)) + geom_bar(aes(fill = Amount, order = Location), stat = "identity")
ggplot(d, aes(x = Day, y = Length)) + geom_bar(aes(fill = Amount, order = rev(Location)), stat = "identity")
The first ggplot plot shows the data in order of Location, with Location=1 nearest the x-axis and data for each increasing value of Location stacked upon the next.
The second ggplot plot shows the data in a different order, but it doesn't stack the data with the highest Location value nearest the x-axis with the data for the next highest Location stacked in the second from the x-axis position for the first bar column, like I would expect it to based on an earlier post.
This next snippet does show the data in the desired way, but I think this is an artifact of the simple and small example data set. Stacking order hasn't been specified, so I think ggplot is stacking based on values for Amount.
ggplot(d, aes(x = Day, y = Length)) + geom_bar(aes(fill = Amount), stat = "identity")
What I want is to force ggplot to stack the data in order of decreasing Location values (Location=4 nearest the x-axis, Location=3 next, ... , and Location=1 at the very top of the bar column) by calling the order =
or some equivalent argument. Any thoughts or suggestions?
It seems like it should be easy because I am only dealing with numbers. It shouldn't be so hard to ask ggplot to stack the data in a way that corresponds to a column of decreasing (as you move away from the x-axis) numbers, should it?
Try:
ggplot(d, aes(x = Day, y = Length)) +
geom_bar(aes(fill = Amount, order = -Location), stat = "identity")
Notice how I swapped rev
with -
. Using rev
does something very different: it stacks by the value for each row you happen to get if you reverse the order of values in the column Location
, which could be just about anything.
这篇关于ggplot根据数据帧中的值重新排序堆积条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!