ggplot barchart

I'm struggling to find a suitable graph to represent the sample of the data below. I would like to see the data in a bar chart or something similar where i can compare both methods side by side. Can anyone help please?

df <- data.frame(
  total.counts.Method01 = c(0L, 33L, 260L, 413L, 468L, 577L, 659L, 520L, 579L,
                            570L),
  total.counts.method02 = c(78L, 78L, 193L, 234L, 247L, 311L, 312L, 320L, 312L,
                            321L)
)

Created on 2020-05-27 by the reprex package (v0.3.0)

Many Thanks

Cay you provide a reproducible dataset? it will make it easier to provide an answer.

What sort of code have you tried?

Perhaps look at something like this:

Thanks! Please see a reprex of the code above. Many Thanks

You probably need to provide more detail on what you actually want.

I have put this together anyway:

library(tidyverse)
df2 <- df %>% 
  mutate(id = row_number()) %>% 
  pivot_longer(-id, names_to = "method", values_to = "values") %>% 
  mutate(method = str_remove(method, "total.counts.")) %>% 
  mutate(id = factor(id))

ggplot(df2, aes(id, values, fill = method)) +
  geom_col(position = "dodge") +
  theme(legend.position = "bottom")

1 Like

Thanks for this. I am trying to compare 4 different methods of testing. Sorry! I know my code only showing two methods. The issue is as the number of rows increase the Y axis's get crowded and make the graph looks hard to understand. It doesn't have to be bar chart can use something else as long as i have away of showing the difference. May be something like a facets. Thanks for your help

Here is a facet_wrap version with geom_line. The id is numeric, but can be removed.

You should be able to get it to the what you are looking for.

library(tidyverse)
df2 <- df %>% 
  mutate(id = row_number()) %>% 
  pivot_longer(-id, names_to = "method", values_to = "values") %>% 
  mutate(method = str_remove(method, "total.counts.")) 

ggplot(df2, aes(id, values, col = method)) +
  geom_line(size = 2) +
  facet_wrap(~method) + 
  theme(legend.position = "none")

1 Like

Many Thanks for your help! The bar chart looks better in terms of showing the differences clearly. Is there away to get facets with a bar chart of each method, and with the Y axis's show as scale but not representing the value of each test. Thanks a lot

1 Like