Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 打印刻面网格条打印时缺少值的错误消息_R_Ggplot2_Geom Bar_Facet Grid - Fatal编程技术网

R 打印刻面网格条打印时缺少值的错误消息

R 打印刻面网格条打印时缺少值的错误消息,r,ggplot2,geom-bar,facet-grid,R,Ggplot2,Geom Bar,Facet Grid,我正在使用ggplot2制作一个刻面网格条图。当我的数据集中没有缺失值时,我不断收到错误消息“删除了包含缺失值的15行(geom_bar)” 这里有一个可复制的例子,我正在绘制学校对冰淇淋口味的喜爱程度: IDs <- seq(1,50) IDs <- data.frame(rep(IDs, each = 5)) names(IDs)[1] <- "ID" tastes <- c("Strawberry", "Vanil

我正在使用ggplot2制作一个刻面网格条图。当我的数据集中没有缺失值时,我不断收到错误消息“
删除了包含缺失值的15行(geom_bar)

这里有一个可复制的例子,我正在绘制学校对冰淇淋口味的喜爱程度:

IDs <- seq(1,50)
IDs <- data.frame(rep(IDs, each = 5))
names(IDs)[1] <- "ID"

tastes <- c("Strawberry", "Vanilla", "Chocolate", "Matcha", "Sesame")
tastes <- data.frame(rep(tastes, times = 50))

#random numbers for schools 
A <- runif(250, 1,5)
B <- runif(250, 1,5)
C <- runif(250, 1,5)

#merge
test <- cbind(IDs, tastes)
test <- cbind(test, A)
test <- cbind(test, B)
test <- cbind(test, C)
names(test)[2] <- "Flavour"
#make long
test_long <- melt(test, 
                   id.vars = c("ID", "Flavour"))

#plot
plot <- ggplot(test_long) +
  geom_bar(aes(x = Flavour,
               y = value), stat="summary", fun=mean) + 
  scale_x_discrete(labels=c("C","M","S","S","V")) +
  scale_y_continuous(name = "Rating", limits = c(1, 5)) +
  facet_grid(. ~ variable) + 
  labs(title = "Likeability of Different Flavours by School") +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"))
plot

IDs条形图从零开始,因此使用
scale\u y\u continuous(limits=c(1,5))
在超出绘图窗口时修剪所有条形图。您可以通过将下限设置为0来解决此问题

或者,您可以将
scale_y_continuous()
(将数据修剪为仅在打印数据中的数据)替换为
coord_cartesian(ylim=c(1,5))
,后者打印将超出打印窗口的数据


?coord_cartesian
的帮助文件解释了设置轴的两种不同方法,这在绘制数据摘要或拟合平滑器时会产生很大的差异。

这非常有用,非常感谢!!