Trying to make a bar graph with a data set I made and keep getting the same error

I'm doing a study testing if turkey and alcohol affects the level of contentedness and levels of drowsiness of participants. It's a repeated measures design, so the participants get asked their level of contentedness and drowsiness on a 1-7 scale.
We then made the data set and I’ll copy how I did that below.

Drowsy1 <- c(4, 5, 1, 5, 2, 5, 1, 2, 1, 2, 3, 4, 2, 2, 5, 5, 3, 1, 1, 4, 1, 1, 1, 1, 3, 2, 3, 1, 6, 1, 5, 1, 3, 2, 1, 3)
Drowsy2 <- c(3, 4, 2, 6, 3, 5, 2, 3, 3, 2, 3, 4, 3, 3, 6, 4, 6, 6, 4, 7, 4, 7, 3, 7, 6, 6, 1, 2, 1, 1, 6, 5, 7, 1, 7, 5)
Drowsy3 <- c(6, 2, 6, 7, 4, 7, 2, 6, 4, 6, 7, 6, 5, 5, 4, 3, 7, 6, 7, 5, 6, 5, 6, 7, 7, 6, 6, 7, 5, 7, 7, 6, 7, 5, 7, 6)
Content1 <- c(5, 4, 5, 5, 5, 6, 7, 5, 3, 5, 3, 3, 3, 4, 3, 3, 4, 7, 7, 6, 6, 5, 4, 1, 4, 2, 4, 5, 4, 3, 3, 76, 4, 4, 6)
Content1 <- c(5, 4, 5, 5, 5, 6, 7, 5, 3, 5, 3, 3, 3, 4, 3, 3, 4, 7, 7, 6, 6, 5, 4, 1, 4, 2, 4, 5, 4, 3, 3, 7, 6, 4, 4, 6)
content2 <- c(5, 3, 6, 4, 5, 4, 6, 5, 4, 6, 5, 2, 5, 4, 4, 4, 6, 7, 7, 7, 7, 7, 5, 7, 6, 3, 7, 7, 7, 5, 7, 7, 3, 7, 6, 7)
content3 <- c(4, 6, 3, 2, 5, 5, 7, 4, 7, 7, 5, 2, 5, 6, 5, 7, 5, 7, 7, 7, 7, 6, 7, 7, 7, 5, 7, 6, 3, 6, 7, 7, 6, 7, 7, 6)
turkeydata <- data.frame(Drowsy1, Drowsy2, Drowsy3, Content1, content2, content3)
id.num = c(1:nrow(turkeydata))
turkeydata <- cbind(id.num, turkeydata)
colnames(turkeydata) = c("id.num", "drowsy_noalc_noturk", "drowsy_alc_noturk", "drowsy_alc_turk", "content_noalc_noturk", "content_alc_noturk", "content_alc_turk")
print(turkeydata)
install.packages("lsr")
library(lsr)
turkeydata2 <- wideToLong(turkeydata, within = c("alcohol consumption"," turkey consumption")) View(turkeydata2)

Then, I tried to make a graph and it says there’s an unexpected symbol in the code.

ggbarplot[turkeydata2 (turkeydata2, "turkey consumption", "drowsy",
color = "alcohol consumption", palette = c("#0072B2", "#E69F00"),
add = "mean_ci", position = position_dodge(0.9)) +
scale_y_continuous(expand = c(0,0)) +
coord_cartesian(ylim = c(0,7))+
labs(y = "Likert Scale Ratings(1-7)")

Then I tried reformatting the data and renaming it to a format I thought r would like better. I also split up the data set so it would be just one data set looking at turkey and alcohol (the two IVs) and just looking at one DV at a time (in this case drowsiness). I’ll paste that below.

Drowsy1 <- c(4, 5, 1, 5, 2, 5, 1, 2, 1, 2, 3, 4, 2, 2, 5, 5, 3, 1, 1, 4, 1, 1, 1, 1, 3, 2, 3, 1, 6, 1, 5, 1, 3, 2, 1, 3)
Drowsy2 <- c(3, 4, 2, 6, 3, 5, 2, 3, 3, 2, 3, 4, 3, 3, 6, 4, 6, 6, 4, 7, 4, 7, 3, 7, 6, 6, 1, 2, 1, 1, 6, 5, 7, 1, 7, 5)
Drowsy3 <- c(6, 2, 6, 7, 4, 7, 2, 6, 4, 6, 7, 6, 5, 5, 4, 3, 7, 6, 7, 5, 6, 5, 6, 7, 7, 6, 6, 7, 5, 7, 7, 6, 7, 5, 7, 6)

drowsyturkey <- data.frame(Drowsy1, Drowsy2, Drowsy3)
id.num = c(1:nrow(drowsyturkey))
drowsyturkey <- cbind(id.num, drowsyturkey)
View(drowsyturkey)
colnames(drowsyturkey) = c("id.num", "drowsy_alcno_turkno", "drowsy_alcyes_turkno", "drowsy_alcyes_turkyes")
drowsyturkey2 <- wideToLong(drowsyturkey, within = c("Alcohol Consumption", "Turkey Consuption"))
View(drowsyturkey2)

Then, I tried to make a graph one more time…. It did not work:

ggbarplot[drowsyturkey2(drowsyturkey2, "Turkey Consumption", "drowsy",
color = "Alcohol Consumption", palette = c("#0072B2", "#E69F00"),
add = "mean_ci", position = position_dodge(0.9))+
scale_y_continuous(expand = c(0,0)) +
coord_cartesian(ylim = c(0, 7)) +
labs(y = "Drowsiness Scale Rating (1-7)")

This is the error I get in result:

ggbarplot[drowsyturkey2(drowsyturkey2, "Turkey Consumption", "drowsy",

  •       color = "Alcohol Consumption", palette = c("#0072B2", "#E69F00"),
    
  • scale_y_continuous(expand = c(0,0)) +
  • labs(y = "Drowsiness Scale Rating (1-7)")
  •       add = "mean_ci", position = position_dodge(0.9))+
    

Error: unexpected symbol in:
" labs(y = "Drowsiness Scale Rating (1-7)")
add"
add = "mean_ci", position = position_dodge(0.9))+
Error: unexpected ',' in " add = "mean_ci","

If you can help me out that would be awesome, Thanks.

Put a newline before 'View(turkeydata2)'; you might also want to close up the space in " turkey consumption", although that doesn't cause this error.

This line of code produces a column name that starts with an empty space (i.e. " turkey consumption") so you need to refer to it the same way in your call to ggbarplot().

This is not valid syntax

If you fix these two things you should get a plot like this as output

library(ggpubr)

# Sample data in a copy/paste friendly format for reproducibility 
# (you don't need to do this, you can use your own dataframe directly)
turkeydata2 <- data.frame(
       stringsAsFactors = FALSE,
            check.names = FALSE,
                 id.num = c(1L,2L,3L,4L,5L,
                            6L,7L,8L,9L,10L,11L,12L,13L,14L,15L,16L,
                            17L,18L,19L,20L,21L,22L,23L,24L,25L,26L,27L,
                            28L,29L,30L,31L,32L,33L,34L,35L,36L,1L,2L,
                            3L,4L,5L,6L,7L,8L,9L,10L,11L,12L,13L,
                            14L,15L,16L,17L,18L,19L,20L,21L,22L,23L,24L,
                            25L,26L,27L,28L,29L,30L,31L,32L,33L,34L,
                            35L,36L,1L,2L,3L,4L,5L,6L,7L,8L,9L,10L,
                            11L,12L,13L,14L,15L,16L,17L,18L,19L,20L,21L,
                            22L,23L,24L,25L,26L,27L,28L,29L,30L,31L,
                            32L,33L,34L,35L,36L),
                 drowsy = c(4,5,1,5,2,5,1,
                            2,1,2,3,4,2,2,5,5,3,1,1,4,1,1,1,1,3,
                            2,3,1,6,1,5,1,3,2,1,3,3,4,2,6,3,5,
                            2,3,3,2,3,4,3,3,6,4,6,6,4,7,4,7,3,
                            7,6,6,1,2,1,1,6,5,7,1,7,5,6,2,6,7,4,
                            7,2,6,4,6,7,6,5,5,4,3,7,6,7,5,6,5,
                            6,7,7,6,6,7,5,7,7,6,7,5,7,6),
                content = c(5,4,5,5,5,6,7,
                            5,3,5,3,3,3,4,3,3,4,7,7,6,6,5,4,1,4,
                            2,4,5,4,3,3,7,6,4,4,6,5,3,6,4,5,4,
                            6,5,4,6,5,2,5,4,4,4,6,7,7,7,7,7,5,
                            7,6,3,7,7,7,5,7,7,3,7,6,7,4,6,3,2,5,
                            5,7,4,7,7,5,2,5,6,5,7,5,7,7,7,7,6,
                            7,7,7,5,7,6,3,6,7,7,6,7,7,6),
  `alcohol consumption` = c("noalc","noalc",
                            "noalc","noalc","noalc","noalc","noalc","noalc",
                            "noalc","noalc","noalc","noalc","noalc","noalc",
                            "noalc","noalc","noalc","noalc","noalc",
                            "noalc","noalc","noalc","noalc","noalc","noalc",
                            "noalc","noalc","noalc","noalc","noalc","noalc",
                            "noalc","noalc","noalc","noalc","noalc","alc",
                            "alc","alc","alc","alc","alc","alc","alc","alc",
                            "alc","alc","alc","alc","alc","alc","alc",
                            "alc","alc","alc","alc","alc","alc","alc",
                            "alc","alc","alc","alc","alc","alc","alc","alc",
                            "alc","alc","alc","alc","alc","alc","alc",
                            "alc","alc","alc","alc","alc","alc","alc","alc",
                            "alc","alc","alc","alc","alc","alc","alc",
                            "alc","alc","alc","alc","alc","alc","alc","alc",
                            "alc","alc","alc","alc","alc","alc","alc",
                            "alc","alc","alc","alc"),
  ` turkey consumption` = c("noturk","noturk",
                            "noturk","noturk","noturk","noturk","noturk",
                            "noturk","noturk","noturk","noturk","noturk",
                            "noturk","noturk","noturk","noturk","noturk",
                            "noturk","noturk","noturk","noturk","noturk","noturk",
                            "noturk","noturk","noturk","noturk","noturk",
                            "noturk","noturk","noturk","noturk","noturk",
                            "noturk","noturk","noturk","noturk","noturk",
                            "noturk","noturk","noturk","noturk","noturk",
                            "noturk","noturk","noturk","noturk","noturk","noturk",
                            "noturk","noturk","noturk","noturk","noturk",
                            "noturk","noturk","noturk","noturk","noturk",
                            "noturk","noturk","noturk","noturk","noturk",
                            "noturk","noturk","noturk","noturk","noturk",
                            "noturk","noturk","noturk","turk","turk","turk",
                            "turk","turk","turk","turk","turk","turk","turk",
                            "turk","turk","turk","turk","turk","turk",
                            "turk","turk","turk","turk","turk","turk","turk",
                            "turk","turk","turk","turk","turk","turk",
                            "turk","turk","turk","turk","turk","turk","turk")
)

# Relevant code
ggbarplot(turkeydata2, " turkey consumption", "drowsy",
          color = "alcohol consumption", palette = c("#0072B2", "#E69F00"),
          add = "mean_ci", position = position_dodge(0.9)) +
    scale_y_continuous(expand = c(0,0)) +
    coord_cartesian(ylim = c(0,7)) +
    labs(y = "Likert Scale Ratings(1-7)")

Created on 2021-12-10 by the reprex package (v2.0.1)

Note: Next time please provide a proper REPRoducible EXample (reprex) illustrating your issue.

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.