我只想在两个月没有数据时绘制月度数据。我的代码如下,其中Months April和May的数据为空。但是,当我尝试绘制图表时,我收到错误
错误:(从警告转换)删除了包含非有限值的两行(stat_boxplot)。
如果我只是在每个条目中添加一个零(而不是NA),我会得到一个情节,但现在有一个不应该有的值(见https://i.stack.imgur.com/CHI51.png)。如果我添加na.omit(df),它只会删除两个月。有人可以帮我吗?
Cost_Delta<-c(85000,-32672.62,28335.64,-85000,30963.5,-28335.64,NA,NA,
-85000,32672.62,85000,-32672.62,-85000,-32672.62,85000,
-32672.62,-85000,32672.62,85000,32672.62,-85000,-32672.62)
Month<-c("Jan","Jan","Feb","Feb","Mar","Mar","Apr","May","Jun","Jun",
"Jul","Jul","Aug","Aug","Sep","Sep","Oct","Oct","Nov","Nov","Dec","Dec")
df<-data.frame(Cost_Delta,Month)
df$Month <- as.character(df$Month)
df$Month <- factor(df$Month, levels=unique(df$Month))
library(ggplot2)
p<-ggplot(df, aes(x=Month, y=Cost_Delta)) +
geom_point(aes(fill=Month), size=2, shape=21, colour="grey20",
position=position_jitter(width=0.2, height=0.1)) +
geom_boxplot(outlier.colour=NA, fill=NA, colour="grey20") +
scale_y_continuous(labels=scales::comma,breaks=seq(-300000,400000,50000)) +
labs(x="Month-Year", y="Cost Delta (Demand-Mean Forecast)")
p
答案 0 :(得分:2)
ggplot2
会在用户尝试绘制NA
值时发出警告。如果要显式忽略此行为,可以将参数na.rm = TRUE
与图层一起使用。
Cost_Delta<-c(85000,-32672.62,28335.64,-85000,30963.5,-28335.64,NA,NA,
-85000,32672.62,85000,-32672.62,-85000,-32672.62,85000,
-32672.62,-85000,32672.62,85000,32672.62,-85000,-32672.62)
Month<-c("Jan","Jan","Feb","Feb","Mar","Mar","Apr","May","Jun","Jun",
"Jul","Jul","Aug","Aug","Sep","Sep","Oct","Oct","Nov","Nov","Dec","Dec")
df <- data.frame(Cost_Delta, Month)
df$Month <- as.character(df$Month)
df$Month <- factor(df$Month, levels = unique(df$Month))
library(ggplot2)
p <- ggplot(df, aes(x = Month, y = Cost_Delta)) +
geom_point(
aes(fill = Month),
size = 2,
shape = 21,
colour = "grey20",
position = position_jitter(width = 0.2, height = 0.1),
na.rm = TRUE
) +
geom_boxplot(outlier.colour = NA,
fill = NA,
colour = "grey20",
na.rm = TRUE) +
scale_y_continuous(label = scales::comma, breaks = seq(-300000, 400000, 50000)) +
labs(x = "Month-Year", y = "Cost Delta (Demand-Mean Forecast)")
p