下面是一个使用ggplot生成直方图的代码:
ggplot(res, aes(x=TOPIC,y=count)) +
scale_y_continuous(expand=c(0,0)) +
geom_bar(stat='identity') +
theme(axis.title.x=element_blank()) +
theme_bw(base_size = 16) +
ggtitle("Peer-reviewed papers per topic")
res<-structure(list(TOPIC = structure(c(20L, 18L, 21L, 3L, 9L, 4L,
7L, 8L, 17L, 27L, 29L, 2L, 24L, 30L, 16L, 28L, 10L, 22L, 12L,
5L, 1L, 19L, 6L, 25L, 11L, 23L, 14L, 15L, 13L, 26L), .Label = c("ANA",
"BEH", "BOUND", "CC", "DIS", "EVO", "HAB", "HABP", "HARV", "HWC",
"ISSUE", "METH", "MINOR", "PA", "PEFF", "PHYS", "POLL", "POPABU",
"POPGEN", "POPSTAT", "POPTR", "PRED", "PROT", "REPEC", "RESIMP",
"REV", "SHIP", "TEK", "TOUR", "ZOO"), class = "factor"), count = c(9,
7, 13, 5, 23, 35, 27, 5, 118, 0, 9, 22, 29, 46, 27, 12, 9, 44,
70, 40, 24, 19, 26, 2, 4, 17, 4, 10, 86, 31)), .Names = c("TOPIC",
"count"), row.names = c(NA, -30L), class = "data.frame")
主题有“人口状况”(POPSTAT),“人口趋势”(POPTREND)等名称。
我需要帮助解决一些问题:
1. The theme(axis.title.x=element_blank()) does not remove the x axis title.
2. The scale_y_continuous(expand=c(0,0)) puts the bar on the level of
the x-axis, which I need, but I need some expansion on the upper part
of the y-axis. How do I achieve that?
3. My labels from the data frame are abbreviations. How do I replace
these with new tick labels?
4. I need to turn the x-axis labels 90°.
答案 0 :(得分:1)
使用labs(x = NULL)
您的意思是ylim
吗?使用此选项,您可以设置y轴的限制。或使用scale_y_continuous(limits = c(0, <upperLimit>), expand = c(0, 0))
使用带标签的离散比例:scale_x_discrete(labels = <axis-labels>, limits = c(1:length(axis-labels))
theme(axis.text.x = element_text(angle = 90))