0

I programmed this barplot with ggplot2, and it worked just fine. But it is al little bit too wide, so I would like to delete the fill legend and add it to the x-axis like in this image. How should I change my code?

The plot should look like this

Here is the data:

> dput(plot.Subject.means)
structure(list(Mode = structure(c(1L, 1L, 2L, 2L), .Label = c("silent", 
"aloud"), class = "factor"), PlausibleFit = structure(c(1L, 2L, 
1L, 2L), .Label = c("plausible", "implausible"), class = "factor"), 
Stimulus = structure(c(2L, 2L, 2L, 2L), .Label = c("fist sent", 
"second sent"), class = "factor"), Mean = c(241.743191964286, 
258.42306547619, 304.836607142857, 333.330133928571), SE = 
c(10.7086547532698, 
12.5941104184202, 14.3157931004567, 17.5915009397571)), class = 
"data.frame", row.names = c(NA, 
-4L))

And the code for the figure

library(ggplot2)
Fig1.GD = ggplot(plot.Subject.means, 
                 aes(x = Mode, y = Mean, fill = PlausibleFit)) +
  geom_bar(stat = 'identity', position = 'dodge') +
  geom_errorbar(aes(ymin = Mean - SE, ymax = Mean + SE), 
                width = .2, position = position_dodge(.9)) +
  coord_cartesian(ylim = c(100, 400)) +
  labs(title = "Gaze Duration Adults") +
  xlab("Reading Mode") +   ylab("Gaze Duration in ms") + 
  guides(fill = guide_legend(title = NULL))
Fig1.GD
teunbrand
  • 33,645
  • 4
  • 37
  • 63
Laura S
  • 15
  • 4
  • Can you please provide a working MWE ? – JohnBee Aug 13 '19 at 13:57
  • 3
    Per `r` tag (hover to see): Please provide [minimal and reproducible example(s)](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5965451) along with the desired output. Use `dput()` for data and specify all non-base packages with `library()` calls. – Parfait Aug 13 '19 at 14:00

1 Answers1

0

I don't know about a way to produce your plot exactly, but you could either try to move the legend:

a <- structure(list(Mode = structure(c(1L, 1L, 2L, 2L), .Label = c("silent", 
                                                                   "aloud"), class = "factor"), PlausibleFit = structure(c(1L, 2L, 
                                                                                                                           1L, 2L), .Label = c("plausible", "implausible"), class = "factor"), 
                    Stimulus = structure(c(2L, 2L, 2L, 2L), .Label = c("fist sent", 
                                                                       "second sent"), class = "factor"), Mean = c(241.743191964286, 
                                                                                                                   258.42306547619, 304.836607142857, 333.330133928571), SE = 
                      c(10.7086547532698, 
                        12.5941104184202, 14.3157931004567, 17.5915009397571)), class = 
                 "data.frame", row.names = c(NA, 
                                             -4L))

library(ggplot2)
#> Registered S3 methods overwritten by 'ggplot2':
#>   method         from 
#>   [.quosures     rlang
#>   c.quosures     rlang
#>   print.quosures rlang
Fig1.GD = ggplot(a, aes(x=Mode, y=Mean, 
                        fill=PlausibleFit))+
  geom_bar(stat='identity', position='dodge')+
  geom_errorbar(aes(ymin=Mean-SE, ymax=Mean+SE), 
                width=.2,position=position_dodge(.9))+
  coord_cartesian(ylim= c(100,400)) +
  labs(title="Gaze Duration Adults")+ 
  ylab("Gaze Duration in ms")+
  xlab("Reading Mode")+
  guides(fill=guide_legend(title=NULL)) +
  theme(legend.position = 'bottom')
Fig1.GD


Or add annotations to the plot:



Fig1.GD = ggplot(a, aes(x=Mode, y=Mean, 
                        fill=PlausibleFit))+
  geom_bar(stat='identity', position='dodge')+
  geom_errorbar(aes(ymin=Mean-SE, ymax=Mean+SE), 
                width=.2,position=position_dodge(.9))+
  coord_cartesian(ylim= c(100,400)) +
  labs(title="Gaze Duration Adults")+ 
  ylab("Gaze Duration in ms")+
  guides(fill=guide_legend(title=NULL)) +
  theme(legend.position = 'none')+
  annotate(geom = 'text', x = c(.78,1.22,1.78,2.22),label = a$PlausibleFit, y = 100, color = 'white')
Fig1.GD

Max Teflon
  • 1,760
  • 10
  • 16