Help with creating a barplot with ggplot2

I'm having issues generating my bar chart and am not sure what the issue might be, any help on this would be greatly appreciated. Please see code and myData below:

#install.packages("readxl")
#install.packages("tidyverse")
#install.packages("ggplot2")

library(readxl)
library(tidyverse)
library(ggplot2)

url <-'https://static-content.springer.com/esm/art%3A10.1038%2Fs41467-022-33628-8/MediaObjects/41467_2022_33628_MOESM16_ESM.xlsx'
#create a dataframe from the Excel data
temp <-tempfile()
download.file(url, temp, mode='wb')
myData <- read_excel(path = temp, sheet = "Fig_1b")

myData %>%
#select the rows and columns that are needed, 3 to 6 and 8
select(c(19:20)) %>%
slice(2:11) %>%

rename(Median = ...20,
Heart = Subgroup 7 "Heart") %>%
mutate(across(c(2), as.numeric))

Everything seems to work fine up until this point.

ggplot(aes(x=Median,y=Heart, color=Sample, fill=Sample, width=0.65)) +
stat_summary(fun.data = mean_se, geom = "bar", lwd=1)

After you run select(19:20), there are two columns in the data frame. You rename those to be Median and Heart. Your ggplot code refers to a column named Sample, which does not exist. Which column in the original data should be named Sample?

1 Like

Thankyou for your reply. Ah, I see I have edited the code but this error has popped up.

EDITED CODE:
ggplot(aes(x=Heart,y=Median, color= Heart, fill= Heart, width=0.65)) +
stat_summary(fun.data = mean_se, geom = "bar", lwd=1)

ERROR CODE
Error in fortify():
! data must be a <data.frame>, or an object coercible by fortify(), not an S3 object with class .
:information_source: Did you accidentally pass aes() to the data argument?
Run rlang::last_error() to see where the error occurred.

I have run the code again and it has fixed the problem. Thankyou for your help.

This topic was automatically closed 7 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.