graph-ggplot-barplot

Hello everybody,

Please, Help me
I want to use ggplot for this graph, but I get "NULL "

#library(ggplot2)
tot<-c(82/107,25/107)
ptot<-round(tot*100)
ptot

bp=barplot(ptot,names.arg=c('vrai','faux'),col=c('light blue','magenta'),main='Questionnaire 1: Tri global',col.main=1,xlab='Type de réponses',ylab='% de réponses')
plot1 = bp+theme_get()+
annotate("text", x = -1.9, y = 75, label = "get()" , col="orange" , size=4)+
text(bp, 0, ptot, cex=1, offset=5, pos = 3)
plot1

Is the result of this code close enough?

library(ggplot2)
tot<-c(82/107,25/107)
ptot<-round(tot*100)
ptot

DAT <- data.frame(Type = c('vrai','faux'), Perc = ptot)
DAT
ggplot(DAT, aes(Type, Perc, fill = Type)) + geom_col(color = "black") +
  labs(x = 'Type de réponses', y = '% de réponses', title = 'Questionnaire 1: Tri global') +
  scale_fill_manual(values = c(vrai = "light blue", faux = "magenta"), guide = NULL) +
  geom_text(aes(y = 60, label = Perc)) + theme_bw() 
1 Like

With additional styling, we can get even closer if you'd like:

library(ggplot2)
tot <- c(82/107, 25/107)
ptot <- round(tot * 100)

# we can use a factor with levels to set the order
# note: if we omit levels, defaults to alphabetical order.
types <- factor(c("vrai", "faux"),
                levels = c("vrai", "faux"))

df <- data.frame(type = types, 
                 perc = ptot)

ggplot(data = df) +
  geom_col(aes(x = type,
               y = perc,
               fill = type),
           color = "black") +
  geom_text(aes(x = type,
                # perc / 2 places label in the middle
                y = perc / 2,
                label = perc)) +
  scale_y_continuous("% de réponses",
                     # expand to remove extra space
                     expand = c(0,0),
                     # set the limits
                     limits = c(0, 80),
                     # set breaks for y-axis labels
                     breaks = seq(0, 80, 10)) +
  scale_x_discrete("Type de réponses") +
  scale_fill_manual(breaks = c("vrai","faux"),
                    values = c("lightblue", "magenta")) +
  ggtitle("Questionnaire 1: Tri global") +
  theme_bw() +
  theme(panel.grid = element_blank(), # remove gray grid
        panel.border = element_blank(), # remove border
        axis.line.y = element_line()) # add back the y-axis
1 Like

Thank you :clap: :clap: :clap: :clap: :blush: :blush: :blush:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.