Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8643719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:07:12+00:00 2026-06-12T12:07:12+00:00

I have a data.frame : df <- structure(list(sample = structure(c(1L, 1L, 1L, 1L, 2L,

  • 0

I have a data.frame :

df <- structure(list(sample = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("A", 
"B", "C"), class = "factor"), replicate = c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L), 
    treatment = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 
    2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("V0", 
    "V2", "V4", "V6"), class = "factor"), mean = c(72.5, 66.4, 
    60.3, 56.3, 90.7, 86.9, 68.8, 64.6, 73.3, 69.8, 62.3, 68.7, 
    77.1, 73.6, 65.9, 65.3, 75.7, 73.8, 65.9, 66.8), sd = c(5.4, 
    6.1, 3.5, 4.7, 1.2, 4.9, 8.4, 6.1, 7.3, 3.5, 5.9, 2.2, 3.2, 
    3, 3.1, 4.2, 4.2, 2.4, 2.2, 2.6)), .Names = c("sample", "replicate", 
"treatment", "mean", "sd"), class = "data.frame", row.names = c(NA, 
-20L))

For some samples I have replicates while for others I don’t :

> head(df, 15)
   sample replicate treatment mean  sd
1       A         1        V0 72.5 5.4
2       A         1        V2 66.4 6.1
3       A         1        V4 60.3 3.5
4       A         1        V6 56.3 4.7
5       B         1        V0 90.7 1.2
6       B         1        V2 86.9 4.9
7       B         1        V4 68.8 8.4
8       B         1        V6 64.6 6.1
9       C         1        V0 73.3 7.3
10      C         1        V2 69.8 3.5
11      C         1        V4 62.3 5.9
12      C         1        V6 68.7 2.2
13      C         2        V0 77.1 3.2
14      C         2        V2 73.6 3.0
15      C         2        V4 65.9 3.1

I would like to plot means in a faceted bar plot for each sample value. I would like to get grouped bars when replicates > 1. Is it possible with ggplot2 ?

What I get so far :

g <- ggplot(df, aes(x=treatment, y=mean, fill=sample)) 
g <- g + geom_bar(position="dodge")
g <- g + geom_errorbar(aes(ymax=mean+sd, ymin=mean-sd), position="dodge")
g <- g +  facet_grid(. ~ sample)

g

bar plot

EDIT

Following the suggestion of @Tyler Rinker, I get :

g <- ggplot(df, aes(x=treatment, y=mean, fill=replicate))
g <- g + geom_bar(position="dodge", stat="identity")
g <- g + geom_errorbar(aes(ymax=mean+sd, ymin=mean-sd), position="dodge", width=0.25)
g <- g +  facet_grid(. ~ sample)

test2

However, I would like to get the bars for the ‘C’ sample replicates displayed side-by-side.

EDIT2 Very close to …

Simply by adding group = replicate :

g <- ggplot(df, aes(x=treatment, y=mean, group=replicate, fill=replicate))
g <- g + geom_bar(position="dodge", stat="identity")
g <- g + geom_errorbar(aes(ymax=mean+sd, ymin=mean-sd), position="dodge", width=0.25)
g <- g +  facet_grid(. ~ sample)

test3

However there is a remaining problem with the position of the error bars. I guess I have to play with dodge ? But I don’t know how …

EDIT3 Eventually… thanks to @joran

dodge <- position_dodge(width = 0.9)

I checked that other values than 0.9 did not lead to a correct positioning of the error bars

g <- ggplot(df, aes(x=treatment, y=mean, group=replicate, fill=replicate))
g <- g + geom_bar(position="dodge", stat="identity") 
g <- g + geom_errorbar(aes(ymax=mean+sd, ymin=mean-sd), position=dodge, width=0.25, color="black")
g <- g +  facet_grid(. ~ sample)

test4

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:07:13+00:00Added an answer on June 12, 2026 at 12:07 pm

    Try this and experiment a bit:

    dodge <- position_dodge(width = 0.9)
    
    g <- ggplot(df, aes(x=treatment, y=mean, group=replicate, fill=replicate))
    g <- g + geom_bar(position=dodge, stat="identity")
    g <- g + geom_errorbar(aes(ymax=mean+sd, ymin=mean-sd), position = dodge,width = 0.25)
    g <- g +  facet_grid(. ~ sample)
    g
    

    I think the different dodging in each panel was confusing the width setting. I’d have to read up a bit on the details of dodging (and there are several people around these parts who may be able to weigh in more easily than myself…) but I think that when you set the dodging “locally” in each geom, ggplot is deciding things about widths based upon information in each layer separately. So the trick here seemed to be to ensure a “global” dodge setting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data.frame as follows: dat <- structure(list(id = 1:4, date = structure(list(sec
I have this nested data frame test <- structure(list(id = c(13, 27), seq =
I have a fairly simple data.frame dput(x), below: x <- structure(list(variable = c(a, b,
I have a melted data.frame, dput(x), below: ## dput(x) x <- structure(list(variable = structure(c(1L,
I have data frame with some numerical variables and some categorical factor variables. The
I have a data frame like below (20,000 rows by 49 cols). Each row
I have a data.frame , called so_data. Columns 13:23 are list s, which hold
I have following data ct<-structure(list(Conc = c(50L, 100L, 150L, 50L, 100L, 150L, 50L, 100L,
I have the following (dummy) data: d <- structure(list(group = structure(c(1L, 1L, 1L, 1L,
i have a time series data like this: x <- structure(list(date = structure(c(1264572000, 1266202800,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.