绘制问题最高值显示在y轴的底部

时间:2016-06-20 05:53:33

标签: r ggplot2

我试图使用ggplot绘制我的数据集,即实习广告的数量,但出于某种原因,最高值显示在y轴的底部。

这是数据集:

month       internship_count_2012
January      68
February     48
March        43
April        49
May          52
June         83
July        104
August       91
September    72
October      58
November     70
December     77

情节的图像:

enter image description here

我运行以下代码来提出上述数据集,该数据集是从这里找到的较大的过滤数据集中提取的:https://www.dropbox.com/home?preview=rwjAll_internship_2012.csv

按月过滤数据集:

rwjAll_internship_jan2012 <- filter(rwjAll_internship_2012, month == 1)
rwjAll_internship_feb2012 <- filter(rwjAll_internship_2012, month == 2)
rwjAll_internship_mar2012 <- filter(rwjAll_internship_2012, month == 3)
rwjAll_internship_apr2012 <- filter(rwjAll_internship_2012, month == 4)
rwjAll_internship_may2012 <- filter(rwjAll_internship_2012, month == 5)
rwjAll_internship_jun2012 <- filter(rwjAll_internship_2012, month == 6)
rwjAll_internship_jul2012 <- filter(rwjAll_internship_2012, month == 7)
rwjAll_internship_aug2012 <- filter(rwjAll_internship_2012, month == 8)
rwjAll_internship_sep2012 <- filter(rwjAll_internship_2012, month == 9)
rwjAll_internship_oct2012 <- filter(rwjAll_internship_2012, month == 10)
rwjAll_internship_nov2012 <- filter(rwjAll_internship_2012, month == 11)
rwjAll_internship_dec2012 <- filter(rwjAll_internship_2012, month == 12)

按月获得实习广告数量

jan_2012_internship <- sum(rwjAll_internship_jan2012$internship)
feb_2012_internship <- sum(rwjAll_internship_feb2012$internship)
mar_2012_internship <- sum(rwjAll_internship_mar2012$internship)
apr_2012_internship <- sum(rwjAll_internship_apr2012$internship)
may_2012_internship <- sum(rwjAll_internship_may2012$internship)
jun_2012_internship <- sum(rwjAll_internship_jun2012$internship)
jul_2012_internship <- sum(rwjAll_internship_jul2012$internship)
aug_2012_internship <- sum(rwjAll_internship_aug2012$internship)
sep_2012_internship <- sum(rwjAll_internship_sep2012$internship)
oct_2012_internship <- sum(rwjAll_internship_oct2012$internship)
nov_2012_internship <- sum(rwjAll_internship_nov2012$internship)
dec_2012_internship <- sum(rwjAll_internship_dec2012$internship)

创建数据集以绘制2012年实习广告

month <- c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")

internship_count_2012<- c(jan_2012_internship, feb_2012_internship, mar_2012_internship, apr_2012_internship, may_2012_internship, jun_2012_internship, jul_2012_internship, aug_2012_internship, sep_2012_internship, oct_2012_internship, nov_2012_internship, dec_2012_internship)

all_internship_2012 <- cbind(month, internship_count_2012)
all_internship_2012 <- as.data.frame(all_internship_2012)

plot2 <- ggplot(all_internship_2012, aes(x = month, y = internship_count_2012, group = 1)) +
       geom_line(colour="purple1") +
       geom_point(size=0.5, colour="purple1") +
       scale_x_discrete(limits = c("January", "February", "March", "April", "May",                                 "June", "July", "August", "September", "October", "November",                              "December")) +
       theme(axis.text.x = element_text(angle = 45, hjust = 1))

如何订购y值(internship_count_2012)以使最高值位于顶部?谢谢!

1 个答案:

答案 0 :(得分:0)

以下代码极大地简化了新数据集的创建 库 dplyr 并在几行内生成您想要的情节。

library(dplyr)
library(ggplot2)

    all_internship_2012 <- rwjAll_internship_2012 %>%
                           group_by(month) %>%
                           summarise(internship_count_2012 = sum(internship))

    month <- c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")

    ggplot(all_internship_2012, aes(x = month, y = internship_count_2012)) + 
        geom_line(colour = "purple1") + 
        geom_point(size=0.5, colour="purple1") +
        scale_x_continuous(breaks=1:12, labels = month) +
        theme(axis.text.x = element_text(angle = 45, hjust = 1))

有关 dplyr 的信息,请查看this link on CRAN