3

These are the data:

structure(list(Group.1 = c((name list)
), Group.2 = structure(c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 3L, 3L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 5L, 5L, 5L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Radio", "Video", "Engineering", 
"800Mhz", "PSSRP", "Other"), class = "factor"), x = c(93.5, 208.75, 
214, 48, 66.33, 71.5, 19.5, 64.75, 17, 39, 30.75, 96.75, 30, 
19, 32.5, 12.75, 47.25, 14, 22.25, 12, 3, 128.5, 9.5, 303.2, 
290.35, 364.05, 333.25, 11.75, 553.25, 423, 6, 496)), .Names = c("Group.1", 
"Group.2", "x"), row.names = c(NA, -32L), class = "data.frame")

running this plot:

ggplot(data = HrSums, aes(x = Group.1, y = x, fill = Group.2)) +
  geom_bar(stat = "sum", position = position_stack(reverse = TRUE)) + 
  coord_flip() + 
  labs(title = "Hours Billed, by Technician and Shop", y = "Hours Billed", 
       x = "Technician", fill = "Shop")

I get this bar chart:

What is the "n" box, and how do I remove it (only) from the legend?

2 Answers2

3

if you include the following then you'll only see the aesthetics you're expecting:

show.legend = c(
    "x" = TRUE,
    "y" = TRUE,
    "alpha" = FALSE,
    "color" = FALSE,
    "fill" = TRUE,
    "linetype" = FALSE, 
    "size" = FALSE, 
    "weight" = FALSE
)

See show.legend argument on ?geom_bar:

show.legend logical. Should this layer be included in the legends? NA, the default, includes if any aesthetics are mapped. FALSE never includes, and TRUE always includes. It can also be a named logical vector to finely select the aesthetics to display.

George
  • 31
  • 1
  • 1
    This is a useful answer. Also to note that `size = FALSE` is the relevant one for this particular example, as a comment on the question notes. – qdread Feb 20 '21 at 01:25
2

I believe the n box is because geom_bar expects to count the number of times each combination of Group.1 and Group.2 occurs, but instead you're giving a y value in your aes. geom_bar can use a different stat instead of counting, but if you want the sums of values, it expects a weight aesthetic. Here are two ways to do this, one using weight = x in geom_bar, and one that uses dplyr functions to calculate sums beforehand, then supplies this to y.

library(tidyverse)


ggplot(df, aes(x = Group.1, fill = Group.2)) +
  geom_bar(aes(weight = x), position = position_stack(reverse = T)) +
  coord_flip()




df_sums <- df %>%
  group_by(Group.1, Group.2) %>%
  summarise(x = sum(x))

ggplot(df_sums, aes(x = Group.1, y = x, fill = Group.2)) +
  geom_col(position = position_stack(reverse = T)) +
  coord_flip()
camille
  • 16,432
  • 18
  • 38
  • 60
  • Thank you! I used the first one. I still have trouble (as a generality) with conflicts between the aesthetics and the parameters within the geoms. But this is brilliant, exactly the answer I needed. – Mark Bunster May 16 '18 at 21:32