首页 > 解决方案 > 使用ggplot没有显示并排的栏

问题描述

我的问题与这个有关: 条形图中的并排条 我正在尝试做同样的事情,但我没有得到任何数据,轴已填充。但没有可视化图表。我试图以小步骤重新修改数据,但仍然没有成功。我不知道,为什么没有显示数据。

n<-15
data <- data.frame("number" = c(1:n), 
              "Nasal" = c(15.4, 13.5, 13.3, 12.4, 12.8,
                          13.5, 14.5, 13.9, 11.0, 15.0,
                          17.0, 13.8, 17.4, 16.5, 14.4),
              "Endob" = c(16.5, 13.2, 13.6, 13.6, 14.0,
                          14.0, 16.0, 14.1, 11.5, 14.4,
                          16.0, 13.2, 16.6, 18.5, 14.5))

library(ggplot2)

method<-rep(c("Nasal","Endob"),each=n)
values<-c(data$Nasal, data$Endob)
patient<-factor(c(1:15))
data2<- data.frame(patient,method,values)
data2

ggplot(data2, aes(x=factor(patient), fill=method, y=values))
  geom_bar(position ="dodge",stat="identity")

标签: rggplot2geom-bar

解决方案


我认为它运行良好,你只是忘记了and+之间的关系。ggplotgeom_bar

ggplot(data2, aes(x=factor(patient), fill=method, y=values)) +
  geom_bar(position ="dodge",stat="identity")


推荐阅读