简体   繁体   English

ggplot2,geom_text和顺序

[英]ggplot2, geom_text and order

The issue I'm facing is very simple. 我面临的问题非常简单。

I'm plotting 3 types of data in a bar graph with the percentages on the Y axis, and coloring each group using another column from my dataframe. 我将在Y轴上的百分比的条形图中绘制3种类型的数据,并使用数据框中的另一列为每个组着色。

The graph looks like this : 该图如下所示:

图形

generated with 产生于

ggplot(cleaned_data, aes(x = Engine, fill = Errorcat)) +
  geom_bar(aes(y = (..count..)/sum(..count..))) +
  scale_y_continuous(labels = percent) + 
  scale_fill_discrete("Events", breaks = c("Ok", "Waiting", "Error")) +
  labs(y = "")

The data have been factored like this : cleaned_data$Errorcat <- factor(cleaned_data$Errorcat, levels = c("Error", "Ok", "Waiting")) 数据已经像这样分解:cleaned_data $ Errorcat <-factor(cleaned_data $ Errorcat,level = c(“ Error”,“ Ok”,“ Waiting”))

I just want to display the corresponding percentages in the middle of each bin. 我只想在每个垃圾箱的中间显示相应的百分比。 So I went back in and just added a geom_text call, like this : 因此,我返回并添加了一个geom_text调用,如下所示:

ggplot(cleaned_data, aes(x = Engine, fill = Errorcat)) +
  geom_bar(aes(y = (..count..)/sum(..count..))) +
  geom_text(aes(y = ((..count..)/sum(..count..)) - 0.5*    (((..count..)/sum(..count..))) ,
                label = scales::percent((..count..)/sum(..count..))), stat = "count", vjust = -0.25, check_overlap = TRUE) +
  scale_y_continuous(labels = percent) + 
  scale_fill_discrete("Events", breaks = c("Ok", "Waiting", "Error")) +
  labs(y = "")

It produces this : 它产生这个:

图2

The figures are right, only their respectives Y coordinates are not in the correct order. 这些图是正确的,只是它们各自的Y坐标顺序不正确。 As you can see, the 45% for instance is the "top" figure, while it should correspond to the blue, not the green. 如您所见,例如45%是“顶部”数字,而它应该对应于蓝色而不是绿色。 from bottom to top, I think geom_text is displaying red/blue/green while I want just like the bars red/green/blue. 从下到上,我认为geom_text显示的是红色/蓝色/绿色,而我想要的颜色就像红色/绿色/蓝色的条一样。

I can't seem to understand how I can do that nor why it's not doing it right away. 我似乎无法理解我该怎么做,也无法理解为什么它没有立即做到。 The end goal is to have specific color assigned to the Errorcat. 最终目标是为Errorcat分配特定的颜色。 I want Ok to be green, waiting to be blue and Error to be red. 我希望确定为绿色,等待为蓝色,错误为红色。 And the legend I want Ok on top then waiting then Error. 和传说,我想让Ok在最上面,然后等待,然后出现Error。

If anyone would be kind enough to explain what I am missing... Thank you ! 如果有人愿意解释我所缺少的内容,谢谢!

Summary of the comments above, the following code is producing all the labels at the right spot, centered in each bin : 上面的评论摘要,以下代码在每个位置居中的正确位置生成所有标签:

ggplot(cleaned_data, aes(x = Engine, fill = Errorcat)) +
  geom_bar(aes(y = (..count..)/sum(..count..))) +
  geom_text(aes(y = (..count..)/sum(..count..), label = scales::percent((..count..)/sum(..count..))),
            stat = "count", check_overlap = FALSE, position = position_stack(vjust = 0.5)) +
  scale_y_continuous(labels = percent) + 
  scale_fill_discrete("Events", breaks = c("Ok", "Waiting", "Error")) +
  labs(y = "")

Thanks everyone. 感谢大家。

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

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