绘制位置=“身份” ggplot2中的geom_col-某些藏在其他背后的系列是不可见的
我正在尝试以Bar-chart格式绘制年度时间序列。
我有7组年度数据,并且它们处于设定顺序(因此,相同的数据集每年具有最低的值,并且相同的数据集是每年最高的值
) ...
但是当我将其绘制为堆叠的条形图时,抽奖顺序是指某些条且不可见的。我一生不知道如何覆盖这一点。我尝试了因子和水平,无法破解它。
如果我绘制为位置=“躲避”,我可以看到组中的较早的少数数量较小,因此当我使用位置=“身份”时,它们是'后面'(即隐形)的'。
在绘制位置=“身份”时,是否有一种方法可以对大到小?
此代码与我的前几行创建了类似的数据框架:
temp2 <- as.data.frame(matrix(1:70, 5, 14))
colnames(temp2) <- c("FY", "B", "L1", "L2", "L3", "L4", "M1", "M2", "M3", "M4",
"H1", "H2", "H3", "H4")
temp2$FY <- 1976:1980
temp2$B <- c(800, 935, 650, 850, 650)
temp2$L1 <- temp2$B * 1.0149
temp2$L2 <- temp2$B * 1.0161
temp2$L3 <- temp2$B * 1.0191
temp2$L4 <- temp2$B * 1.0269
temp2$H1 <- temp2$B * 0.9323
temp2$H2 <- temp2$B * 0.9269
temp2$H3 <- temp2$B * 0.9135
temp2$H4 <- temp2$B * 0.8787
temp2$M1 <- temp2$B * 0.9867
temp2$M2 <- temp2$B * 0.9856
temp2$M3 <- temp2$B * 0.9827
temp2$M4 <- temp2$B * 0.9754
temp2 <- gather(temp2, Type, Rain,
B:H4, factor_key=TRUE)
temp2 <- ddply(temp2, .(FY, Type), summarise,
Rain = sum(Rain, na.rm=TRUE))
然后,如果我这样做:
ggplot(temp2, aes(x=FY, y=Rain, colour=Type)) + geom_line()
...我获得了一个普通的线路图,您可以在其中看到所有系列。 但是,如果我这样做:
ggplot(temp2, aes(x=FY, y=Rain, fill=Type)) + geom_col(position="identity")
...那么一些酒吧隐藏在其他条后面。
ggplot(temp2, aes(x=FY, y=Rain, fill=Type)) + geom_col(position="dodge")
...表明第二个第四和第五条通常比第一个栏高,但最后8个栏较低。我需要通过以“类型”中的“类型”中的值对我从其他数据框架中奏效的此预定义顺序进行排序来以某种方式重新排序我的数据。
希望这很清楚。
编辑: (这是使用我的完整数据集,但希望您能获得要旨) position =“ dodge”
很明显,每个“ bundle”有7个系列。
但是位置=“身份”只有5个出现。关于每个图的轮廓的某些内容可能会干扰系列之间的差异很小吗? 态
I'm trying to plot an annual time series in bar-chart format.
I have 7 sets of yearly data, and they are in a set order (so, the same dataset has the lowest value each year, and the same dataset is highest value each year)
I can plot it as a line graph and all looks well...
But when I plot it as a stacked bar graph, the draw order is meaning some bars and not visible. I don't for the life of me know how to override this. I've tried factor and levels and can't crack it.
If I plot as position="dodge" I can see that the earlier few in the group are smaller, so they are the ones that are 'behind' (i.e. invisible) when I use position="identity".
Is there a way of sorting large to small when plotting position="identity" ?
This code creates a similar dataframe to the first few lines of mine:
temp2 <- as.data.frame(matrix(1:70, 5, 14))
colnames(temp2) <- c("FY", "B", "L1", "L2", "L3", "L4", "M1", "M2", "M3", "M4",
"H1", "H2", "H3", "H4")
temp2$FY <- 1976:1980
temp2$B <- c(800, 935, 650, 850, 650)
temp2$L1 <- temp2$B * 1.0149
temp2$L2 <- temp2$B * 1.0161
temp2$L3 <- temp2$B * 1.0191
temp2$L4 <- temp2$B * 1.0269
temp2$H1 <- temp2$B * 0.9323
temp2$H2 <- temp2$B * 0.9269
temp2$H3 <- temp2$B * 0.9135
temp2$H4 <- temp2$B * 0.8787
temp2$M1 <- temp2$B * 0.9867
temp2$M2 <- temp2$B * 0.9856
temp2$M3 <- temp2$B * 0.9827
temp2$M4 <- temp2$B * 0.9754
temp2 <- gather(temp2, Type, Rain,
B:H4, factor_key=TRUE)
temp2 <- ddply(temp2, .(FY, Type), summarise,
Rain = sum(Rain, na.rm=TRUE))
Then if i do:
ggplot(temp2, aes(x=FY, y=Rain, colour=Type)) + geom_line()
... I get a normal line graph where you can see all the series.
But, if i do:
ggplot(temp2, aes(x=FY, y=Rain, fill=Type)) + geom_col(position="identity")
... then some of the bars are hidden behind the other bars.
ggplot(temp2, aes(x=FY, y=Rain, fill=Type)) + geom_col(position="dodge")
... shows that the 2nd 3rd 4th and 5th bars are routinely higher than the first, but the last 8 are lower. I need to somehow reorder my data by sorting it by the value in "Type" to this predefined order I have worked out from a different dataframe.
Hope that is clear.
Edit:
(this is using my full dataset but hopefully you get the gist)
position="dodge"
Its clear that there are 7 series per "bundle".
But in position="identity" only 5 show up. Something about the outline of each graph maybe interfering with the very small difference between series?
position="identity"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
forcats :: fct_reorder()
通过lain
订购值。这仅在type
值中的相对顺序中的相对顺序相同。最好在以下躲闪图中看到。在2022-05-09创建的 reprex软件包(v2.0.1)
You can use
forcats::fct_reorder()
to order values byRain
. This only works whereType
values are in the same relative order inYear
. This can best be seen in the following dodged graph.Created on 2022-05-09 by the reprex package (v2.0.1)