关于条形图x轴限制

时间:2015-12-04 03:43:56

标签: r bar-chart axis

我的条形图在x轴上有NA因此它会返回一些奇怪的数据图像 x轴应该是几个月,比如“Jan Feb Mar .... DEC”

ggplot(x, aes(x = Month, y = x$freq)) + geom_bar(aes(fill=x$Sex), stat = 'identity')

这是结果图

enter image description here

1 个答案:

答案 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")

enter image description here