首页 文章

关于条形图x轴限制

提问于
浏览
0

我的条形图在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

    请尝试以下代码:

    # 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")
    

相关问题