Melih Aran
Melih Aran

Reputation: 23

How can I add specific numbers to the legend barplot in ggplot2?

I tried to draw barplot in ggplot2, and tried to add specific numbers to the legend. It worked, but this time I lost the my specific colors (mycolors2), what do you recommend me? You can see my expected output (in different color) below.

  df <- data.frame("Var"=c("A", "B", "C", "D"),
                 "IQ"=c(28, 32, 25, 33),
             "Se"=c(2.1,3.2,4.1,3.5))


   mycolors2<-c(paste0("royalblue", c(4:3)), c("steelblue4", "steelblue3"))

p<-ggplot(df, aes(x=Var, y=IQ, fill=Var))+
  geom_bar(stat="identity", color="black")+
  scale_fill_manual(values=mycolors2)+
  geom_errorbar(aes(ymin=IQ-Se, ymax=IQ+Se), width=.2,
                position=position_dodge(.9))


p

p + coord_cartesian(ylim=c(0, 100))+ theme_classic()+
  xlab("Variant Groups")+
  ylab("Comparison of IQ Scores for Autism Female Probands with \n ROH Blocks Carrying Variants or No-Variant")+
  theme( axis.title.y = element_text(size = 16,   margin = margin(t = 0, r =0, b = 0, l = 4,   unit = "mm")), 
         axis.title.x = element_text(size = 16,    margin = margin(t = 0, r =0, b = 0, l = 0,   unit = "mm")), 
         panel.background = element_rect(fill = "white")) +
  scale_fill_discrete(labels = c("A(n=128)", "B(n=148)","C(n=223)","D(n=154)"))
  theme_classic()

My expected output in a different color is

enter image description here

Upvotes: 0

Views: 42

Answers (1)

Duck
Duck

Reputation: 39613

Try this approach. Instead of adding a new scale_fill_*() argument that will not work, set directly the labels into your first fill option. Here the code:

library(ggplot2)
#Data
df <- data.frame("Var"=c("A", "B", "C", "D"),
                 "IQ"=c(28, 32, 25, 33),
             "Se"=c(2.1,3.2,4.1,3.5))
#Colors
mycolors2<-c(paste0("royalblue", c(4:3)), c("steelblue4", "steelblue3"))

The code for plots:

#Code
ggplot(df, aes(x=Var, y=IQ, fill=Var))+
  geom_bar(stat="identity", color="black")+
  scale_fill_manual(values=mycolors2,
                    labels = c("A"="A(n=128)","B"="B(n=148)",
                               "C"="C(n=223)","D"="D(n=154)"))+
  geom_errorbar(aes(ymin=IQ-Se, ymax=IQ+Se), width=.2,
                position=position_dodge(.9))

Output:

enter image description here

Using your whole code, you would have:

#Code 2
ggplot(df, aes(x=Var, y=IQ, fill=Var))+
  geom_bar(stat="identity", color="black")+
  scale_fill_manual(values=mycolors2,
                    labels = c("A"="A(n=128)","B"="B(n=148)",
                               "C"="C(n=223)","D"="D(n=154)"))+
  geom_errorbar(aes(ymin=IQ-Se, ymax=IQ+Se), width=.2,
                position=position_dodge(.9))+
  coord_cartesian(ylim=c(0, 100))+ theme_classic()+
  xlab("Variant Groups")+
  ylab("Comparison of IQ Scores for Autism Female Probands with \n ROH Blocks Carrying Variants or No-Variant")+
  theme( axis.title.y = element_text(size = 16,   margin = margin(t = 0, r =0, b = 0, l = 4,   unit = "mm")), 
         axis.title.x = element_text(size = 16,    margin = margin(t = 0, r =0, b = 0, l = 0,   unit = "mm")), 
         panel.background = element_rect(fill = "white"))+
theme_classic()

Output:

enter image description here

Upvotes: 1

Related Questions