R 并排条形图中的颜色问题

R 并排条形图中的颜色问题,r,ggplot2,R,Ggplot2,我不知道如何让这个并排的条形图以我想要的方式显示。我试图创建一个并排的条形图,在水平轴上显示恶心(是与否),并用颜色编码的条形图指示麻醉剂的类型。我有并排的情节,但颜色代表恶心的是或否,而不是麻醉,但传说它们代表麻醉。以下是我的数据: Nausea_Y Nausea_N Anesthesia_A 26 13 Anesthesia_B 10

我不知道如何让这个并排的条形图以我想要的方式显示。我试图创建一个并排的条形图,在水平轴上显示恶心(是与否),并用颜色编码的条形图指示麻醉剂的类型。我有并排的情节,但颜色代表恶心的是或否,而不是麻醉,但传说它们代表麻醉。以下是我的数据:

                Nausea_Y              Nausea_N
Anesthesia_A          26                    13         
Anesthesia_B          10                    23
这是我的代码:


plotdata=data.frame(Anesthesia_Type=c("Anesthesia A","Anesthesia B"),
                    Anesthesia_A=c(anes_df$Nausea_Y[1],anes_df$Nausea_N[1]),
                    Anesthesia_B=c(anes_df$Nausea_Y[2],anes_df$Nausea_N[2]),
                    row.names = c("st1","st2"))

plotdata
d = melt(plotdata, id.vars = "Anesthesia_Type")

ggplot(data = d,
       mapping = aes(x = Anesthesia_Type, y = value, fill = variable)) +
    geom_col(position = position_dodge())


感谢您的帮助。我知道我不知怎么把它倒过来了,但不知道如何更改它。

假设数据是以如上所示的表开始的(麻醉剂是一个行名,而不是一个显式变量):


dat这很简单。非常感谢。
dat <- read.table(text = "Nausea_Y          Nausea_N
Anesthesia_A          26                    13         
Anesthesia_B          10                    23", header = TRUE)

# Reshape to long format
plotdata <- as.data.frame.table(as.matrix(dat))

library(ggplot2)

ggplot(plotdata,
       mapping = aes(x = Var2, y = Freq, fill = Var1)) +
  geom_col(position = position_dodge()) +
  labs(fill = "Anesthetic type", x = "Nausea") +
  scale_x_discrete(labels = c("Yes", "No"))