【问题标题】:How do I get a barplot from a dataset?如何从数据集中获取条形图?
【发布时间】:2018-07-08 21:00:56
【问题描述】:

我有这个包叫gapminder

我正在尝试这样做

一个。显示美国多年来预期寿命的条形图。 (数据来源:gapminder)

制作一个条形图,显示美国多年来的预期寿命。但我不完全确定如何做到这一点。

谁能告诉我如何做到这一点,我做了names(gapminder),我得到了:

"country"   "continent" "year"      "lifeExp"   "pop"       "gdpPercap"

【问题讨论】:

  • 嗨,欢迎来到 Stack Overflow。人们为您完成了工作(您应该接受答案),但通常您应该在发布问题之前表明您已经努力自己解决问题。例如,您应该阅读 ?barplot 以了解如何制作条形图。然后,如果您显示不起作用的代码,人们会有所帮助。这可能就是您投反对票的原因。

标签: r bar-chart


【解决方案1】:

您可以在使用table() 转换数据后使用barplot() 函数。 举个例子,看看here

【讨论】:

    【解决方案2】:

    首先您需要进行子集化(选择您要使用的数据部分),然后您可以绘制。我对情节进行了一些调整,默认形式的可读性不是很好。

    library(gapminder)
    
    # this selects all the rows where the column 'country' says 'United States'
    us <- gapminder[gapminder$country == "United States", ]
    
    # the first argument is the life expectancy values
    # I subtract 65 to make the difference between the various years more apparent
    barplot(us$lifeExp - 65, names.arg=us$year, main="US life expectancy", 
      xlab="Year", ylab="Age", axes=FALSE)
    axis(2, at=0:15, labels=65:80, cex.axis=0.8)
    abline(h=0:15, col="#00000044", lty=2)
    

    【讨论】:

      猜你喜欢
      • 2020-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-17
      • 1970-01-01
      • 2016-02-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多