R计数条形图:具有较大计数列和零计数或少计数列的偏斜数据

以下代码绘制了以对数刻度的y轴表示的x元素计数的条形图。

library(ggplot2)  
library(scales)

myData <- data.frame(
  x = c(rep(1,22500),rep(2,6000),rep(3,8000),rep(4,5000),rep(11,86),rep(16,15),rep(31,1),rep(32,rep(47,1))
)

ggplot(myData,aes(x=x)) + 
  geom_bar(width = 0.5)+
  geom_text(stat='count',aes(label = ..count..),vjust = -1,size=3)+
  scale_y_log10(breaks = trans_breaks("log10",function(x) 10^x),labels = trans_format("log10",math_format(10^.x)))+
  scale_x_continuous(breaks=(seq(1:47)))

下面是情节:

R计数条形图:具有较大计数列和零计数或少计数列的偏斜数据

我的问题是:

  1. 如何删除计数为零的那些列的x轴刻度线/标签?

  2. 如何更好地显示列313247的值? (计数为1的人)

  3. 如何仅标记最高列的计数? (在这种情况下,列22500的{​​{1}})

stkelvinlee 回答:R计数条形图:具有较大计数列和零计数或少计数列的偏斜数据

一个选项是为您添加边框颜色,这将有助于突出显示在图形的这些部分中至少有一些内容:

library(tidyverse)

df <- 
  myData %>% 
  group_by(x) %>% 
  count()

df %>% 
  ggplot(aes(x = x,y = n)) +
  geom_col(color = "cyan4",fill = "cyan3") +
  geom_text(data = . %>% filter(x == 1),aes(label = n,y = n + 10000)) +
  scale_y_log10()

enter image description here

本文链接:https://www.f2er.com/3120995.html

大家都在问