0

How do I re-plot the below geom_col where Group 1 and Group 2 have one thick bar each, rather than multiple thin bars that all show exactly the same result?

This is the data I am plotting:

structure(list(mean_group = c(0.263699696150573, 0.255161746412958, 
0.263699696150573, 0.255161746412958, 0.263699696150573, 0.255161746412958, 
0.263699696150573, 0.255161746412958, 0.263699696150573, 0.255161746412958, 
0.263699696150573, 0.255161746412958, 0.255161746412958, 0.263699696150573, 
0.263699696150573, 0.263699696150573, 0.263699696150573), mean_sample = c(0.304449388830337, 
0.304449388830337, 0.270879368594138, 0.270879368594138, 0.233636248731973, 
0.233636248731973, 0.234536475984982, 0.234536475984982, 0.250903685479549, 
0.250903685479549, 0.263039371741738, 0.263039371741738, 0.227068796332047, 
0.227068796332047, 0.280737487680756, 0.272906770129839, 0.477592456931195
), Group = c("2", "1", "2", "1", "2", "1", "2", "1", "2", "1", 
"2", "1", "1", "2", "2", "2", "2"), sample = c(1, 1, 2, 2, 3, 
3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 9, 10)), class = c("tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -17L))

This is my current code for plotting:

p = ggplot(exp) +
  geom_col(aes(x=Group, y=mean_group), position = position_dodge2(width = 0, preserve = "single")) + 
  geom_dotplot(binaxis='y', stackdir='center', stackgroups = TRUE, binpositions="all", dotsize=0.8,
               colour="Black", fill="Black", aes(x=Group, y = mean_sample)) 
p

enter image description here

MeganCole
  • 39
  • 1
  • 6

1 Answers1

0

ggplot is very good at plotting the data you give it. If you give it 7 identical values, it will plot 7 identical bars. Here I use unique() on the subset of data used for the columns to de-duplicate the data and thus de-duplicate the bars.

ggplot(exp, aes(x = Group)) +
  geom_col(
    data = unique(exp[c("Group", "mean_group")]),
    aes(y = mean_group),
    fill = "gray60"
  ) + 
  geom_dotplot(
    aes(y = mean_sample),
    binaxis='y', stackdir='center', stackgroups = TRUE, 
    binpositions="all", dotsize=0.8,
    colour="Black", fill="Black"
    )

enter image description here

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294