stacked bar chart issues: Error in FUN(X[[i]], ...)

  1. i had to re-classify types of pupil abnormalities

sub$pupil_auto <- paste(sub$normalpupil, sub$pupil_abn, sep = ":")

sub$pupil_auto <- ifelse(sub$pupil_auto == "1:NA", "normal", sub$pupil_auto)
sub$pupil_auto <- ifelse(sub$pupil_auto == "2:1", "sympathetic", sub$pupil_auto)
sub$pupil_auto <- ifelse(sub$pupil_auto == "2:2", "parasympathetic", sub$pupil_auto)
sub$pupil_auto <- ifelse(sub$pupil_auto == "2:3", "both", sub$pupil_auto)
sub$pupil_auto <- ifelse(sub$pupil_auto == "1:1", "sympathetic", sub$pupil_auto)
sub$pupil_auto <- ifelse(sub$pupil_auto == "NA:NA", NA, sub$pupil_auto)

sub$pupil_auto <- as.factor(sub$pupil_auto)
class(sub$pupil_auto)

  1. reorder groups in preparation for plot

sub$pupil_auto <- factor(sub$pupil_auto,
levels = c("both", "parasympathetic", "sympathetic", "normal"), ordered = TRUE)

table(sub$pupil_auto, sub$final_dx_groups2)

  1. stacked bar chart coding

png("Pupil_barchart.png",width = 800, height = 480)
ecols <- c(normal="seashell", sympathetic = "pink", parasympathetic = "steelblue1", both = "orchid")
ggplot(data = subpup,aes(x = final_dx_groups2, fill = pupil_auto)) + geom_bar() +
scale_fill_manual(values = ecols) +
theme(text = element_text(size=18), axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
labs(fill = "Pupil deficits") +
ylab("Number of patients") +
xlab("Final Diagnosis") +
ggtitle("Pupillometry")
dev.off()

error received:
Error in FUN(X[[i]], ...) : object 'pupil_auto' not found

HELP!

your chart is being made from subpup which has no other mention in your code.
It seemed like it would have been sub, as that has many mentions.

1 Like

I changed it to sub (thank you for noticing), this solved my error in FUN(X[[i]],...) and now my graph looks like this:

how would I bring the box plots down to the x axis?

ggplot offers functions to alter the axis limits.
see : Set scale limits — lims • ggplot2 (tidyverse.org)

thanks for your help

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.