简体   繁体   English

geom_bar of count plot:为没有值的列添加 x 轴 label 并自定义剩余条的顺序

[英]geom_bar of count plot: add x axis label for column with no value and customize order of remaining bars

I need to add a label for the count of empty value in the below data:我需要为以下数据中的空值计数添加 label:

library(ggplot2)
myData <- data.frame(
  x = c("A", "A", "B", "B", "B", "C", "C", "C", "", "", "", ""),
  y = c(1, 4, 1, 5, 1, 5, 6, 7, 3, 6, 6, 3)
)

ggplot(myData, aes(x=x)) + 
  geom_bar(width = 0.5)+
  geom_text(stat='count', aes(label = ..count..), vjust = -1)

My plot is currently like this:我的 plot 目前是这样的: 在此处输入图像描述

How do I add a customized label for the first column?如何为第一列添加定制的 label? Say "Not Available"说“不可用”

Second question , I need to customize the order of the remaining bars, so the first bar is always the one with empty value, but subsequently, I want bars in this order B, C, A (not descending or ascending order).第二个问题,我需要自定义剩余条的顺序,所以第一个条总是具有空值的条,但随后,我想要按B, C, A顺序排列的条(不是降序或升序)。 Other StackOverflow answers seem to address the re-ordering of bars based on counts.其他 StackOverflow 答案似乎解决了基于计数的条形重新排序问题。

you can do this by using scale_x_discrete, limits changes the order, label will decide what text you want to ave你可以通过使用 scale_x_discrete 来做到这一点,限制改变顺序,label 将决定你想要什么文本

p = ggplot(myData, aes(x=x)) + 
  geom_bar(width = 0.5)+
  geom_text(stat='count', aes(label = ..count..), vjust = -1)

p+scale_x_discrete(limits=c("","B","C","A"),
label=c("Not Available","B","C","A"))

在此处输入图像描述

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

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