我的条形图在x轴上有NA
因此它会返回一些奇怪的数据图像
x轴应该是几个月,比如“Jan Feb Mar .... DEC”
ggplot(x, aes(x = Month, y = x$freq)) + geom_bar(aes(fill=x$Sex), stat = 'identity')
这是结果图
答案 0 :(得分:0)
请尝试以下代码:
# Sample dataframe
df <- data.frame(sex=c("Female","Female", "Male","Male"), month=c("Jan","Feb","Jan","Feb"), freq=c(130,160,160,175))
# Reorder the factor levels of a month column
df$month <- factor(df$month, levels = c("Jan","Feb"))
# ggplot
library('ggplot2')
ggplot(df, aes(x=month, y=freq, fill=sex)) + geom_bar(position="dodge", stat="identity")