0

I'm trying to produce bar graph with ggplot2 in R. Graph is produced perfectly so far but when I try adding sample size, it doesn't work. Here is my code:

library(ggplot2)
library(ggthemes)
library(dplyr)
library(ggsignif)
Demo1 <- demo.csv("demo.csv", header = T, sep = ",")
Demo1
mean <- aggregate(A ~ B, Demo1, mean)
sd <- aggregate(A ~ B, Demo1, sd)
samplesize <- Demo1 %>% group_by(B) %>% summarise(count = n())

X <- ggplot(Demo1, aes(y = B, x = A))
X <- X + stat_summary(fun = "mean", 
                  geom = "bar") 
X <- X + stat_summary(fun = "mean", 
                  geom = "errorbar",
                  fun.min = function(x)mean(x)-sd(x), 
                  fun.max = function(x)mean(x) + sd(x), 
                  width = 0.1,
                  size = 0.5)
X <- X + theme_classic() 

Then, tried to paste samplesize which I calculated in the beginning by following code.

X <- X + geom_text(aes(x = A, y = B, label = samplesize))

Is there any good way to do?

Thanks in advance.

Ritz
  • 3
  • 1
  • 4
  • 3
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick May 18 '21 at 05:10
  • Your question is a bit vague. Can you be more specific? – Harley May 18 '21 at 05:32
  • @MrFlick Thanks for your advice. I added little more code. – Ritz May 18 '21 at 06:13
  • @user3121518 Thanks for your advice. I added little more code. – Ritz May 18 '21 at 06:13

1 Answers1

4

Are you looking for?

library(dplyr)
library(ggplot2)

mtcars %>%
  count(cyl) %>%
  ggplot() + aes(cyl, n, label = n) + 
  geom_col() + geom_text(vjust = -0.5)

enter image description here


For your data you can try to add -

X + geom_text(data = samplesize, aes(label = count), vjust = -5.5, hjust = -0.5)

Adjust hjust and vjust value according to your choice.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thank you so much for your quick response! This code worked but how can I add sample size text to the bar graph I produced by the code I shared? Thanks – Ritz May 18 '21 at 06:11
  • 1
    The code is not useful for us because we don't have data (`Demo1`) to run it. Please edit your post to include output of `dput(Demo1)` or if the data is huge use `dput(head(Demo1, 20))` – Ronak Shah May 18 '21 at 06:15
  • Thanks. I tried to edit my post but eventhough I use proper formatting, error message about code formatting appears and cannot post anymore. Here is the data I used `> dput(read.table(pipe("pbpaste"))) structure(list(V1 = c("A", "21days", "21days", "21days", "21days", "3days", "3days", "3days", "3days"), V2 = c("B", "50", "100", "150", "200", "30", "60", "90", "120")), class = "data.frame", row.names = c(NA, -9L))` – Ritz May 18 '21 at 13:53
  • @Ritz Can you try `X + geom_text(data = samplesize, aes(label = count), vjust = -5.5, hjust = -0.5)`. I have also updated the answer. – Ronak Shah May 18 '21 at 14:02
  • Thank you so much again. I tried and this error came out. "Error in FUN(X[[i]], …) : object 'carrot' not found" I googled and tried some solutions but nothing worked. – Ritz May 19 '21 at 00:48
  • It worked!! I added x and y as following `X <- X + geom_text(data=samplesize, aes(x=days, y=count, label=count),vjust=0)` Thank you soo much for your help! This is exactly what I wanted. – Ritz May 19 '21 at 06:21