Home > front end >  Remove select labels from plot ggplot2
Remove select labels from plot ggplot2

Time:11-05

This is going to sound bizarre but I need to remove the label from two different bars in a bar char that I am making. I have read a few posts that talk about ways to filter out values, for example enter image description here

Desired plot:

enter image description here

CodePudding user response:

Why not just send a filtered data frame to the data argument of geom_text?

ggplot()  
  geom_bar(data=data.bw, aes(x = num, y = bar),
           position = position_dodge(),stat = 'identity', width = 0.75,fill = "#D0D3D4", alpha=0.5)   
  geom_text(data=data.bw[!data.bw$num2 %in% c(130, 1),], 
            aes(x= num, y = bar,label = num2), vjust = -0.2, size=4.5)  
  scale_y_continuous(
    breaks = seq(0, 1), limits = c(0, 1),
    expand = expansion(mult = c(0, 0)),
  )  
  scale_x_continuous(
    expand = expansion(mult = c(0, 0)),
  )  
  scale_fill_manual(values = alpha(c("red"), .3))  
  ylab("Probability")  
  xlab("Number")  
  theme_bw()  
  guides(color = guide_legend(override.aes = list(shape = c(9,8))))  
  theme(
    panel.border = element_rect(colour = "black", fill=NA, size=1),
    plot.background = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    axis.text.x.top = element_blank(),
    axis.text.y.right = element_blank(),
    axis.text.x = element_text(color = "grey20", size = 12, hjust = .5, vjust = .5, face = "plain"),
    axis.text.y = element_text(color = "grey20", size = 12, hjust = .5, vjust = 0.2, face = "plain"),  
    axis.title.x = element_text(color = "grey20", size = 14, hjust = .5, vjust = 0, face = "plain"),
    axis.title.y = element_text(color = "grey20", size = 16, hjust = .5, vjust = .5, face = "plain"),
    plot.margin = margin(20, 3, 10, 3),
    legend.title = element_blank(),
    legend.text=element_text(size=12)
  )

enter image description here

  • Related