neuron
neuron

Reputation: 2049

Remove select labels from plot ggplot2

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 here. However, the two values that I need to remove are much different from one another so I don't think this technique will work. Let me give you the code that I am using, an example dataset, and some pictures of what exactly I am looking for. What I want to do is remove 130 and 1 from the plot. Both numbers are circled in red in the first picture.

Note: the values may look small in the example dataset however in geotext() the labels get multiplied by 310

Example code:

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, aes(x= num, y = bar,label = bar*310), vjust = -0.2, size=4.5) +
  scale_y_continuous(
    #minor_breaks = seq(0, 1, by = 50),
    breaks = seq(0, 1), limits = c(0, 1),
    expand = expansion(mult = c(0, 0)),
    #guide = "axis_minor" 
  ) +
  scale_x_continuous(
    #minor_breaks = seq(0, 25, by = 5),
    #breaks = seq(0, 25, by = 2), limits = c(0, 25),
    expand = expansion(mult = c(0, 0)),
    #guide = "axis_minor"
  ) +
  guides(x.sec = "axis_minor", y.sec = "axis_minor") +
  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)
  )

Example data:

structure(list(num = c(1, 1.75, 2.5, 4, 4.75, 5.5, 6.25, 7, 10, 
13.75, 15.25, 16, 17.5, 18.25, 19, 19.75, 20.5, 22, 22.75, 23.5
), bar = c(0.948387096774194, 0.419354838709677, 0.145161290322581, 
0.0419354838709677, 0.0161290322580645, 0.00645161290322581, 
0.0032258064516129, 0.0032258064516129, 0.0032258064516129, 0.0032258064516129, 
0.00967741935483871, 0.0193548387096774, 0.0354838709677419, 
0.067741935483871, 0.112903225806452, 0.125806451612903, 0.148387096774194, 
0.132258064516129, 0.0516129032258065, 0.00967741935483871), 
    num2 = c(294L, 130L, 45L, 13L, 5L, 2L, 1L, 1L, 1L, 1L, 3L, 
    6L, 11L, 21L, 35L, 39L, 46L, 41L, 16L, 3L)), row.names = c(NA, 
-20L), class = "data.frame")

Current plot with values that I would like to remove circled in red:

enter image description here

Desired plot:

enter image description here

Upvotes: 0

Views: 620

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 173793

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

Upvotes: 1

Related Questions