在R中的条形图和饼图中包含值 [英] Include values to the barplot and pie charts in R

查看:121
本文介绍了在R中的条形图和饼图中包含值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种形式的数据:

proprete.freq <- table(cnData$proprete)
proprete.freq.genre <-table(cnData$genre,cnData$proprete)

我正在使用以下功能(条形图和饼图)来绘制数据:

I am using these functions (barplot and pie) to plot the data:

barplot(proprete.freq.genre, col = heat.colors(length(rownames(proprete.freq.genre)))
    , main="Proprete", beside = TRUE)

pie(proprete.freq, col=rainbow(3), names.arg=avis, main="Propreté")

这是结果:

问题:如何在条形图的顶部和饼图的类别变量下方包括值. 像这样的东西:

Question: How to include the value just on top of the barplots and below the categorical variables for the pie. Something like that:

推荐答案

这是一种通用方法:

  1. pie具有标签参数,因此您可以使用它,将\n用作换行符,并在名称下添加文本
  2. barplot的返回值是每个小节的x坐标,因此只需将text与数据(对于y坐标)一起使用,然后使用pos = 3将标签放在该坐标上方{x, y}点.
  1. pie has a labels parameter so you can just use that, use \n for a line break, and add text under the name
  2. barplot has a return value which are the x-coordinates of each bar, so just use text along with the data (for the y-coordinates), and use pos = 3 to put the label above that {x, y} point.

例如:

par(mfrow = c(2, 1))
pie(1:3, labels = sprintf('%s\n%s%%', 1:3, round(1:3 / 6 * 100)))
bp <- barplot(VADeaths, beside = TRUE)
text(c(bp), c(VADeaths), labels = c(VADeaths), pos = 3, xpd = NA)

这篇关于在R中的条形图和饼图中包含值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆