【问题标题】:R Programming: How to put a label above the bar of the qplot? [duplicate]R Programming:如何在 qplot 的条形上方放置标签? [复制]
【发布时间】:2012-05-16 06:49:53
【问题描述】:

可能重复:
How to put labels over geom_bar in R with ggplot2

我一直在寻找如何在我的 qplot 栏的顶部放置标签的方法。这是下面的图片

和代码,

library(ggplot2)
library(colorRamps)
TawiTawiPop <- c(17000, 45000, 46000, 59000, 79000, 110000, 143000, 195000, 228204, 250718, 322317, 450346, 366550)
YearNames <- c("1903", "1918", "1939", "1948", "1960", "1970", "1975", "1980", "1990", "1995", "2000", "2007", "2010")

qplot(YearNames, TawiTawiPop, 
      xlab = expression(bold("Censal Year")), 
      ylab = expression(bold("Population")), 
      geom = "bar",
      stat = "identity", colour = I("red"), 
      fill = matlab.like2(13)) + theme_bw() + 
      opts(
        title = expression(bold("Tawi-Tawi Population from 1903 to 2010")),
        plot.title = theme_text(size = 18, colour = "darkblue"),
        legend.position = "none",
        panel.border = theme_rect(linetype = "dashed", colour = "red"))
        plot.title = theme_text(size = 18, colour = "darkblue")

现在我想在它的顶部放一个标签,如下所示。我在 Mathematica 中做了这个

那些标签“17000、45000 等等”,我想在我的 qplot 中有它。

【问题讨论】:

  • 我开始看,但 matlab.like2 对我造成了错误。也许我需要在你的代码运行之前安装一个特定的包(除了 ggplot2)?
  • 哦,对不起,我忘了。您需要为 matlab.like2 安装 colorRamps 包

标签: r label ggplot2


【解决方案1】:
library(ggplot2)
library(colorRamps)
TawiTawiPop <- c(17000, 45000, 46000, 59000, 79000, 110000, 143000, 195000, 228204, 250718, 322317, 450346, 366550)
YearNames <- c("1903", "1918", "1939", "1948", "1960", "1970", "1975", "1980", "1990", "1995", "2000", "2007", "2010")

qplot(YearNames, TawiTawiPop, 
      xlab = expression(bold("Censal Year")), 
      ylab = expression(bold("Population")), 
      geom = "bar",
      stat = "identity", colour = I("red"), 
      fill = matlab.like2(13)) + theme_bw() + 
      opts(
        title = expression(bold("Tawi-Tawi Population from 1903 to 2010")),
        plot.title = theme_text(size = 18, colour = "darkblue"),
        legend.position = "none",
        panel.border = theme_rect(linetype = "dashed", colour = "red"),
        plot.title = theme_text(size = 18, colour = "darkblue"))+
     geom_text(aes(label = TawiTawiPop,angle=90,hjust=-0.1))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-14
    • 2020-09-08
    • 1970-01-01
    • 1970-01-01
    • 2018-12-22
    相关资源
    最近更新 更多